Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Jun 10, 2024
2 parents c4adecc + 68c89f4 commit 6a05e4d
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 15 deletions.
18 changes: 18 additions & 0 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ console.log(client.getCredentialEndpoint()); // https://issuer.research.identipr
console.log(client.getAccessTokenEndpoint()); // https://auth.research.identiproof.io/oauth2/token
```

Using https scheme
```typescript
import { OpenID4VCIClient } from '@sphereon/oid4vci-client';

// The client is initiated from a URI. This URI is provided by the Issuer, typically as a URL or QR code.
const client = await OpenID4VCIClient.fromURI({
uri: 'https://launchpad.vii.electron.mattrlabs.io?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Flaunchpad.vii.electron.mattrlabs.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22ldp_vc%22%2C%22types%22%3A%5B%22OpenBadgeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X%22%7D%7D%7D',
kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21#key-1', // Our DID. You can defer this also to when the acquireCredential method is called
alg: Alg.ES256, // The signing Algorithm we will use. You can defer this also to when the acquireCredential method is called
clientId: 'test-clientId', // The clientId if the Authrozation Service requires it. If a clientId is needed you can defer this also to when the acquireAccessToken method is called
retrieveServerMetadata: true, // Already retrieve the server metadata. Can also be done afterwards by invoking a method yourself.
});

console.log(client.getIssuer()); // https://launchpad.vii.electron.mattrlabs.io
console.log(client.getCredentialEndpoint()); // https://launchpad.vii.electron.mattrlabs.io/credential
console.log(client.getAccessTokenEndpoint()); // https://launchpad.vii.electron.mattrlabs.io/oauth2/token
```

## Server metadata

The OID4VCI Server metadata contains information about token endpoints, credential endpoints, as well as additional
Expand Down
8 changes: 5 additions & 3 deletions packages/client/lib/CredentialOfferClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ export class CredentialOfferClient {
const version = determineSpecVersionFromURI(uri);
let credentialOffer: CredentialOffer;
let credentialOfferPayload: CredentialOfferPayload;
// credential offer was introduced in draft 9 and credential_offer_uri in draft 11
if (version < OpenId4VCIVersion.VER_1_0_11) {
credentialOfferPayload = convertURIToJsonObject(uri, {
arrayTypeProperties: ['credential_type'],
requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri'] : ['issuer', 'credential_type'],
requiredProperties: uri.includes('credential_offer=') ? ['credential_offer'] : ['issuer', 'credential_type'],
}) as CredentialOfferPayloadV1_0_09;
credentialOffer = {
credential_offer: credentialOfferPayload,
};
} else {
credentialOffer = convertURIToJsonObject(uri, {
arrayTypeProperties: ['credentials'],
requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri'] : ['credential_offer'],
// It must have the '=' sign after credential_offer otherwise the uri will get split at openid_credential_offer
arrayTypeProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri='] : ['credential_offer='],
requiredProperties: uri.includes('credential_offer_uri=') ? ['credential_offer_uri='] : ['credential_offer='],
}) as CredentialOfferV1_0_11;
if (credentialOffer?.credential_offer_uri === undefined && !credentialOffer?.credential_offer) {
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri);
Expand Down
45 changes: 42 additions & 3 deletions packages/client/lib/__tests__/IT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ describe('OID4VCI-Client should', () => {
const INITIATE_QR =
'openid-initiate-issuance://?issuer=https%3A%2F%2Fissuer.research.identiproof.io&credential_type=OpenBadgeCredentialUrl&pre-authorized_code=4jLs9xZHEfqcoow0kHE7d1a8hUk6Sy-5bVSV2MqBUGUgiFFQi-ImL62T-FmLIo8hKA1UdMPH0lM1xAgcFkJfxIw9L-lI3mVs0hRT8YVwsEM1ma6N3wzuCdwtMU4bcwKp&user_pin_required=true';
const OFFER_QR =
'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';
'openid-credential-offer://?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Fissuer.research.identiproof.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22adhjhdjajkdkhjhdj%22%2C%22user_pin_required%22%3Atrue%7D%7D%7D';
const HTTPS_INITIATE_QR =
'https://issuer.research.identiproof.io?issuer=https%3A%2F%2Fissuer.research.identiproof.io&credential_type=OpenBadgeCredentialUrl&pre-authorized_code=4jLs9xZHEfqcoow0kHE7d1a8hUk6Sy-5bVSV2MqBUGUgiFFQi-ImL62T-FmLIo8hKA1UdMPH0lM1xAgcFkJfxIw9L-lI3mVs0hRT8YVwsEM1ma6N3wzuCdwtMU4bcwKp&user_pin_required=true';
const HTTPS_OFFER_QR_AUTHORIZATION_CODE =
'https://issuer.research.identiproof.io?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Fissuer.research.identiproof.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22authorization_code%22%3A%7B%22issuer_state%22%3A%22eyJhbGciOiJSU0Et...FYUaBy%22%7D%7D%7D';
const HTTPS_OFFER_QR_PRE_AUTHORIZED =
'https://issuer.research.identiproof.io?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Fissuer.research.identiproof.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22adhjhdjajkdkhjhdj%22%2C%22user_pin_required%22%3Atrue%7D%7D%7D';

function succeedWithAFullFlowWithClientSetup() {
nock(IDENTIPROOF_ISSUER_URL).get('/.well-known/openid-credential-issuer').reply(200, JSON.stringify(IDENTIPROOF_OID4VCI_METADATA));
Expand Down Expand Up @@ -78,7 +84,7 @@ describe('OID4VCI-Client should', () => {
await assertionOfsucceedWithAFullFlowWithClient(client);
});

test.skip('succeed with a full flow wit the client using OpenID4VCI version 11', async () => {
it('succeed with a full flow with the client using OpenID4VCI version 11 and deeplink', async () => {
succeedWithAFullFlowWithClientSetup();
const client = await OpenID4VCIClient.fromURI({
uri: OFFER_QR,
Expand All @@ -89,14 +95,47 @@ describe('OID4VCI-Client should', () => {
await assertionOfsucceedWithAFullFlowWithClient(client);
});

it('succeed with a full flow with the client using OpenID4VCI draft < 9 and https', async () => {
succeedWithAFullFlowWithClientSetup();
const client = await OpenID4VCIClient.fromURI({
uri: HTTPS_INITIATE_QR,
kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1',
alg: Alg.ES256,
clientId: 'test-clientId',
});
await assertionOfsucceedWithAFullFlowWithClient(client);
});

it('should succeed with a full flow with the client using OpenID4VCI draft > 11, https and authorization_code flow', async () => {
succeedWithAFullFlowWithClientSetup();
const client = await OpenID4VCIClient.fromURI({
uri: HTTPS_OFFER_QR_AUTHORIZATION_CODE,
kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1',
alg: Alg.ES256,
clientId: 'test-clientId',
});
await assertionOfsucceedWithAFullFlowWithClient(client);
});

it('should succeed with a full flow with the client using OpenID4VCI draft > 11, https and preauthorized_code flow', async () => {
succeedWithAFullFlowWithClientSetup();
const client = await OpenID4VCIClient.fromURI({
uri: HTTPS_OFFER_QR_PRE_AUTHORIZED,
kid: 'did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1',
alg: Alg.ES256,
clientId: 'test-clientId',
});
await assertionOfsucceedWithAFullFlowWithClient(client);
});

async function assertionOfsucceedWithAFullFlowWithClient(client: OpenID4VCIClient) {
expect(client.credentialOffer).toBeDefined();
expect(client.endpointMetadata).toBeDefined();
expect(client.getIssuer()).toEqual('https://issuer.research.identiproof.io');
expect(client.getCredentialEndpoint()).toEqual('https://issuer.research.identiproof.io/credential');
expect(client.getAccessTokenEndpoint()).toEqual('https://auth.research.identiproof.io/oauth2/token');

const accessToken = await client.acquireAccessToken({ pin: '1234' });
const accessToken = await client.acquireAccessToken({ pin: '1234', code: 'ABCD' });
expect(accessToken).toEqual(mockedAccessTokenResponse);

const credentialResponse = await client.acquireCredentials({
Expand Down
22 changes: 22 additions & 0 deletions packages/client/lib/__tests__/IssuanceInitiation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ describe('Issuance Initiation', () => {
expect(client.credential_offer.credential_issuer).toEqual('https://launchpad.vii.electron.mattrlabs.io');
expect(client.preAuthorizedCode).toEqual('UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X');
});

it('Should take an https url as input and return a Credential Offer', async () => {
const client = await CredentialOfferClient.fromURI(
'https://launchpad.vii.electron.mattrlabs.io?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Flaunchpad.vii.electron.mattrlabs.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22ldp_vc%22%2C%22types%22%3A%5B%22OpenBadgeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X%22%7D%7D%7D',
);
expect(client.version).toEqual(OpenId4VCIVersion.VER_1_0_11);
expect(client.baseUrl).toEqual('https://launchpad.vii.electron.mattrlabs.io');
expect(client.scheme).toEqual('https');
expect(client.credential_offer.credential_issuer).toEqual('https://launchpad.vii.electron.mattrlabs.io');
expect(client.preAuthorizedCode).toEqual('UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X');
})

it('Should take an http url as input and return a Credential Offer', async () => {
const client = await CredentialOfferClient.fromURI(
'http://launchpad.vii.electron.mattrlabs.io?credential_offer=%7B%22credential_issuer%22%3A%22http%3A%2F%2Flaunchpad.vii.electron.mattrlabs.io%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22ldp_vc%22%2C%22types%22%3A%5B%22OpenBadgeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Apre-authorized_code%22%3A%7B%22pre-authorized_code%22%3A%22UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X%22%7D%7D%7D',
);
expect(client.version).toEqual(OpenId4VCIVersion.VER_1_0_11);
expect(client.baseUrl).toEqual('http://launchpad.vii.electron.mattrlabs.io');
expect(client.scheme).toEqual('http');
expect(client.credential_offer.credential_issuer).toEqual('http://launchpad.vii.electron.mattrlabs.io');
expect(client.preAuthorizedCode).toEqual('UPZohaodPlLBnGsqB02n2tIupCIg8nKRRUEUHWA665X');
})
});
26 changes: 17 additions & 9 deletions packages/common/lib/functions/Encoding.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { BAD_PARAMS, DecodeURIAsJsonOpts, EncodeJsonAsURIOpts, JsonURIMode, OpenId4VCIVersion, SearchValue } from '../types';
import {
BAD_PARAMS,
DecodeURIAsJsonOpts,
EncodeJsonAsURIOpts,
JsonURIMode,
OpenId4VCIVersion,
SearchValue
} from '../types'

/**
* @function encodeJsonAsURI encodes a Json object into a URI
* @param json object
* @param opts:
* @type {(json: {[s:string]: never} | ArrayLike<never> | string | object, opts?: EncodeJsonAsURIOpts)} encodes a Json object into a URI
* @param { {[s:string]: never} | ArrayLike<never> | string | object } json
* @param {EncodeJsonAsURIOpts} [opts] Option to encode json as uri
* - urlTypeProperties: a list of properties of which the value is a URL
* - arrayTypeProperties: a list of properties which are an array
*/
Expand Down Expand Up @@ -81,11 +88,12 @@ export function convertJsonToURI(
}

/**
* @function decodeUriAsJson decodes a URI into a Json object
* @param uri string
* @param opts:
* @type {(uri: string, opts?: DecodeURIAsJsonOpts): unknown} convertURIToJsonObject converts an URI into a Json object decoding its properties
* @param {string} uri
* @param {DecodeURIAsJsonOpts} [opts]
* - requiredProperties: the required properties
* - arrayTypeProperties: properties that can show up more that once
* @returns JSON object
*/
export function convertURIToJsonObject(uri: string, opts?: DecodeURIAsJsonOpts): unknown {
if (!uri || (opts?.requiredProperties && !opts.requiredProperties?.every((p) => uri.includes(p)))) {
Expand Down Expand Up @@ -128,8 +136,8 @@ function decodeJsonProperties(parts: string[] | string[][]): unknown {

/**
* @function get URI Components as Array
* @param uri string
* @param arrayTypes array of string containing array like keys
* @param {string} uri uri
* @param {string[]} [arrayTypes] array of string containing array like keys
*/
function getURIComponentsAsArray(uri: string, arrayTypes?: string[]): string[] | string[][] {
const parts = uri.includes('?') ? uri.split('?')[1] : uri.includes('://') ? uri.split('://')[1] : uri;
Expand Down
56 changes: 56 additions & 0 deletions packages/issuer/lib/__tests__/CredentialOfferUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,60 @@ describe('CredentialOfferUtils should', () => {
'openid-credential-offer://?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Fcredential-issuer.example.com%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22authorization_code%22%3A%7B%22issuer_state%22%3A%22eyJhbGciOiJSU0Et...FYUaBy%22%7D%7D%7D',
)
})

it('create an https link 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
// https://credential-issuer.example.com?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: 'jwt_vc_json',
types: ['VerifiableCredential', 'UniversityDegreeCredential'],
},
],
grants: {
authorization_code: {
issuer_state: 'eyJhbGciOiJSU0Et...FYUaBy',
},
},
} as CredentialOfferPayloadV1_0_11

expect(createCredentialOfferURI({
credential_issuer: credentialOffer.credential_issuer,
credential_endpoint: 'test_issuer',
issuer: 'test_issuer',
credentials_supported: []
}, { credentialOffer, state: 'eyJhbGciOiJSU0Et...FYUaBy', scheme: 'https' })).toEqual(
`${credentialOffer.credential_issuer}?credential_offer=%7B%22credential_issuer%22%3A%22https%3A%2F%2Fcredential-issuer.example.com%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22authorization_code%22%3A%7B%22issuer_state%22%3A%22eyJhbGciOiJSU0Et...FYUaBy%22%7D%7D%7D`,
)
})

it('create an http link 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
// http://credential-issuer.example.com?credential_offer=%7B%22credential_issuer%22:%22http://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: 'http://credential-issuer.example.com',
credentials: [
{
format: 'jwt_vc_json',
types: ['VerifiableCredential', 'UniversityDegreeCredential'],
},
],
grants: {
authorization_code: {
issuer_state: 'eyJhbGciOiJSU0Et...FYUaBy',
},
},
} as CredentialOfferPayloadV1_0_11

expect(createCredentialOfferURI({
credential_issuer: credentialOffer.credential_issuer,
credential_endpoint: 'test_issuer',
issuer: 'test_issuer',
credentials_supported: []
}, { credentialOffer, state: 'eyJhbGciOiJSU0Et...FYUaBy', scheme: 'http' })).toEqual(
`${credentialOffer.credential_issuer}?credential_offer=%7B%22credential_issuer%22%3A%22http%3A%2F%2Fcredential-issuer.example.com%22%2C%22credentials%22%3A%5B%7B%22format%22%3A%22jwt_vc_json%22%2C%22types%22%3A%5B%22VerifiableCredential%22%2C%22UniversityDegreeCredential%22%5D%7D%5D%2C%22grants%22%3A%7B%22authorization_code%22%3A%7B%22issuer_state%22%3A%22eyJhbGciOiJSU0Et...FYUaBy%22%7D%7D%7D`,
)
})
})

0 comments on commit 6a05e4d

Please sign in to comment.