Skip to content

Commit

Permalink
feat: Support data supplier callback
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Jun 3, 2023
1 parent d9dae74 commit 1c49cc8
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ xdescribe('issuerCallback', () => {
.withCredentialOfferStateManager(stateManager)
.withInMemoryCNonceState()
.withJWTVerifyCallback(verifyCallbackFunction)
.withIssuerCallback(() =>
.withCredentialSignerCallback(() =>
Promise.resolve({
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
Expand Down Expand Up @@ -225,10 +225,11 @@ xdescribe('issuerCallback', () => {
type: ['VerifiableCredential'],
})

const credentialResponse = await vcIssuer.issueCredentialFromIssueRequest({
const credentialResponse = await vcIssuer.issueCredential({
credentialRequest: credentialRequest,
credential,
responseCNonce: state,
issuerCallback: getIssuerCallback(credential, didKey.keyPairs, didKey.didDocument.verificationMethod[0].id),
credentialSignerCallback: getIssuerCallback(credential, didKey.keyPairs, didKey.didDocument.verificationMethod[0].id),
})

expect(credentialResponse).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion packages/common/lib/functions/CredentialOfferUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export async function toUniformCredentialOfferRequest(
let originalCredentialOffer = offer.credential_offer;
let credentialOfferURI: string | undefined;
if ('credential_offer_uri' in offer && offer?.credential_offer_uri !== undefined) {
credentialOfferURI = offer.credential_offer_uri;
if (opts?.resolve || opts?.resolve === undefined) {
credentialOfferURI = offer.credential_offer_uri;
originalCredentialOffer = (await resolveCredentialOfferURI(credentialOfferURI)) as CredentialOfferPayloadV1_0_11;
} else if (!originalCredentialOffer) {
throw Error(`Credential offer uri (${credentialOfferURI}) found, but resolution was explicitly disabled and credential_offer was supplied`);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/lib/types/StateManager.types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CredentialOfferV1_0_11 } from './v1_0_11.types';
import { AssertedUniformCredentialOffer } from './CredentialIssuance.types';

export interface StateType {
createdAt: number;
}

export interface CredentialOfferSession extends StateType {
clientId?: string;
credentialOffer: CredentialOfferV1_0_11;
credentialOffer: AssertedUniformCredentialOffer;
userPin?: string;
issuerState?: string; //todo: Probably good to hash it here, since it would come in from the client and we could match the hash and thus use the client value
preAuthorizedCode?: string; //todo: Probably good to hash it here, since it would come in from the client and we could match the hash and thus use the client value
Expand Down
18 changes: 13 additions & 5 deletions packages/issuer-rest/lib/OID4VCIServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class OID4VCIServer {
dotenv.config()

this._baseUrl = new URL(opts?.serverOpts?.baseUrl ?? process.env.BASE_URL ?? 'http://localhost')
const httpPort = getNumberOrUndefined(this._baseUrl.port) ?? getNumberOrUndefined(process.env.PORT) ?? 3000
const httpPort = opts?.serverOpts?.port ?? getNumberOrUndefined(this._baseUrl.port) ?? getNumberOrUndefined(process.env.PORT) ?? 3000
const host = opts?.serverOpts?.host ?? this._baseUrl.host.split(':')[0]

if (!opts?.serverOpts?.app) {
Expand All @@ -129,8 +129,8 @@ export class OID4VCIServer {
if (!this.isTokenEndpointDisabled(opts?.tokenEndpointOpts)) {
this.accessTokenEndpoint(opts?.tokenEndpointOpts)
}
this.cNonceExpiresIn = opts?.tokenEndpointOpts?.cNonceExpiresIn || 300
this.tokenExpiresIn = opts?.tokenEndpointOpts?.tokenExpiresIn || 300
this.cNonceExpiresIn = opts?.tokenEndpointOpts?.cNonceExpiresIn ?? 300000
this.tokenExpiresIn = opts?.tokenEndpointOpts?.tokenExpiresIn ?? 300000
this._server = this.app.listen(httpPort, host, () => console.log(`HTTP server listening on port ${httpPort}`))
}

Expand Down Expand Up @@ -209,14 +209,22 @@ export class OID4VCIServer {
this.app.post(path, async (request: Request, response: Response) => {
try {
const credentialRequest = request.body as CredentialRequestV1_0_11
const credential = await this.issuer.issueCredentialFromIssueRequest({
const credential = await this.issuer.issueCredential({
credentialRequest: credentialRequest,
tokenExpiresIn: this.tokenExpiresIn,
cNonceExpiresIn: this.cNonceExpiresIn,
})
return response.send(credential)
} catch (e) {
return sendErrorResponse(response, 500, e)
return sendErrorResponse(
response,
500,
{
error: 'invalid_request',
error_description: (e as Error).message,
},
e
)
}
})
}
Expand Down
25 changes: 18 additions & 7 deletions packages/issuer-rest/lib/__tests__/ClientIssuerIT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ describe('VcIssuer', () => {
.build()
const stateManager = new MemoryStates<CredentialOfferSession>()

const credential = {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
issuer: 'did:key:test',
issuanceDate: new Date().toISOString(),
credentialSubject: {},
}

vcIssuer = new VcIssuerBuilder()
// .withAuthorizationServer('https://authorization-server')
.withCredentialEndpoint('http://localhost:3456/test/credential-endpoint')
.withDefaultCredentialOfferBaseUri('http://localhost:3456/test')
.withCredentialIssuer(ISSUER_URL)
.withIssuerDisplay({
name: 'example issuer',
Expand All @@ -100,13 +109,15 @@ describe('VcIssuer', () => {
.withCredentialOfferStateManager(stateManager)
.withInMemoryCNonceState()
.withInMemoryCredentialOfferURIState()
.withIssuerCallback(() =>
.withCredentialDataSupplier(() =>
Promise.resolve({
format: 'ldp_vc',
credential,
})
)
.withCredentialSignerCallback(() =>
Promise.resolve({
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential'],
issuer: 'did:key:test',
issuanceDate: new Date().toISOString(),
credentialSubject: {},
...credential,
proof: {
type: IProofType.JwtProof2020,
jwt: 'ye.ye.ye',
Expand Down Expand Up @@ -283,7 +294,7 @@ describe('VcIssuer', () => {
proofCallbacks: { signCallback: proofOfPossessionCallbackFunction },
})
expect(credentialResponse).toMatchObject({
c_nonce_expires_in: 90000,
c_nonce_expires_in: 300000,
credential: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
credentialSubject: {},
Expand Down
Loading

0 comments on commit 1c49cc8

Please sign in to comment.