Skip to content

Commit 7817db7

Browse files
committed
chore: test fixes
1 parent 9478b65 commit 7817db7

7 files changed

Lines changed: 123 additions & 124 deletions

File tree

packages/client/lib/CredentialOfferClient.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import {
1212
PRE_AUTH_GRANT_LITERAL,
1313
toUniformCredentialOfferRequest,
1414
} from '@sphereon/oid4vci-common';
15-
import { fetch } from 'cross-fetch';
1615
import Debug from 'debug';
16+
import { constructBaseResponse, handleCredentialOfferUri } from './functions';
1717

18-
import { LOG } from './types'
19-
import { fetch } from 'cross-fetch'
20-
import { constructBaseResponse, handleCredentialOfferUri, isUrlEncoded } from './functions'
18+
import { LOG } from './types';
2119

2220
const debug = Debug('sphereon:oid4vci:offer');
2321

@@ -45,7 +43,7 @@ export class CredentialOfferClient {
4543
};
4644
} else {
4745
if (uri.includes('credential_offer_uri')) {
48-
credentialOffer = await handleCredentialOfferUri(uri) as CredentialOfferV1_0_11 | CredentialOfferV1_0_13
46+
credentialOffer = (await handleCredentialOfferUri(uri)) as CredentialOfferV1_0_11 | CredentialOfferV1_0_13;
4947
} else {
5048
credentialOffer = convertURIToJsonObject(uri, {
5149
// It must have the '=' sign after credential_offer otherwise the uri will get split at openid_credential_offer
@@ -54,22 +52,22 @@ export class CredentialOfferClient {
5452
}) as CredentialOfferV1_0_11 | CredentialOfferV1_0_13;
5553
}
5654
if (credentialOffer?.credential_offer_uri === undefined && !credentialOffer?.credential_offer) {
57-
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri) // cannot be reached since convertURIToJsonObject will check the params
55+
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri); // cannot be reached since convertURIToJsonObject will check the params
5856
}
5957
}
6058

6159
const request = await toUniformCredentialOfferRequest(credentialOffer, {
6260
...opts,
63-
version
64-
})
61+
version,
62+
});
6563

6664
return {
6765
...constructBaseResponse(request, scheme, baseUrl),
6866
userPinRequired:
6967
request.credential_offer?.grants?.[PRE_AUTH_GRANT_LITERAL]?.user_pin_required ??
7068
!!request.credential_offer?.grants?.[PRE_AUTH_GRANT_LITERAL]?.tx_code ??
71-
false
72-
}
69+
false,
70+
};
7371
}
7472

7573
public static toURI(

packages/client/lib/CredentialOfferClientV1_0_13.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@ import {
77
determineSpecVersionFromURI,
88
OpenId4VCIVersion,
99
PRE_AUTH_GRANT_LITERAL,
10-
toUniformCredentialOfferRequest
11-
} from '@sphereon/oid4vci-common'
12-
import Debug from 'debug'
13-
import { constructBaseResponse, handleCredentialOfferUri } from './functions'
10+
toUniformCredentialOfferRequest,
11+
} from '@sphereon/oid4vci-common';
12+
import Debug from 'debug';
13+
import { constructBaseResponse, handleCredentialOfferUri } from './functions';
1414

1515
const debug = Debug('sphereon:oid4vci:offer');
1616

1717
export class CredentialOfferClientV1_0_13 {
1818
public static async fromURI(uri: string, opts?: { resolve?: boolean }): Promise<CredentialOfferRequestWithBaseUrl> {
19-
debug(`Credential Offer URI: ${uri}`)
19+
debug(`Credential Offer URI: ${uri}`);
2020
if (!uri.includes('?') || !uri.includes('://')) {
21-
debug(`Invalid Credential Offer URI: ${uri}`)
22-
throw Error(`Invalid Credential Offer Request`)
21+
debug(`Invalid Credential Offer URI: ${uri}`);
22+
throw Error(`Invalid Credential Offer Request`);
2323
}
24-
const scheme = uri.split('://')[0]
25-
const baseUrl = uri.split('?')[0]
26-
const version = determineSpecVersionFromURI(uri)
27-
let credentialOffer: CredentialOffer
28-
if (uri.includes('credential_offer_uri')) { // FIXME deduplicate
29-
credentialOffer = await handleCredentialOfferUri(uri) as CredentialOfferV1_0_13
24+
const scheme = uri.split('://')[0];
25+
const baseUrl = uri.split('?')[0];
26+
const version = determineSpecVersionFromURI(uri);
27+
let credentialOffer: CredentialOffer;
28+
if (uri.includes('credential_offer_uri')) {
29+
// FIXME deduplicate
30+
credentialOffer = (await handleCredentialOfferUri(uri)) as CredentialOfferV1_0_13;
3031
} else {
3132
credentialOffer = convertURIToJsonObject(uri, {
3233
// It must have the '=' sign after credential_offer otherwise the uri will get split at openid_credential_offer
@@ -37,18 +38,18 @@ export class CredentialOfferClientV1_0_13 {
3738
}) as CredentialOfferV1_0_13;
3839
}
3940
if (credentialOffer?.credential_offer_uri === undefined && !credentialOffer?.credential_offer) {
40-
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri) // cannot be reached since convertURIToJsonObject will check the params
41+
throw Error('Either a credential_offer or credential_offer_uri should be present in ' + uri); // cannot be reached since convertURIToJsonObject will check the params
4142
}
4243

4344
const request = await toUniformCredentialOfferRequest(credentialOffer, {
4445
...opts,
45-
version
46-
})
46+
version,
47+
});
4748

4849
return {
4950
...constructBaseResponse(request, scheme, baseUrl),
50-
userPinRequired: !!request.credential_offer?.grants?.[PRE_AUTH_GRANT_LITERAL]?.tx_code ?? false
51-
}
51+
userPinRequired: !!request.credential_offer?.grants?.[PRE_AUTH_GRANT_LITERAL]?.tx_code ?? false,
52+
};
5253
}
5354

5455
public static toURI(

packages/client/lib/__tests__/CredentialRequestClient.spec.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -344,59 +344,53 @@ describe('Credential Request Client with different issuers ', () => {
344344

345345
describe('Credential Offer Client error handling', () => {
346346
beforeEach(() => {
347-
nock.cleanAll()
348-
})
347+
nock.cleanAll();
348+
});
349349

350350
afterEach(() => {
351-
nock.cleanAll()
352-
})
351+
nock.cleanAll();
352+
});
353353

354354
it('should handle non-200 response from credential offer URI endpoint', async () => {
355-
const IRR_URI = 'openid-credential-offer://?credential_offer_uri=https%3A%2F%2Ftest.example.com%2Foffer'
355+
const IRR_URI = 'openid-credential-offer://?credential_offer_uri=https%3A%2F%2Ftest.example.com%2Foffer';
356356

357-
nock('https://test.example.com')
358-
.get('/offer')
359-
.reply(404, 'Not Found')
357+
nock('https://test.example.com').get('/offer').reply(404, 'Not Found');
360358

361359
await expect(CredentialOfferClient.fromURI(IRR_URI)).rejects.toMatch(
362-
/the credential offer URI endpoint call was not successful. http code 404 - reason Not Found/
363-
)
364-
})
360+
/the credential offer URI endpoint call was not successful. http code 404 - reason Not Found/,
361+
);
362+
});
365363

366364
it('should handle invalid content type from credential offer URI endpoint', async () => {
367-
const IRR_URI = 'openid-credential-offer://?credential_offer_uri=https%3A%2F%2Ftest.example.com%2Foffer'
365+
const IRR_URI = 'openid-credential-offer://?credential_offer_uri=https%3A%2F%2Ftest.example.com%2Foffer';
368366

369-
nock('https://test.example.com')
370-
.get('/offer')
371-
.reply(200, 'plain text response', { 'Content-Type': 'text/plain' })
367+
nock('https://test.example.com').get('/offer').reply(200, 'plain text response', { 'Content-Type': 'text/plain' });
372368

373369
await expect(CredentialOfferClient.fromURI(IRR_URI)).rejects.toMatch(
374-
'the credential offer URI endpoint did not return content type application/json'
375-
)
376-
})
370+
'the credential offer URI endpoint did not return content type application/json',
371+
);
372+
});
377373

378374
it('should handle missing required credential offer properties', async () => {
379-
const IRR_URI = 'openid-credential-offer://?invalid_param=test'
375+
const IRR_URI = 'openid-credential-offer://?invalid_param=test';
380376

381-
await expect(CredentialOfferClient.fromURI(IRR_URI)).rejects.toThrow('Wrong parameters provided')
382-
})
377+
await expect(CredentialOfferClient.fromURI(IRR_URI)).rejects.toThrow('Wrong parameters provided');
378+
});
383379

384380
it('should handle credential offer URI with credential_offer param', async () => {
385-
const IRR_URI = 'openid-credential-offer://?credential_offer=%7B%22test%22%3A%22value%22%7D'
381+
const IRR_URI = 'openid-credential-offer://?credential_offer=%7B%22test%22%3A%22value%22%7D';
386382

387-
const client = await CredentialOfferClient.fromURI(IRR_URI)
388-
expect(client.credential_offer).toBeDefined()
389-
})
383+
const client = await CredentialOfferClient.fromURI(IRR_URI);
384+
expect(client.credential_offer).toBeDefined();
385+
});
390386

391387
it('should handle URL encoded credential offer URI properly', async () => {
392-
const encodedUri = 'https%3A%2F%2Ftest.example.com%2Foffer'
393-
const IRR_URI = `openid-credential-offer://?credential_offer_uri=${encodedUri}`
388+
const encodedUri = 'https%3A%2F%2Ftest.example.com%2Foffer';
389+
const IRR_URI = `openid-credential-offer://?credential_offer_uri=${encodedUri}`;
394390

395-
nock('https://test.example.com')
396-
.get('/offer')
397-
.reply(200, { test: 'value' }, { 'Content-Type': 'application/json' })
391+
nock('https://test.example.com').get('/offer').reply(200, { test: 'value' }, { 'Content-Type': 'application/json' });
398392

399-
const client = await CredentialOfferClient.fromURI(IRR_URI)
400-
expect(client.credential_offer).toBeDefined()
401-
})
402-
})
393+
const client = await CredentialOfferClient.fromURI(IRR_URI);
394+
expect(client.credential_offer).toBeDefined();
395+
});
396+
});

packages/client/lib/functions/CredentialOfferCommons.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ import {
44
getURIComponentsAsArray,
55
PRE_AUTH_CODE_LITERAL,
66
PRE_AUTH_GRANT_LITERAL,
7-
UniformCredentialOfferRequest
8-
} from '@sphereon/oid4vci-common'
9-
import { fetch } from 'cross-fetch'
7+
UniformCredentialOfferRequest,
8+
} from '@sphereon/oid4vci-common';
9+
import { fetch } from 'cross-fetch';
1010

1111
export function isUriEncoded(str: string): boolean {
12-
const pattern = /%[0-9A-F]{2}/i
13-
return pattern.test(str)
12+
const pattern = /%[0-9A-F]{2}/i;
13+
return pattern.test(str);
1414
}
1515

1616
export async function handleCredentialOfferUri(uri: string) {
17-
const uriObj = getURIComponentsAsArray(uri) as unknown as Record<string, string>
18-
const credentialOfferUri = decodeURIComponent(uriObj['credential_offer_uri'])
19-
const decodedUri = isUriEncoded(credentialOfferUri) ? decodeURIComponent(credentialOfferUri) : credentialOfferUri
20-
const response = await fetch(decodedUri)
17+
const uriObj = getURIComponentsAsArray(uri) as unknown as Record<string, string>;
18+
const credentialOfferUri = decodeURIComponent(uriObj['credential_offer_uri']);
19+
const decodedUri = isUriEncoded(credentialOfferUri) ? decodeURIComponent(credentialOfferUri) : credentialOfferUri;
20+
const response = await fetch(decodedUri);
2121

2222
if (!(response && response.status >= 200 && response.status < 400)) {
23-
return Promise.reject(`the credential offer URI endpoint call was not successful. http code ${response.status} - reason ${response.statusText}`)
23+
return Promise.reject(`the credential offer URI endpoint call was not successful. http code ${response.status} - reason ${response.statusText}`);
2424
}
2525

2626
if (response.headers.get('Content-Type')?.startsWith('application/json') === false) {
27-
return Promise.reject('the credential offer URI endpoint did not return content type application/json')
27+
return Promise.reject('the credential offer URI endpoint did not return content type application/json');
2828
}
2929

3030
return {
31-
credential_offer: decodeJsonProperties(await response.json())
32-
}
31+
credential_offer: decodeJsonProperties(await response.json()),
32+
};
3333
}
3434

3535
export function constructBaseResponse(request: UniformCredentialOfferRequest, scheme: string, baseUrl: string) {
36-
const clientId = getClientIdFromCredentialOfferPayload(request.credential_offer)
37-
const grants = request.credential_offer?.grants
36+
const clientId = getClientIdFromCredentialOfferPayload(request.credential_offer);
37+
const grants = request.credential_offer?.grants;
3838

3939
return {
4040
scheme,
@@ -43,10 +43,10 @@ export function constructBaseResponse(request: UniformCredentialOfferRequest, sc
4343
...request,
4444
...(grants?.authorization_code?.issuer_state && { issuerState: grants.authorization_code.issuer_state }),
4545
...(grants?.[PRE_AUTH_GRANT_LITERAL]?.[PRE_AUTH_CODE_LITERAL] && {
46-
preAuthorizedCode: grants[PRE_AUTH_GRANT_LITERAL][PRE_AUTH_CODE_LITERAL]
46+
preAuthorizedCode: grants[PRE_AUTH_GRANT_LITERAL][PRE_AUTH_CODE_LITERAL],
4747
}),
4848
...(request.credential_offer?.grants?.[PRE_AUTH_GRANT_LITERAL]?.tx_code && {
49-
txCode: request.credential_offer.grants[PRE_AUTH_GRANT_LITERAL].tx_code
50-
})
51-
}
52-
}
49+
txCode: request.credential_offer.grants[PRE_AUTH_GRANT_LITERAL].tx_code,
50+
}),
51+
};
52+
}

0 commit comments

Comments
 (0)