-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a61bd4a
commit f63341c
Showing
7 changed files
with
297 additions
and
49 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/controllers/identity-controllers/keypairs-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { EdcConnectorClientContext } from "../../context"; | ||
import { KeyPair } from "../../entities/keypairs"; | ||
import { Inner } from "../../inner"; | ||
|
||
export class KeyPairsController { | ||
#inner: Inner; | ||
#context?: EdcConnectorClientContext; | ||
|
||
constructor(inner: Inner, context?: EdcConnectorClientContext) { | ||
this.#inner = inner; | ||
this.#context = context; | ||
} | ||
|
||
async queryAll( | ||
query: { offset?: string; limit?: string } = {}, | ||
context?: EdcConnectorClientContext, | ||
) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<KeyPair[]>(actualContext.identity, { | ||
path: "/v1alpha/keypairs", | ||
method: "GET", | ||
apiToken: actualContext.apiToken, | ||
query, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...ntrollers/identity-controllers/participant-controllers/participant-keypairs-controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { EdcConnectorClientContext } from "../../../context"; | ||
import { KeyDescriptor, KeyPair } from "../../../entities/keypairs"; | ||
import { Inner } from "../../../inner"; | ||
|
||
export class ParticipantKeyPairContoller { | ||
#inner: Inner; | ||
#context?: EdcConnectorClientContext; | ||
|
||
constructor( | ||
inner: Inner, | ||
public participantId: string, | ||
context?: EdcConnectorClientContext, | ||
) { | ||
this.#inner = inner; | ||
this.#context = context; | ||
} | ||
|
||
getKeyPair(keyPairId: string, context?: EdcConnectorClientContext) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<KeyPair>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs/${keyPairId}`, | ||
method: "GET", | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
|
||
queryAllKeyPairs(context?: EdcConnectorClientContext) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<KeyPair[]>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs`, | ||
method: "GET", | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
|
||
createKeyPair( | ||
keyDescriptor: KeyDescriptor, | ||
makeDefault = false, | ||
context?: EdcConnectorClientContext, | ||
) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<void>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs`, | ||
method: "PUT", | ||
body: keyDescriptor, | ||
query: { | ||
makeDefault: String(makeDefault), | ||
}, | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
|
||
activate(keyPairId: string, context?: EdcConnectorClientContext) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<void>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs/${keyPairId}/activate`, | ||
method: "POST", | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
|
||
revoke( | ||
keyPairId: string, | ||
newKeyDescriptor: KeyDescriptor, | ||
context?: EdcConnectorClientContext, | ||
) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<void>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs/${keyPairId}/revoke`, | ||
method: "POST", | ||
body: newKeyDescriptor, | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
|
||
rotate( | ||
keyPairId: string, | ||
duration?: number, | ||
newKeyDescriptor?: KeyDescriptor, | ||
context?: EdcConnectorClientContext, | ||
) { | ||
const actualContext = context || this.#context!; | ||
|
||
return this.#inner.request<void>(actualContext.identity, { | ||
path: `/v1alpha/participants/${this.participantId}/keypairs/${keyPairId}/rotate`, | ||
method: "POST", | ||
body: newKeyDescriptor, | ||
query: { | ||
duration: String(duration), | ||
}, | ||
apiToken: actualContext.apiToken, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export interface KeyPair { | ||
defaultPair: boolean; | ||
groupName: string; | ||
id: string; | ||
keyContext: string; | ||
keyId: string; | ||
participantId: string; | ||
privateKeyAlias: string; | ||
rotationDuration: number; | ||
serializedPublicKey: string; | ||
state: number; | ||
timestamp: number; | ||
useDuration: number; | ||
} | ||
|
||
export interface KeyDescriptor { | ||
active: boolean; | ||
keyGeneratorParams: { | ||
[key: string]: unknown; | ||
}; | ||
keyId: string; | ||
privateKeyAlias: string; | ||
publicKeyJwk: { | ||
[key: string]: unknown; | ||
}; | ||
publicKeyPem: string; | ||
resourceId: string; | ||
type: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { GenericContainer, StartedTestContainer } from "testcontainers"; | ||
import { EdcConnectorClient } from "../../../src"; | ||
import { KeyPairsController } from "../../../src/controllers/identity-controllers/keypairs-controller"; | ||
import { ParticipantKeyPairContoller } from "../../../src/controllers/identity-controllers/participant-controllers/participant-keypairs-controller"; | ||
import { KeyDescriptor } from "../../../src/entities/keypairs"; | ||
|
||
describe("Key Pairs", () => { | ||
let startedContainer: StartedTestContainer; | ||
let participantKeyPairs: ParticipantKeyPairContoller; | ||
let keyPairs: KeyPairsController; | ||
|
||
beforeAll(async () => { | ||
startedContainer = await new GenericContainer("stoplight/prism:5.8.1") | ||
.withCopyFilesToContainer([ | ||
{ | ||
source: "node_modules/identity-api.yml", | ||
target: "/identity-api.yml", | ||
}, | ||
]) | ||
.withCommand(["mock", "-h", "0.0.0.0", "/identity-api.yml"]) | ||
.withExposedPorts(4010) | ||
.start(); | ||
|
||
keyPairs = new EdcConnectorClient.Builder() | ||
.identityUrl("http://localhost:" + startedContainer.getFirstMappedPort()) | ||
.build().identity.keyPairs; | ||
|
||
participantKeyPairs = new EdcConnectorClient.Builder() | ||
.identityUrl("http://localhost:" + startedContainer.getFirstMappedPort()) | ||
.build() | ||
.identity.participant("1").keypairs; | ||
}); | ||
|
||
afterAll(async () => { | ||
await startedContainer.stop(); | ||
}); | ||
|
||
it("should query all key pairs", async () => { | ||
const keyPairs = await participantKeyPairs.queryAllKeyPairs(); | ||
|
||
expect(keyPairs).not.toBeNull(); | ||
expect(keyPairs.length).toBeGreaterThan(0); | ||
expect(keyPairs[0]).toHaveProperty("defaultPair"); | ||
expect(keyPairs[0]).toHaveProperty("groupName"); | ||
expect(keyPairs[0]).toHaveProperty("id"); | ||
expect(keyPairs[0]).toHaveProperty("keyContext"); | ||
expect(keyPairs[0]).toHaveProperty("keyId"); | ||
expect(keyPairs[0]).toHaveProperty("participantId"); | ||
expect(keyPairs[0]).toHaveProperty("privateKeyAlias"); | ||
expect(keyPairs[0]).toHaveProperty("rotationDuration"); | ||
expect(keyPairs[0]).toHaveProperty("serializedPublicKey"); | ||
expect(keyPairs[0]).toHaveProperty("state"); | ||
expect(keyPairs[0]).toHaveProperty("timestamp"); | ||
expect(keyPairs[0]).toHaveProperty("useDuration"); | ||
}); | ||
|
||
it("should get key pair", async () => { | ||
const keyPair = await participantKeyPairs.getKeyPair("1"); | ||
|
||
expect(keyPair).not.toBeNull(); | ||
expect(keyPair).toHaveProperty("defaultPair"); | ||
expect(keyPair).toHaveProperty("groupName"); | ||
expect(keyPair).toHaveProperty("id"); | ||
expect(keyPair).toHaveProperty("keyContext"); | ||
expect(keyPair).toHaveProperty("keyId"); | ||
expect(keyPair).toHaveProperty("participantId"); | ||
expect(keyPair).toHaveProperty("privateKeyAlias"); | ||
expect(keyPair).toHaveProperty("rotationDuration"); | ||
expect(keyPair).toHaveProperty("serializedPublicKey"); | ||
expect(keyPair).toHaveProperty("state"); | ||
expect(keyPair).toHaveProperty("timestamp"); | ||
expect(keyPair).toHaveProperty("useDuration"); | ||
}); | ||
|
||
it("should create a key pair", async () => { | ||
const newKeyDescriptor: KeyDescriptor = { | ||
privateKeyAlias: "", | ||
type: "", | ||
keyId: "", | ||
active: true, | ||
resourceId: "", | ||
publicKeyJwk: {}, | ||
publicKeyPem: "", | ||
keyGeneratorParams: {}, | ||
}; | ||
|
||
expect( | ||
participantKeyPairs.createKeyPair(newKeyDescriptor), | ||
).resolves.not.toThrow(); | ||
}); | ||
|
||
it("should query All", () => { | ||
const keyPairsList = keyPairs.queryAll(); | ||
|
||
expect(keyPairsList).not.toBeNull(); | ||
}); | ||
|
||
it("should activate a key pair", () => { | ||
expect(participantKeyPairs.activate("1")).resolves.not.toThrow(); | ||
}); | ||
|
||
it("should revoke a key pair", () => { | ||
const newKeyDescriptor: KeyDescriptor = { | ||
privateKeyAlias: "", | ||
type: "", | ||
keyId: "", | ||
active: true, | ||
resourceId: "", | ||
publicKeyJwk: {}, | ||
publicKeyPem: "", | ||
keyGeneratorParams: {}, | ||
}; | ||
|
||
expect( | ||
participantKeyPairs.revoke("1", newKeyDescriptor), | ||
).resolves.not.toThrow(); | ||
}); | ||
|
||
it("should rotate a key pair", () => { | ||
expect(participantKeyPairs.rotate("1")).resolves.not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters