Skip to content

Commit

Permalink
fix: test added
Browse files Browse the repository at this point in the history
  • Loading branch information
sksadjad committed Jun 25, 2024
1 parent 19813c8 commit 19b0704
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { KeyObject } from 'crypto'

import {
CredentialRequestClient,
CredentialRequestClientBuilder,
CredentialRequestClientBuilderV1_0_13,
ProofOfPossessionBuilder,
} from '@sphereon/oid4vci-client'
import {
Expand Down Expand Up @@ -224,7 +222,7 @@ describe('issuerCallback', () => {
})

it('Should pass requesting a verifiable credential using the client', async () => {
const credReqClient = ((await CredentialRequestClientBuilder.fromURI({ uri: INITIATION_TEST_URI })) as CredentialRequestClientBuilderV1_0_13)
const credReqClient = (await CredentialRequestClientBuilder.fromURI({ uri: INITIATION_TEST_URI }))
.withCredentialEndpoint('https://oidc4vci.demo.spruceid.com/credential')
.withCredentialEndpointFromMetadata({
credential_configurations_supported: { VeriCred: { format: 'jwt_vc_json' } },
Expand Down Expand Up @@ -257,7 +255,7 @@ describe('issuerCallback', () => {
.withKid(kid)
.build()

const credentialRequestClient = new CredentialRequestClient(credReqClient)
const credentialRequestClient = credReqClient.build()
const credentialRequest: CredentialRequest = await credentialRequestClient.createCredentialRequest({
credentialIdentifier: 'VerifiableCredential',
// format: 'jwt_vc_json',
Expand Down
52 changes: 26 additions & 26 deletions packages/client/lib/CredentialRequestClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,34 @@ import {
ExperimentalSubjectIssuance,
OID4VCICredentialFormat,
OpenId4VCIVersion,
UniformCredentialOfferRequest,
} from '@sphereon/oid4vci-common';
import { CredentialFormat } from '@sphereon/ssi-types';
UniformCredentialOfferRequest
} from '@sphereon/oid4vci-common'
import { CredentialFormat } from '@sphereon/ssi-types'

import { CredentialOfferClient } from './CredentialOfferClient';
import { CredentialRequestClient } from './CredentialRequestClient';
import { CredentialRequestClientBuilderV1_0_11 } from './CredentialRequestClientBuilderV1_0_11';
import { CredentialRequestClientBuilderV1_0_13 } from './CredentialRequestClientBuilderV1_0_13';
import { CredentialRequestClientV1_0_11 } from './CredentialRequestClientV1_0_11';

type CredentialRequestClientBuilderVersionSpecific = CredentialRequestClientBuilderV1_0_11 | CredentialRequestClientBuilderV1_0_13;

function isV1_0_13(builder: CredentialRequestClientBuilderVersionSpecific): builder is CredentialRequestClientBuilderV1_0_13 {
return (builder as CredentialRequestClientBuilderV1_0_13).withCredentialIdentifier !== undefined;
}

export class CredentialRequestClientBuilder {
private _builder: CredentialRequestClientBuilderVersionSpecific;

private constructor(builder: CredentialRequestClientBuilderVersionSpecific) {
this._builder = builder;
}

private isV1_0_13(builder: CredentialRequestClientBuilderVersionSpecific): builder is CredentialRequestClientBuilderV1_0_13 {
return (builder as CredentialRequestClientBuilderV1_0_13).withCredentialIdentifier !== undefined;
}

public static fromCredentialIssuer({
credentialIssuer,
metadata,
version,
credentialIdentifier,
credentialTypes,
}: {
credentialIssuer,
metadata,
version,
credentialIdentifier,
credentialTypes,
}: {
credentialIssuer: string;
metadata?: EndpointMetadata;
version?: OpenId4VCIVersion;
Expand Down Expand Up @@ -101,9 +99,9 @@ export class CredentialRequestClientBuilder {
}

public static fromCredentialOffer({
credentialOffer,
metadata,
}: {
credentialOffer,
metadata,
}: {
credentialOffer: CredentialOfferRequestWithBaseUrl;
metadata?: EndpointMetadata;
}): CredentialRequestClientBuilder {
Expand All @@ -129,8 +127,12 @@ export class CredentialRequestClientBuilder {
return this._builder.version;
}

public withCredentialEndpointFromMetadata(metadata: CredentialIssuerMetadata): this {
this._builder.credentialEndpoint = metadata.credential_endpoint;
public withCredentialEndpointFromMetadata(metadata: CredentialIssuerMetadata | CredentialIssuerMetadataV1_0_13): this {
if (isV1_0_13(this._builder)) {
this._builder.withCredentialEndpointFromMetadata(metadata as CredentialIssuerMetadataV1_0_13)
} else {
this._builder.withCredentialEndpointFromMetadata(metadata as CredentialIssuerMetadata)
}
return this;
}

Expand All @@ -140,12 +142,10 @@ export class CredentialRequestClientBuilder {
}

public withDeferredCredentialEndpointFromMetadata(metadata: CredentialIssuerMetadata | CredentialIssuerMetadataV1_0_13): this {
if (this.isV1_0_13(this._builder)) {
(this._builder as CredentialRequestClientBuilderV1_0_13).withDeferredCredentialEndpointFromMetadata(
metadata as CredentialIssuerMetadataV1_0_13,
);
if (isV1_0_13(this._builder)) {
this._builder.withDeferredCredentialEndpointFromMetadata(metadata as CredentialIssuerMetadataV1_0_13);
} else {
this._builder.withDeferredCredentialEndpointFromMetadata(metadata);
this._builder.withDeferredCredentialEndpointFromMetadata(metadata as CredentialIssuerMetadata);
}
return this;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ export class CredentialRequestClientBuilder {
return this;
}

public build(): CredentialRequestClient | CredentialRequestClientV1_0_11 {
public build() {
return this._builder.build();
}
}
75 changes: 71 additions & 4 deletions packages/client/lib/__tests__/IT.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
AccessTokenResponse,
Alg,
Alg, CredentialOfferPayloadV1_0_13,
CredentialOfferRequestWithBaseUrl,
Jwt,
OpenId4VCIVersion,
ProofOfPossession,
WellKnownEndpoints,
} from '@sphereon/oid4vci-common';
WellKnownEndpoints
} from '@sphereon/oid4vci-common'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import nock from 'nock';
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('OID4VCI-Client should', () => {
}

it(
'succeed with a full flow without the client',
'succeed with a full flow without the client v1_0_11',
async () => {
/* Convert the URI into an object */
const credentialOffer: CredentialOfferRequestWithBaseUrl = await CredentialOfferClient.fromURI(INITIATE_QR_V1_0_08);
Expand Down Expand Up @@ -284,6 +284,73 @@ describe('OID4VCI-Client should', () => {
UNIT_TEST_TIMEOUT,
);

it(
'succeed with a full flow without the client v1_0_13',
async () => {
/* Convert the URI into an object */
const credentialOffer: CredentialOfferRequestWithBaseUrl = await CredentialOfferClient.fromURI(INITIATE_QR_V1_0_13);
const preAuthorizedCode = 'oaKazRN8I0IbtZ0C7JuMn5';
expect(credentialOffer.baseUrl).toEqual('openid-credential-offer://');
expect((credentialOffer.credential_offer as CredentialOfferPayloadV1_0_13).credential_configuration_ids).toEqual(['OpenBadgeCredentialUrl']);
expect(credentialOffer.original_credential_offer.grants).toEqual({
'urn:ietf:params:oauth:grant-type:pre-authorized_code': {
'pre-authorized_code': preAuthorizedCode,
tx_code: {
input_mode: 'text',
description: 'Please enter the serial number of your physical drivers license',
length: preAuthorizedCode.length,
},
},
});

nock(ISSUER_URL)
.post(/token.*/)
.reply(200, JSON.stringify(mockedAccessTokenResponse));

/* The actual access token calls */
const accessTokenClient: AccessTokenClient = new AccessTokenClient();
const accessTokenResponse = await accessTokenClient.acquireAccessToken({ credentialOffer: credentialOffer, pin: '1234' });
expect(accessTokenResponse.successBody).toEqual(mockedAccessTokenResponse);
// Get the credential
nock(ISSUER_URL)
.post(/credential/)
.reply(200, {
format: 'jwt-vc',
credential: mockedVC,
});
const credReqClient = CredentialRequestClientBuilder.fromCredentialOffer({ credentialOffer: credentialOffer })
.withFormat('jwt_vc')

.withTokenFromResponse(accessTokenResponse.successBody!)
.build();

//TS2322: Type '(args: ProofOfPossessionCallbackArgs) => Promise<string>'
// is not assignable to type 'ProofOfPossessionCallback'.
// Types of parameters 'args' and 'args' are incompatible.
// Property 'kid' is missing in type '{ header: unknown; payload: unknown; }' but required in type 'ProofOfPossessionCallbackArgs'.
const proof: ProofOfPossession = await ProofOfPossessionBuilder.fromJwt({
jwt: jwtDid,
callbacks: {
signCallback: proofOfPossessionCallbackFunction,
},
version: OpenId4VCIVersion.VER_1_0_11,
})
.withEndpointMetadata({
issuer: 'https://issuer.research.identiproof.io',
credential_endpoint: 'https://issuer.research.identiproof.io/credential',
token_endpoint: 'https://issuer.research.identiproof.io/token',
})
.withKid('did:example:ebfeb1f712ebc6f1c276e12ec21/keys/1')
.build();
const credResponse = await credReqClient.acquireCredentialsUsingProof({
proofInput: proof,
credentialTypes: credentialOffer.original_credential_offer.credential_configuration_ids[0],
});
expect(credResponse.successBody?.credential).toEqual(mockedVC);
},
UNIT_TEST_TIMEOUT,
);

it(
'succeed with a full flow without the client and without did',
async () => {
Expand Down

0 comments on commit 19b0704

Please sign in to comment.