@sphereon/oid4vci-client 0.10.3 → 0.10.4-unstable.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/AccessTokenClient.d.ts +5 -5
  2. package/dist/AccessTokenClient.d.ts.map +1 -1
  3. package/dist/AccessTokenClient.js +45 -25
  4. package/dist/AccessTokenClient.js.map +1 -1
  5. package/dist/AccessTokenClientV1_0_11.d.ts +29 -0
  6. package/dist/AccessTokenClientV1_0_11.d.ts.map +1 -0
  7. package/dist/AccessTokenClientV1_0_11.js +212 -0
  8. package/dist/AccessTokenClientV1_0_11.js.map +1 -0
  9. package/dist/AuthorizationCodeClient.d.ts +5 -5
  10. package/dist/AuthorizationCodeClient.d.ts.map +1 -1
  11. package/dist/AuthorizationCodeClient.js +19 -8
  12. package/dist/AuthorizationCodeClient.js.map +1 -1
  13. package/dist/AuthorizationCodeClientV1_0_11.d.ts +9 -0
  14. package/dist/AuthorizationCodeClientV1_0_11.d.ts.map +1 -0
  15. package/dist/AuthorizationCodeClientV1_0_11.js +132 -0
  16. package/dist/AuthorizationCodeClientV1_0_11.js.map +1 -0
  17. package/dist/CredentialOfferClient.d.ts.map +1 -1
  18. package/dist/CredentialOfferClient.js +14 -25
  19. package/dist/CredentialOfferClient.js.map +1 -1
  20. package/dist/CredentialOfferClientV1_0_11.d.ts +10 -0
  21. package/dist/CredentialOfferClientV1_0_11.d.ts.map +1 -0
  22. package/dist/CredentialOfferClientV1_0_11.js +103 -0
  23. package/dist/CredentialOfferClientV1_0_11.js.map +1 -0
  24. package/dist/CredentialRequestClient.d.ts +1 -1
  25. package/dist/CredentialRequestClient.d.ts.map +1 -1
  26. package/dist/CredentialRequestClient.js +11 -8
  27. package/dist/CredentialRequestClient.js.map +1 -1
  28. package/dist/CredentialRequestClientBuilder.d.ts +3 -3
  29. package/dist/CredentialRequestClientBuilder.d.ts.map +1 -1
  30. package/dist/CredentialRequestClientBuilder.js +4 -4
  31. package/dist/CredentialRequestClientBuilder.js.map +1 -1
  32. package/dist/CredentialRequestClientBuilderV1_0_11.d.ts +46 -0
  33. package/dist/CredentialRequestClientBuilderV1_0_11.d.ts.map +1 -0
  34. package/dist/CredentialRequestClientBuilderV1_0_11.js +117 -0
  35. package/dist/CredentialRequestClientBuilderV1_0_11.js.map +1 -0
  36. package/dist/CredentialRequestClientV1_0_11.d.ts +44 -0
  37. package/dist/CredentialRequestClientV1_0_11.d.ts.map +1 -0
  38. package/dist/CredentialRequestClientV1_0_11.js +151 -0
  39. package/dist/CredentialRequestClientV1_0_11.js.map +1 -0
  40. package/dist/MetadataClient.d.ts +5 -15
  41. package/dist/MetadataClient.d.ts.map +1 -1
  42. package/dist/MetadataClient.js +13 -33
  43. package/dist/MetadataClient.js.map +1 -1
  44. package/dist/MetadataClientV1_0_11.d.ts +31 -0
  45. package/dist/MetadataClientV1_0_11.d.ts.map +1 -0
  46. package/dist/MetadataClientV1_0_11.js +182 -0
  47. package/dist/MetadataClientV1_0_11.js.map +1 -0
  48. package/dist/OpenID4VCIClient.d.ts +5 -17
  49. package/dist/OpenID4VCIClient.d.ts.map +1 -1
  50. package/dist/OpenID4VCIClient.js +19 -90
  51. package/dist/OpenID4VCIClient.js.map +1 -1
  52. package/dist/OpenID4VCIClientV1_0_11.d.ts +107 -0
  53. package/dist/OpenID4VCIClientV1_0_11.d.ts.map +1 -0
  54. package/dist/OpenID4VCIClientV1_0_11.js +462 -0
  55. package/dist/OpenID4VCIClientV1_0_11.js.map +1 -0
  56. package/dist/ProofOfPossessionBuilder.js +1 -1
  57. package/dist/ProofOfPossessionBuilder.js.map +1 -1
  58. package/dist/functions/OpenIDUtils.d.ts +12 -0
  59. package/dist/functions/OpenIDUtils.d.ts.map +1 -0
  60. package/dist/functions/OpenIDUtils.js +37 -0
  61. package/dist/functions/OpenIDUtils.js.map +1 -0
  62. package/dist/functions/ProofUtil.d.ts.map +1 -1
  63. package/dist/index.d.ts +8 -1
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +8 -1
  66. package/dist/index.js.map +1 -1
  67. package/lib/AccessTokenClient.ts +277 -245
  68. package/lib/AccessTokenClientV1_0_11.ts +255 -0
  69. package/lib/AuthorizationCodeClient.ts +183 -163
  70. package/lib/AuthorizationCodeClientV1_0_11.ts +167 -0
  71. package/lib/CredentialOfferClient.ts +99 -111
  72. package/lib/CredentialOfferClientV1_0_11.ts +112 -0
  73. package/lib/CredentialRequestClient.ts +212 -208
  74. package/lib/CredentialRequestClientBuilder.ts +156 -155
  75. package/lib/CredentialRequestClientBuilderV1_0_11.ts +156 -0
  76. package/lib/CredentialRequestClientV1_0_11.ts +190 -0
  77. package/lib/MetadataClient.ts +186 -208
  78. package/lib/MetadataClientV1_0_11.ts +186 -0
  79. package/lib/OpenID4VCIClient.ts +547 -629
  80. package/lib/OpenID4VCIClientV1_0_11.ts +644 -0
  81. package/lib/ProofOfPossessionBuilder.ts +204 -204
  82. package/lib/__tests__/AccessTokenClient.spec.ts +239 -211
  83. package/lib/__tests__/CredentialRequestClient.spec.ts +328 -311
  84. package/lib/__tests__/CredentialRequestClientBuilder.spec.ts +131 -131
  85. package/lib/__tests__/CredentialRequestClientV1_0_11.spec.ts +316 -0
  86. package/lib/__tests__/EBSIE2E.spec.test.ts +145 -145
  87. package/lib/__tests__/IT.spec.ts +382 -171
  88. package/lib/__tests__/IssuanceInitiation.spec.ts +64 -61
  89. package/lib/__tests__/IssuanceInitiationV1_0_11.spec.ts +62 -0
  90. package/lib/__tests__/JsonURIConversions.spec.ts +146 -146
  91. package/lib/__tests__/MattrE2E.spec.test.ts +104 -104
  92. package/lib/__tests__/MetadataClient.spec.ts +324 -260
  93. package/lib/__tests__/MetadataMocks.ts +483 -444
  94. package/lib/__tests__/OpenID4VCIClient.spec.ts +202 -202
  95. package/lib/__tests__/{OpenID4VCIClientPAR.spec.ts → OpenID4VCIClientPARV1_0_11.spec.ts} +122 -122
  96. package/lib/__tests__/OpenID4VCIClientV1_0_11.spec.ts +202 -0
  97. package/lib/__tests__/ProofOfPossessionBuilder.spec.ts +110 -110
  98. package/lib/__tests__/SdJwt.spec.ts +170 -163
  99. package/lib/__tests__/SphereonE2E.spec.test.ts +169 -169
  100. package/lib/__tests__/data/VciDataFixtures.ts +1382 -745
  101. package/lib/functions/AuthorizationUtil.ts +18 -18
  102. package/lib/functions/OpenIDUtils.ts +25 -0
  103. package/lib/functions/ProofUtil.ts +128 -128
  104. package/lib/index.ts +16 -9
  105. package/package.json +3 -3
@@ -1,245 +1,277 @@
1
- import {
2
- AccessTokenRequest,
3
- AccessTokenRequestOpts,
4
- AccessTokenResponse,
5
- assertedUniformCredentialOffer,
6
- AuthorizationServerOpts,
7
- AuthzFlowType,
8
- EndpointMetadata,
9
- getIssuerFromCredentialOfferPayload,
10
- GrantTypes,
11
- IssuerOpts,
12
- JsonURIMode,
13
- OpenIDResponse,
14
- PRE_AUTH_CODE_LITERAL,
15
- TokenErrorResponse,
16
- toUniformCredentialOfferRequest,
17
- UniformCredentialOfferPayload,
18
- } from '@sphereon/oid4vci-common';
19
- import { ObjectUtils } from '@sphereon/ssi-types';
20
- import Debug from 'debug';
21
-
22
- import { MetadataClient } from './MetadataClient';
23
- import { convertJsonToURI, formPost } from './functions';
24
-
25
- const debug = Debug('sphereon:oid4vci:token');
26
-
27
- export class AccessTokenClient {
28
- public async acquireAccessToken(opts: AccessTokenRequestOpts): Promise<OpenIDResponse<AccessTokenResponse>> {
29
- const { asOpts, pin, codeVerifier, code, redirectUri, metadata } = opts;
30
-
31
- const credentialOffer = opts.credentialOffer ? await assertedUniformCredentialOffer(opts.credentialOffer) : undefined;
32
- const isPinRequired = credentialOffer && this.isPinRequiredValue(credentialOffer.credential_offer);
33
- const issuer =
34
- opts.credentialIssuer ??
35
- (credentialOffer ? getIssuerFromCredentialOfferPayload(credentialOffer.credential_offer) : (metadata?.issuer as string));
36
- if (!issuer) {
37
- throw Error('Issuer required at this point');
38
- }
39
- const issuerOpts = {
40
- issuer,
41
- };
42
-
43
- return await this.acquireAccessTokenUsingRequest({
44
- accessTokenRequest: await this.createAccessTokenRequest({
45
- credentialOffer,
46
- asOpts,
47
- codeVerifier,
48
- code,
49
- redirectUri,
50
- pin,
51
- }),
52
- isPinRequired,
53
- metadata,
54
- asOpts,
55
- issuerOpts,
56
- });
57
- }
58
-
59
- public async acquireAccessTokenUsingRequest({
60
- accessTokenRequest,
61
- isPinRequired,
62
- metadata,
63
- asOpts,
64
- issuerOpts,
65
- }: {
66
- accessTokenRequest: AccessTokenRequest;
67
- isPinRequired?: boolean;
68
- metadata?: EndpointMetadata;
69
- asOpts?: AuthorizationServerOpts;
70
- issuerOpts?: IssuerOpts;
71
- }): Promise<OpenIDResponse<AccessTokenResponse>> {
72
- this.validate(accessTokenRequest, isPinRequired);
73
-
74
- const requestTokenURL = AccessTokenClient.determineTokenURL({
75
- asOpts,
76
- issuerOpts,
77
- metadata: metadata
78
- ? metadata
79
- : issuerOpts?.fetchMetadata
80
- ? await MetadataClient.retrieveAllMetadata(issuerOpts.issuer, { errorOnNotFound: false })
81
- : undefined,
82
- });
83
-
84
- return this.sendAuthCode(requestTokenURL, accessTokenRequest);
85
- }
86
-
87
- public async createAccessTokenRequest(opts: AccessTokenRequestOpts): Promise<AccessTokenRequest> {
88
- const { asOpts, pin, codeVerifier, code, redirectUri } = opts;
89
- const credentialOfferRequest = opts.credentialOffer ? await toUniformCredentialOfferRequest(opts.credentialOffer) : undefined;
90
- const request: Partial<AccessTokenRequest> = {};
91
-
92
- if (asOpts?.clientId) {
93
- request.client_id = asOpts.clientId;
94
- }
95
-
96
- if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
97
- this.assertNumericPin(this.isPinRequiredValue(credentialOfferRequest.credential_offer), pin);
98
- request.user_pin = pin;
99
-
100
- request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
101
- // we actually know it is there because of the isPreAuthCode call
102
- request[PRE_AUTH_CODE_LITERAL] =
103
- credentialOfferRequest?.credential_offer.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.[PRE_AUTH_CODE_LITERAL];
104
-
105
- return request as AccessTokenRequest;
106
- }
107
-
108
- if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
109
- request.grant_type = GrantTypes.AUTHORIZATION_CODE;
110
- request.code = code;
111
- request.redirect_uri = redirectUri;
112
-
113
- if (codeVerifier) {
114
- request.code_verifier = codeVerifier;
115
- }
116
-
117
- return request as AccessTokenRequest;
118
- }
119
-
120
- throw new Error('Credential offer request does not follow neither pre-authorized code nor authorization code flow requirements.');
121
- }
122
-
123
- private assertPreAuthorizedGrantType(grantType: GrantTypes): void {
124
- if (GrantTypes.PRE_AUTHORIZED_CODE !== grantType) {
125
- throw new Error("grant type must be 'urn:ietf:params:oauth:grant-type:pre-authorized_code'");
126
- }
127
- }
128
-
129
- private assertAuthorizationGrantType(grantType: GrantTypes): void {
130
- if (GrantTypes.AUTHORIZATION_CODE !== grantType) {
131
- throw new Error("grant type must be 'authorization_code'");
132
- }
133
- }
134
-
135
- private isPinRequiredValue(requestPayload: UniformCredentialOfferPayload): boolean {
136
- let isPinRequired = false;
137
- if (!requestPayload) {
138
- throw new Error(TokenErrorResponse.invalid_request);
139
- }
140
- const issuer = getIssuerFromCredentialOfferPayload(requestPayload);
141
- if (requestPayload.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']) {
142
- isPinRequired = requestPayload.grants['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.user_pin_required ?? false;
143
- }
144
- debug(`Pin required for issuer ${issuer}: ${isPinRequired}`);
145
- return isPinRequired;
146
- }
147
-
148
- private assertNumericPin(isPinRequired?: boolean, pin?: string): void {
149
- if (isPinRequired) {
150
- if (!pin || !/^\d{1,8}$/.test(pin)) {
151
- debug(`Pin is not 1 to 8 digits long`);
152
- throw new Error('A valid pin consisting of maximal 8 numeric characters must be present.');
153
- }
154
- } else if (pin) {
155
- debug(`Pin set, whilst not required`);
156
- throw new Error('Cannot set a pin, when the pin is not required.');
157
- }
158
- }
159
-
160
- private assertNonEmptyPreAuthorizedCode(accessTokenRequest: AccessTokenRequest): void {
161
- if (!accessTokenRequest[PRE_AUTH_CODE_LITERAL]) {
162
- debug(`No pre-authorized code present, whilst it is required`);
163
- throw new Error('Pre-authorization must be proven by presenting the pre-authorized code. Code must be present.');
164
- }
165
- }
166
-
167
- private assertNonEmptyCodeVerifier(accessTokenRequest: AccessTokenRequest): void {
168
- if (!accessTokenRequest.code_verifier) {
169
- debug('No code_verifier present, whilst it is required');
170
- throw new Error('Authorization flow requires the code_verifier to be present');
171
- }
172
- }
173
-
174
- private assertNonEmptyCode(accessTokenRequest: AccessTokenRequest): void {
175
- if (!accessTokenRequest.code) {
176
- debug('No code present, whilst it is required');
177
- throw new Error('Authorization flow requires the code to be present');
178
- }
179
- }
180
- private validate(accessTokenRequest: AccessTokenRequest, isPinRequired?: boolean): void {
181
- if (accessTokenRequest.grant_type === GrantTypes.PRE_AUTHORIZED_CODE) {
182
- this.assertPreAuthorizedGrantType(accessTokenRequest.grant_type);
183
- this.assertNonEmptyPreAuthorizedCode(accessTokenRequest);
184
- this.assertNumericPin(isPinRequired, accessTokenRequest.user_pin);
185
- } else if (accessTokenRequest.grant_type === GrantTypes.AUTHORIZATION_CODE) {
186
- this.assertAuthorizationGrantType(accessTokenRequest.grant_type);
187
- this.assertNonEmptyCodeVerifier(accessTokenRequest);
188
- this.assertNonEmptyCode(accessTokenRequest);
189
- } else {
190
- this.throwNotSupportedFlow();
191
- }
192
- }
193
-
194
- private async sendAuthCode(requestTokenURL: string, accessTokenRequest: AccessTokenRequest): Promise<OpenIDResponse<AccessTokenResponse>> {
195
- return await formPost(requestTokenURL, convertJsonToURI(accessTokenRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }));
196
- }
197
-
198
- public static determineTokenURL({
199
- asOpts,
200
- issuerOpts,
201
- metadata,
202
- }: {
203
- asOpts?: AuthorizationServerOpts;
204
- issuerOpts?: IssuerOpts;
205
- metadata?: EndpointMetadata;
206
- }): string {
207
- if (!asOpts && !metadata?.token_endpoint && !issuerOpts) {
208
- throw new Error('Cannot determine token URL if no issuer, metadata and no Authorization Server values are present');
209
- }
210
- let url;
211
- if (asOpts && asOpts.as) {
212
- url = this.creatTokenURLFromURL(asOpts.as, asOpts?.allowInsecureEndpoints, asOpts.tokenEndpoint);
213
- } else if (metadata?.token_endpoint) {
214
- url = metadata.token_endpoint;
215
- } else {
216
- if (!issuerOpts?.issuer) {
217
- throw Error('Either authorization server options, a token endpoint or issuer options are required at this point');
218
- }
219
- url = this.creatTokenURLFromURL(issuerOpts.issuer, asOpts?.allowInsecureEndpoints, issuerOpts.tokenEndpoint);
220
- }
221
-
222
- if (!url || !ObjectUtils.isString(url)) {
223
- throw new Error('No authorization server token URL present. Cannot acquire access token');
224
- }
225
- debug(`Token endpoint determined to be ${url}`);
226
- return url;
227
- }
228
-
229
- private static creatTokenURLFromURL(url: string, allowInsecureEndpoints?: boolean, tokenEndpoint?: string): string {
230
- if (allowInsecureEndpoints !== true && url.startsWith('http:')) {
231
- throw Error(
232
- `Unprotected token endpoints are not allowed ${url}. Use the 'allowInsecureEndpoints' param if you really need this for dev/testing!`,
233
- );
234
- }
235
- const hostname = url.replace(/https?:\/\//, '').replace(/\/$/, '');
236
- const endpoint = tokenEndpoint ? (tokenEndpoint.startsWith('/') ? tokenEndpoint : tokenEndpoint.substring(1)) : '/token';
237
- const scheme = url.split('://')[0];
238
- return `${scheme ? scheme + '://' : 'https://'}${hostname}${endpoint}`;
239
- }
240
-
241
- private throwNotSupportedFlow(): void {
242
- debug(`Only pre-authorized or authorization code flows supported.`);
243
- throw new Error('Only pre-authorized-code or authorization code flows are supported');
244
- }
245
- }
1
+ import {
2
+ AccessTokenRequest,
3
+ AccessTokenRequestOpts,
4
+ AccessTokenResponse,
5
+ assertedUniformCredentialOffer,
6
+ AuthorizationServerOpts,
7
+ AuthzFlowType,
8
+ CredentialOfferPayloadV1_0_13,
9
+ CredentialOfferV1_0_13,
10
+ determineSpecVersionFromOffer,
11
+ EndpointMetadata,
12
+ getIssuerFromCredentialOfferPayload,
13
+ GrantTypes,
14
+ IssuerOpts,
15
+ JsonURIMode,
16
+ OpenId4VCIVersion,
17
+ OpenIDResponse,
18
+ PRE_AUTH_CODE_LITERAL,
19
+ TokenErrorResponse,
20
+ toUniformCredentialOfferRequest,
21
+ TxCodeAndPinRequired,
22
+ UniformCredentialOfferPayload,
23
+ } from '@sphereon/oid4vci-common';
24
+ import { ObjectUtils } from '@sphereon/ssi-types';
25
+ import Debug from 'debug';
26
+
27
+ import { MetadataClient } from './MetadataClient';
28
+ import { convertJsonToURI, formPost } from './functions';
29
+
30
+ const debug = Debug('sphereon:oid4vci:token');
31
+
32
+ export class AccessTokenClient {
33
+ public async acquireAccessToken(opts: AccessTokenRequestOpts): Promise<OpenIDResponse<AccessTokenResponse>> {
34
+ const { asOpts, pin, codeVerifier, code, redirectUri, metadata } = opts;
35
+
36
+ const credentialOffer = opts.credentialOffer ? await assertedUniformCredentialOffer(opts.credentialOffer) : undefined;
37
+ const pinMetadata: TxCodeAndPinRequired | undefined = credentialOffer && this.getPinMetadata(credentialOffer.credential_offer);
38
+ const issuer =
39
+ opts.credentialIssuer ??
40
+ (credentialOffer ? getIssuerFromCredentialOfferPayload(credentialOffer.credential_offer) : (metadata?.issuer as string));
41
+ if (!issuer) {
42
+ throw Error('Issuer required at this point');
43
+ }
44
+ const issuerOpts = {
45
+ issuer,
46
+ };
47
+
48
+ return await this.acquireAccessTokenUsingRequest({
49
+ accessTokenRequest: await this.createAccessTokenRequest({
50
+ credentialOffer,
51
+ asOpts,
52
+ codeVerifier,
53
+ code,
54
+ redirectUri,
55
+ pin,
56
+ pinMetadata,
57
+ }),
58
+ pinMetadata,
59
+ metadata,
60
+ asOpts,
61
+ issuerOpts,
62
+ });
63
+ }
64
+
65
+ public async acquireAccessTokenUsingRequest({
66
+ accessTokenRequest,
67
+ pinMetadata,
68
+ metadata,
69
+ asOpts,
70
+ issuerOpts,
71
+ }: {
72
+ accessTokenRequest: AccessTokenRequest;
73
+ pinMetadata?: TxCodeAndPinRequired;
74
+ metadata?: EndpointMetadata;
75
+ asOpts?: AuthorizationServerOpts;
76
+ issuerOpts?: IssuerOpts;
77
+ }): Promise<OpenIDResponse<AccessTokenResponse>> {
78
+ this.validate(accessTokenRequest, pinMetadata);
79
+
80
+ const requestTokenURL = AccessTokenClient.determineTokenURL({
81
+ asOpts,
82
+ issuerOpts,
83
+ metadata: metadata
84
+ ? metadata
85
+ : issuerOpts?.fetchMetadata
86
+ ? await MetadataClient.retrieveAllMetadata(issuerOpts.issuer, { errorOnNotFound: false })
87
+ : undefined,
88
+ });
89
+
90
+ return this.sendAuthCode(requestTokenURL, accessTokenRequest);
91
+ }
92
+
93
+ public async createAccessTokenRequest(opts: AccessTokenRequestOpts): Promise<AccessTokenRequest> {
94
+ const { asOpts, pin, codeVerifier, code, redirectUri } = opts;
95
+ const credentialOfferRequest =
96
+ opts.credentialOffer &&
97
+ determineSpecVersionFromOffer(opts.credentialOffer as CredentialOfferPayloadV1_0_13).valueOf() <= OpenId4VCIVersion.VER_1_0_13.valueOf()
98
+ ? await toUniformCredentialOfferRequest(opts.credentialOffer as CredentialOfferV1_0_13)
99
+ : undefined;
100
+ const request: Partial<AccessTokenRequest> = {};
101
+
102
+ if (asOpts?.clientId) {
103
+ request.client_id = asOpts.clientId;
104
+ }
105
+
106
+ if (credentialOfferRequest?.supportedFlows.includes(AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
107
+ this.assertAlphanumericPin(opts.pinMetadata, pin);
108
+ request.user_pin = pin;
109
+
110
+ request.grant_type = GrantTypes.PRE_AUTHORIZED_CODE;
111
+ // we actually know it is there because of the isPreAuthCode call
112
+ request[PRE_AUTH_CODE_LITERAL] =
113
+ credentialOfferRequest?.credential_offer.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code']?.[PRE_AUTH_CODE_LITERAL];
114
+
115
+ return request as AccessTokenRequest;
116
+ }
117
+
118
+ if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
119
+ request.grant_type = GrantTypes.AUTHORIZATION_CODE;
120
+ request.code = code;
121
+ request.redirect_uri = redirectUri;
122
+
123
+ if (codeVerifier) {
124
+ request.code_verifier = codeVerifier;
125
+ }
126
+
127
+ return request as AccessTokenRequest;
128
+ }
129
+
130
+ throw new Error('Credential offer request does not follow neither pre-authorized code nor authorization code flow requirements.');
131
+ }
132
+
133
+ private assertPreAuthorizedGrantType(grantType: GrantTypes): void {
134
+ if (GrantTypes.PRE_AUTHORIZED_CODE !== grantType) {
135
+ throw new Error("grant type must be 'urn:ietf:params:oauth:grant-type:pre-authorized_code'");
136
+ }
137
+ }
138
+
139
+ private assertAuthorizationGrantType(grantType: GrantTypes): void {
140
+ if (GrantTypes.AUTHORIZATION_CODE !== grantType) {
141
+ throw new Error("grant type must be 'authorization_code'");
142
+ }
143
+ }
144
+
145
+ private getPinMetadata(requestPayload: UniformCredentialOfferPayload): TxCodeAndPinRequired {
146
+ if (!requestPayload) {
147
+ throw new Error(TokenErrorResponse.invalid_request);
148
+ }
149
+ const issuer = getIssuerFromCredentialOfferPayload(requestPayload);
150
+
151
+ const grantDetails = requestPayload.grants?.['urn:ietf:params:oauth:grant-type:pre-authorized_code'];
152
+ const isPinRequired = !!grantDetails?.tx_code || !!grantDetails?.['pre-authorized_code'];
153
+
154
+ debug(`Pin required for issuer ${issuer}: ${isPinRequired}`);
155
+ return {
156
+ txCode: grantDetails?.tx_code,
157
+ isPinRequired,
158
+ };
159
+ }
160
+
161
+ private assertAlphanumericPin(pinMeta?: TxCodeAndPinRequired, pin?: string): void {
162
+ if (pinMeta?.isPinRequired) {
163
+ let regex;
164
+
165
+ if (pinMeta.txCode) {
166
+ const { input_mode, length } = pinMeta.txCode;
167
+
168
+ if (input_mode === 'numeric') {
169
+ // Create a regex for numeric input. If no length specified, allow any length of numeric input.
170
+ regex = length ? new RegExp(`^\\d{1,${length}}$`) : /^\d+$/;
171
+ } else if (input_mode === 'text') {
172
+ // Create a regex for text input. If no length specified, allow any length of alphanumeric input.
173
+ regex = length ? new RegExp(`^[a-zA-Z0-9]{1,${length}}$`) : /^[a-zA-Z0-9]+$/;
174
+ }
175
+ }
176
+
177
+ // Default regex for alphanumeric with no specific length limit if no input_mode is specified.
178
+ regex = regex || /^[a-zA-Z0-9]+$|^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/;
179
+
180
+ if (!pin || !regex.test(pin)) {
181
+ debug(
182
+ `Pin is not valid. Expected format: ${pinMeta?.txCode?.input_mode || 'alphanumeric'}, Length: up to ${pinMeta?.txCode?.length || 'any number of'} characters`,
183
+ );
184
+ throw new Error('A valid pin must be present according to the specified transaction code requirements.');
185
+ }
186
+ } else if (pin) {
187
+ debug('Pin set, whilst not required');
188
+ throw new Error('Cannot set a pin when the pin is not required.');
189
+ }
190
+ }
191
+
192
+ private assertNonEmptyPreAuthorizedCode(accessTokenRequest: AccessTokenRequest): void {
193
+ if (!accessTokenRequest[PRE_AUTH_CODE_LITERAL]) {
194
+ debug(`No pre-authorized code present, whilst it is required`);
195
+ throw new Error('Pre-authorization must be proven by presenting the pre-authorized code. Code must be present.');
196
+ }
197
+ }
198
+
199
+ private assertNonEmptyCodeVerifier(accessTokenRequest: AccessTokenRequest): void {
200
+ if (!accessTokenRequest.code_verifier) {
201
+ debug('No code_verifier present, whilst it is required');
202
+ throw new Error('Authorization flow requires the code_verifier to be present');
203
+ }
204
+ }
205
+
206
+ private assertNonEmptyCode(accessTokenRequest: AccessTokenRequest): void {
207
+ if (!accessTokenRequest.code) {
208
+ debug('No code present, whilst it is required');
209
+ throw new Error('Authorization flow requires the code to be present');
210
+ }
211
+ }
212
+ private validate(accessTokenRequest: AccessTokenRequest, pinMeta?: TxCodeAndPinRequired): void {
213
+ if (accessTokenRequest.grant_type === GrantTypes.PRE_AUTHORIZED_CODE) {
214
+ this.assertPreAuthorizedGrantType(accessTokenRequest.grant_type);
215
+ this.assertNonEmptyPreAuthorizedCode(accessTokenRequest);
216
+ this.assertAlphanumericPin(pinMeta, accessTokenRequest['pre-authorized_code']);
217
+ } else if (accessTokenRequest.grant_type === GrantTypes.AUTHORIZATION_CODE) {
218
+ this.assertAuthorizationGrantType(accessTokenRequest.grant_type);
219
+ this.assertNonEmptyCodeVerifier(accessTokenRequest);
220
+ this.assertNonEmptyCode(accessTokenRequest);
221
+ } else {
222
+ this.throwNotSupportedFlow();
223
+ }
224
+ }
225
+
226
+ private async sendAuthCode(requestTokenURL: string, accessTokenRequest: AccessTokenRequest): Promise<OpenIDResponse<AccessTokenResponse>> {
227
+ return await formPost(requestTokenURL, convertJsonToURI(accessTokenRequest, { mode: JsonURIMode.X_FORM_WWW_URLENCODED }));
228
+ }
229
+
230
+ public static determineTokenURL({
231
+ asOpts,
232
+ issuerOpts,
233
+ metadata,
234
+ }: {
235
+ asOpts?: AuthorizationServerOpts;
236
+ issuerOpts?: IssuerOpts;
237
+ metadata?: EndpointMetadata;
238
+ }): string {
239
+ if (!asOpts && !metadata?.token_endpoint && !issuerOpts) {
240
+ throw new Error('Cannot determine token URL if no issuer, metadata and no Authorization Server values are present');
241
+ }
242
+ let url;
243
+ if (asOpts && asOpts.as) {
244
+ url = this.creatTokenURLFromURL(asOpts.as, asOpts?.allowInsecureEndpoints, asOpts.tokenEndpoint);
245
+ } else if (metadata?.token_endpoint) {
246
+ url = metadata.token_endpoint;
247
+ } else {
248
+ if (!issuerOpts?.issuer) {
249
+ throw Error('Either authorization server options, a token endpoint or issuer options are required at this point');
250
+ }
251
+ url = this.creatTokenURLFromURL(issuerOpts.issuer, asOpts?.allowInsecureEndpoints, issuerOpts.tokenEndpoint);
252
+ }
253
+
254
+ if (!url || !ObjectUtils.isString(url)) {
255
+ throw new Error('No authorization server token URL present. Cannot acquire access token');
256
+ }
257
+ debug(`Token endpoint determined to be ${url}`);
258
+ return url;
259
+ }
260
+
261
+ private static creatTokenURLFromURL(url: string, allowInsecureEndpoints?: boolean, tokenEndpoint?: string): string {
262
+ if (allowInsecureEndpoints !== true && url.startsWith('http:')) {
263
+ throw Error(
264
+ `Unprotected token endpoints are not allowed ${url}. Use the 'allowInsecureEndpoints' param if you really need this for dev/testing!`,
265
+ );
266
+ }
267
+ const hostname = url.replace(/https?:\/\//, '').replace(/\/$/, '');
268
+ const endpoint = tokenEndpoint ? (tokenEndpoint.startsWith('/') ? tokenEndpoint : tokenEndpoint.substring(1)) : '/token';
269
+ const scheme = url.split('://')[0];
270
+ return `${scheme ? scheme + '://' : 'https://'}${hostname}${endpoint}`;
271
+ }
272
+
273
+ private throwNotSupportedFlow(): void {
274
+ debug(`Only pre-authorized or authorization code flows supported.`);
275
+ throw new Error('Only pre-authorized-code or authorization code flows are supported');
276
+ }
277
+ }