-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for creating offer deeplink from object and test …
…it. plus some refactors Signed-off-by: sksadjad <[email protected]>
- Loading branch information
Showing
9 changed files
with
76 additions
and
42 deletions.
There are no files selected for viewing
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
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
27 changes: 27 additions & 0 deletions
27
packages/issuer/lib/__tests__/CredentialOfferUtils.spec.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 { CredentialFormatEnum } from '@sphereon/openid4vci-common' | ||
|
||
import { createCredentialOfferDeeplink } from '../index' | ||
|
||
describe('CredentialOfferUtils should', () => { | ||
it('create a deeplink from credentialOffer object', () => { | ||
// below is the example from spec (https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0-11.html#name-sending-credential-offer-by) and is wrong, the issuer_state should be in the grants and not a top-level property | ||
// openid-credential-offer://credential_offer=%7B%22credential_issuer%22:%22https://credential-issuer.example.com%22,%22credentials%22:%5B%7B%22format%22:%22jwt_vc_json%22,%22types%22:%5B%22VerifiableCredential%22,%22UniversityDegreeCredential%22%5D%7D%5D,%22issuer_state%22:%22eyJhbGciOiJSU0Et...FYUaBy%22%7D | ||
const credentialOffer = { | ||
credential_issuer: 'https://credential-issuer.example.com', | ||
credentials: [ | ||
{ | ||
format: CredentialFormatEnum.jwt_vc_json, | ||
types: ['VerifiableCredential', 'UniversityDegreeCredential'], | ||
}, | ||
], | ||
grants: { | ||
authorization_code: { | ||
issuer_state: 'eyJhbGciOiJSU0Et...FYUaBy', | ||
}, | ||
}, | ||
} | ||
expect(createCredentialOfferDeeplink(undefined, { credentialOffer })).toEqual( | ||
'openid-credential-offer://?credential_offer=credential_issuer=https%3A%2F%2Fcredential-issuer.example.com&credentials=%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D&grants=%7B%22authorization_code%22%3A%7B%22issuer_state%22%3A%22eyJhbGciOiJSU0Et...FYUaBy%22%7D%7D' | ||
) | ||
}) | ||
}) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
import { CredentialOfferPayload, CredentialOfferPayloadV1_0_11, encodeJsonAsURI, IssuerMetadata } from '@sphereon/openid4vci-common' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
export function createCredentialOfferDeeplink( | ||
issuerMetadata?: IssuerMetadata, | ||
opts?: { state?: string; credentialOffer?: CredentialOfferPayload; preAuthorizedCode?: string; userPinRequired?: boolean } | ||
): string { | ||
// openid-credential-offer://credential_offer=%7B%22credential_issuer%22:%22https://credential-issuer.example.com | ||
// %22,%22credentials%22:%5B%7B%22format%22:%22jwt_vc_json%22,%22types%22:%5B%22VerifiableCr | ||
// edential%22,%22UniversityDegreeCredential%22%5D%7D%5D,%22issuer_state%22:%22eyJhbGciOiJSU0Et... | ||
// FYUaBy%22%7D | ||
if (!issuerMetadata && !opts?.credentialOffer) { | ||
throw new Error('You have to provide issuerMetadata or credentialOffer object for creating a deeplink') | ||
} | ||
if (opts?.credentialOffer) { | ||
return `openid-credential-offer://?credential_offer=${encodeJsonAsURI(opts.credentialOffer)}` | ||
} | ||
const credentialOfferPayload = { | ||
credential_issuer: issuerMetadata?.credential_issuer, | ||
credentials: issuerMetadata?.credentials_supported, | ||
grants: { | ||
authorization_code: { | ||
issuer_state: opts && opts.state ? opts.state : uuidv4(), | ||
}, | ||
}, | ||
} as CredentialOfferPayloadV1_0_11 | ||
if (opts?.preAuthorizedCode) { | ||
if (!credentialOfferPayload.grants) { | ||
credentialOfferPayload.grants = {} | ||
} | ||
credentialOfferPayload.grants['urn:ietf:params:oauth:grant-type:pre-authorized_code'] = { | ||
'pre-authorized_code': opts.preAuthorizedCode, | ||
user_pin_required: opts.userPinRequired ? opts.userPinRequired : false, | ||
} | ||
} | ||
return `openid-credential-offer://?credential_offer=${encodeJsonAsURI(credentialOfferPayload)}` | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './CredentialOffer' | ||
export * from './CredentialOfferUtils' |