@sphereon/oid4vci-client 0.8.2-unstable.39 → 0.8.2-unstable.40
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.
- package/dist/AccessTokenClient.d.ts +28 -28
- package/dist/AccessTokenClient.js +221 -221
- package/dist/AuthorizationDetailsBuilder.d.ts +10 -10
- package/dist/AuthorizationDetailsBuilder.js +43 -43
- package/dist/CredentialOfferClient.d.ts +9 -9
- package/dist/CredentialOfferClient.js +87 -87
- package/dist/CredentialRequestClient.d.ts +32 -32
- package/dist/CredentialRequestClient.js +117 -117
- package/dist/CredentialRequestClientBuilder.d.ts +39 -39
- package/dist/CredentialRequestClientBuilder.js +89 -89
- package/dist/MetadataClient.d.ts +40 -40
- package/dist/MetadataClient.js +239 -239
- package/dist/OpenID4VCIClient.d.ts +81 -80
- package/dist/OpenID4VCIClient.d.ts.map +1 -1
- package/dist/OpenID4VCIClient.js +439 -436
- package/dist/OpenID4VCIClient.js.map +1 -1
- package/dist/ProofOfPossessionBuilder.d.ts +39 -39
- package/dist/ProofOfPossessionBuilder.js +150 -150
- package/dist/functions/ProofUtil.d.ts +29 -29
- package/dist/functions/ProofUtil.js +105 -105
- package/dist/functions/index.d.ts +3 -3
- package/dist/functions/index.js +19 -19
- package/dist/index.d.ts +8 -8
- package/dist/index.js +24 -24
- package/lib/CredentialRequestClient.ts +2 -2
- package/lib/OpenID4VCIClient.ts +5 -0
- package/lib/__tests__/CredentialRequestClient.spec.ts +14 -14
- package/lib/__tests__/CredentialRequestClientBuilder.spec.ts +2 -2
- package/lib/__tests__/JsonURIConversions.spec.ts +3 -3
- package/lib/__tests__/ProofOfPossessionBuilder.spec.ts +4 -4
- package/package.json +3 -3
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { AccessTokenRequest, AccessTokenRequestOpts, AccessTokenResponse, AuthorizationServerOpts, EndpointMetadata, IssuerOpts, OpenIDResponse } from '@sphereon/oid4vci-common';
|
|
2
|
-
export declare class AccessTokenClient {
|
|
3
|
-
acquireAccessToken(opts: AccessTokenRequestOpts): Promise<OpenIDResponse<AccessTokenResponse>>;
|
|
4
|
-
acquireAccessTokenUsingRequest({ accessTokenRequest, isPinRequired, metadata, asOpts, issuerOpts, }: {
|
|
5
|
-
accessTokenRequest: AccessTokenRequest;
|
|
6
|
-
isPinRequired?: boolean;
|
|
7
|
-
metadata?: EndpointMetadata;
|
|
8
|
-
asOpts?: AuthorizationServerOpts;
|
|
9
|
-
issuerOpts?: IssuerOpts;
|
|
10
|
-
}): Promise<OpenIDResponse<AccessTokenResponse>>;
|
|
11
|
-
createAccessTokenRequest(opts: AccessTokenRequestOpts): Promise<AccessTokenRequest>;
|
|
12
|
-
private assertPreAuthorizedGrantType;
|
|
13
|
-
private assertAuthorizationGrantType;
|
|
14
|
-
private isPinRequiredValue;
|
|
15
|
-
private assertNumericPin;
|
|
16
|
-
private assertNonEmptyPreAuthorizedCode;
|
|
17
|
-
private assertNonEmptyCode;
|
|
18
|
-
private assertNonEmptyRedirectUri;
|
|
19
|
-
private validate;
|
|
20
|
-
private sendAuthCode;
|
|
21
|
-
static determineTokenURL({ asOpts, issuerOpts, metadata, }: {
|
|
22
|
-
asOpts?: AuthorizationServerOpts;
|
|
23
|
-
issuerOpts?: IssuerOpts;
|
|
24
|
-
metadata?: EndpointMetadata;
|
|
25
|
-
}): string;
|
|
26
|
-
private static creatTokenURLFromURL;
|
|
27
|
-
private throwNotSupportedFlow;
|
|
28
|
-
}
|
|
1
|
+
import { AccessTokenRequest, AccessTokenRequestOpts, AccessTokenResponse, AuthorizationServerOpts, EndpointMetadata, IssuerOpts, OpenIDResponse } from '@sphereon/oid4vci-common';
|
|
2
|
+
export declare class AccessTokenClient {
|
|
3
|
+
acquireAccessToken(opts: AccessTokenRequestOpts): Promise<OpenIDResponse<AccessTokenResponse>>;
|
|
4
|
+
acquireAccessTokenUsingRequest({ accessTokenRequest, isPinRequired, metadata, asOpts, issuerOpts, }: {
|
|
5
|
+
accessTokenRequest: AccessTokenRequest;
|
|
6
|
+
isPinRequired?: boolean;
|
|
7
|
+
metadata?: EndpointMetadata;
|
|
8
|
+
asOpts?: AuthorizationServerOpts;
|
|
9
|
+
issuerOpts?: IssuerOpts;
|
|
10
|
+
}): Promise<OpenIDResponse<AccessTokenResponse>>;
|
|
11
|
+
createAccessTokenRequest(opts: AccessTokenRequestOpts): Promise<AccessTokenRequest>;
|
|
12
|
+
private assertPreAuthorizedGrantType;
|
|
13
|
+
private assertAuthorizationGrantType;
|
|
14
|
+
private isPinRequiredValue;
|
|
15
|
+
private assertNumericPin;
|
|
16
|
+
private assertNonEmptyPreAuthorizedCode;
|
|
17
|
+
private assertNonEmptyCode;
|
|
18
|
+
private assertNonEmptyRedirectUri;
|
|
19
|
+
private validate;
|
|
20
|
+
private sendAuthCode;
|
|
21
|
+
static determineTokenURL({ asOpts, issuerOpts, metadata, }: {
|
|
22
|
+
asOpts?: AuthorizationServerOpts;
|
|
23
|
+
issuerOpts?: IssuerOpts;
|
|
24
|
+
metadata?: EndpointMetadata;
|
|
25
|
+
}): string;
|
|
26
|
+
private static creatTokenURLFromURL;
|
|
27
|
+
private throwNotSupportedFlow;
|
|
28
|
+
}
|
|
29
29
|
//# sourceMappingURL=AccessTokenClient.d.ts.map
|
|
@@ -1,222 +1,222 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.AccessTokenClient = void 0;
|
|
16
|
-
const oid4vci_common_1 = require("@sphereon/oid4vci-common");
|
|
17
|
-
const ssi_types_1 = require("@sphereon/ssi-types");
|
|
18
|
-
const debug_1 = __importDefault(require("debug"));
|
|
19
|
-
const MetadataClient_1 = require("./MetadataClient");
|
|
20
|
-
const functions_1 = require("./functions");
|
|
21
|
-
const debug = (0, debug_1.default)('sphereon:oid4vci:token');
|
|
22
|
-
class AccessTokenClient {
|
|
23
|
-
acquireAccessToken(opts) {
|
|
24
|
-
var _a;
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const { asOpts, pin, codeVerifier, code, redirectUri, metadata } = opts;
|
|
27
|
-
const credentialOffer = opts.credentialOffer ? yield (0, oid4vci_common_1.assertedUniformCredentialOffer)(opts.credentialOffer) : undefined;
|
|
28
|
-
const isPinRequired = credentialOffer && this.isPinRequiredValue(credentialOffer.credential_offer);
|
|
29
|
-
const issuer = (_a = opts.credentialIssuer) !== null && _a !== void 0 ? _a : (credentialOffer ? (0, oid4vci_common_1.getIssuerFromCredentialOfferPayload)(credentialOffer.credential_offer) : metadata === null || metadata === void 0 ? void 0 : metadata.issuer);
|
|
30
|
-
if (!issuer) {
|
|
31
|
-
throw Error('Issuer required at this point');
|
|
32
|
-
}
|
|
33
|
-
const issuerOpts = {
|
|
34
|
-
issuer,
|
|
35
|
-
};
|
|
36
|
-
return yield this.acquireAccessTokenUsingRequest({
|
|
37
|
-
accessTokenRequest: yield this.createAccessTokenRequest({
|
|
38
|
-
credentialOffer,
|
|
39
|
-
asOpts,
|
|
40
|
-
codeVerifier,
|
|
41
|
-
code,
|
|
42
|
-
redirectUri,
|
|
43
|
-
pin,
|
|
44
|
-
}),
|
|
45
|
-
isPinRequired,
|
|
46
|
-
metadata,
|
|
47
|
-
asOpts,
|
|
48
|
-
issuerOpts,
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
acquireAccessTokenUsingRequest({ accessTokenRequest, isPinRequired, metadata, asOpts, issuerOpts, }) {
|
|
53
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
this.validate(accessTokenRequest, isPinRequired);
|
|
55
|
-
const requestTokenURL = AccessTokenClient.determineTokenURL({
|
|
56
|
-
asOpts,
|
|
57
|
-
issuerOpts,
|
|
58
|
-
metadata: metadata
|
|
59
|
-
? metadata
|
|
60
|
-
: (issuerOpts === null || issuerOpts === void 0 ? void 0 : issuerOpts.fetchMetadata)
|
|
61
|
-
? yield MetadataClient_1.MetadataClient.retrieveAllMetadata([issuerOpts.issuer], { errorOnNotFound: false }) // TODO multi-server support?
|
|
62
|
-
: undefined,
|
|
63
|
-
});
|
|
64
|
-
return this.sendAuthCode(requestTokenURL, accessTokenRequest, asOpts === null || asOpts === void 0 ? void 0 : asOpts.tokenProxyUrl);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
createAccessTokenRequest(opts) {
|
|
68
|
-
var _a, _b;
|
|
69
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
const { asOpts, pin, codeVerifier, code, redirectUri } = opts;
|
|
71
|
-
const credentialOfferRequest = opts.credentialOffer ? yield (0, oid4vci_common_1.toUniformCredentialOfferRequest)(opts.credentialOffer) : undefined;
|
|
72
|
-
const request = {};
|
|
73
|
-
if (asOpts === null || asOpts === void 0 ? void 0 : asOpts.clientId) {
|
|
74
|
-
request.client_id = asOpts.clientId;
|
|
75
|
-
}
|
|
76
|
-
if (credentialOfferRequest === null || credentialOfferRequest === void 0 ? void 0 : credentialOfferRequest.supportedFlows.includes(oid4vci_common_1.AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
|
|
77
|
-
this.assertNumericPin(this.isPinRequiredValue(credentialOfferRequest.credential_offer), pin);
|
|
78
|
-
request.user_pin = pin;
|
|
79
|
-
request.grant_type = oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE;
|
|
80
|
-
// we actually know it is there because of the isPreAuthCode call
|
|
81
|
-
request[oid4vci_common_1.PRE_AUTH_CODE_LITERAL] =
|
|
82
|
-
(_b = (_a = credentialOfferRequest === null || credentialOfferRequest === void 0 ? void 0 : credentialOfferRequest.credential_offer.grants) === null || _a === void 0 ? void 0 : _a['urn:ietf:params:oauth:grant-type:pre-authorized_code']) === null || _b === void 0 ? void 0 : _b[oid4vci_common_1.PRE_AUTH_CODE_LITERAL];
|
|
83
|
-
return request;
|
|
84
|
-
}
|
|
85
|
-
if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(oid4vci_common_1.AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
|
|
86
|
-
request.grant_type = oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE;
|
|
87
|
-
request.code = code;
|
|
88
|
-
request.redirect_uri = redirectUri;
|
|
89
|
-
if (codeVerifier) {
|
|
90
|
-
request.code_verifier = codeVerifier;
|
|
91
|
-
}
|
|
92
|
-
return request;
|
|
93
|
-
}
|
|
94
|
-
throw new Error('Credential offer request does not follow neither pre-authorized code nor authorization code flow requirements.');
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
assertPreAuthorizedGrantType(grantType) {
|
|
98
|
-
if (oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE !== grantType) {
|
|
99
|
-
throw new Error("grant type must be 'urn:ietf:params:oauth:grant-type:pre-authorized_code'");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
assertAuthorizationGrantType(grantType) {
|
|
103
|
-
if (oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE !== grantType) {
|
|
104
|
-
throw new Error("grant type must be 'authorization_code'");
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
isPinRequiredValue(requestPayload) {
|
|
108
|
-
var _a, _b, _c;
|
|
109
|
-
let isPinRequired = false;
|
|
110
|
-
if (!requestPayload) {
|
|
111
|
-
throw new Error(oid4vci_common_1.TokenErrorResponse.invalid_request);
|
|
112
|
-
}
|
|
113
|
-
const issuer = (0, oid4vci_common_1.getIssuerFromCredentialOfferPayload)(requestPayload);
|
|
114
|
-
if ((_a = requestPayload.grants) === null || _a === void 0 ? void 0 : _a['urn:ietf:params:oauth:grant-type:pre-authorized_code']) {
|
|
115
|
-
isPinRequired = (_c = (_b = requestPayload.grants['urn:ietf:params:oauth:grant-type:pre-authorized_code']) === null || _b === void 0 ? void 0 : _b.user_pin_required) !== null && _c !== void 0 ? _c : false;
|
|
116
|
-
}
|
|
117
|
-
debug(`Pin required for issuer ${issuer}: ${isPinRequired}`);
|
|
118
|
-
return isPinRequired;
|
|
119
|
-
}
|
|
120
|
-
assertNumericPin(isPinRequired, pin) {
|
|
121
|
-
if (isPinRequired) {
|
|
122
|
-
if (!pin || !/^\d{1,8}$/.test(pin)) {
|
|
123
|
-
debug(`Pin is not 1 to 8 digits long`);
|
|
124
|
-
throw new Error('A valid pin consisting of maximal 8 numeric characters must be present.');
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
else if (pin) {
|
|
128
|
-
debug(`Pin set, whilst not required`);
|
|
129
|
-
throw new Error('Cannot set a pin, when the pin is not required.');
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
assertNonEmptyPreAuthorizedCode(accessTokenRequest) {
|
|
133
|
-
if (!accessTokenRequest[oid4vci_common_1.PRE_AUTH_CODE_LITERAL]) {
|
|
134
|
-
debug(`No pre-authorized code present, whilst it is required`);
|
|
135
|
-
throw new Error('Pre-authorization must be proven by presenting the pre-authorized code. Code must be present.');
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
/*
|
|
139
|
-
private assertNonEmptyCodeVerifier(accessTokenRequest: AccessTokenRequest): void {
|
|
140
|
-
if (!accessTokenRequest.code_verifier) {
|
|
141
|
-
debug('No code_verifier present, whilst it is required');
|
|
142
|
-
throw new Error('Authorization flow requires the code_verifier to be present');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
*/
|
|
146
|
-
assertNonEmptyCode(accessTokenRequest) {
|
|
147
|
-
if (!accessTokenRequest.code) {
|
|
148
|
-
debug('No code present, whilst it is required');
|
|
149
|
-
throw new Error('Authorization flow requires the code to be present');
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
assertNonEmptyRedirectUri(accessTokenRequest) {
|
|
153
|
-
if (!accessTokenRequest.redirect_uri) {
|
|
154
|
-
debug('No redirect_uri present, whilst it is required');
|
|
155
|
-
throw new Error('Authorization flow requires the redirect_uri to be present');
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
validate(accessTokenRequest, isPinRequired) {
|
|
159
|
-
if (accessTokenRequest.grant_type === oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE) {
|
|
160
|
-
this.assertPreAuthorizedGrantType(accessTokenRequest.grant_type);
|
|
161
|
-
this.assertNonEmptyPreAuthorizedCode(accessTokenRequest);
|
|
162
|
-
this.assertNumericPin(isPinRequired, accessTokenRequest.user_pin);
|
|
163
|
-
}
|
|
164
|
-
else if (accessTokenRequest.grant_type === oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE) {
|
|
165
|
-
this.assertAuthorizationGrantType(accessTokenRequest.grant_type);
|
|
166
|
-
// this.assertNonEmptyCodeVerifier(accessTokenRequest); TODO figure out of always required, for MOSIP I do not have it
|
|
167
|
-
this.assertNonEmptyCode(accessTokenRequest);
|
|
168
|
-
this.assertNonEmptyRedirectUri(accessTokenRequest);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
this.throwNotSupportedFlow;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
sendAuthCode(requestTokenURL, accessTokenRequest, tokenProxyUrl) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
if (!tokenProxyUrl) {
|
|
177
|
-
return yield (0, functions_1.formPost)(requestTokenURL, (0, functions_1.convertJsonToURI)(accessTokenRequest));
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
return yield (0, functions_1.formPost)(tokenProxyUrl, JSON.stringify(Object.assign(Object.assign({}, accessTokenRequest), { issuer_url: requestTokenURL })), { contentType: 'application/json' }); // TODO maybe switch to x-www-form-urlencoded in proxy backend ot support both
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
static determineTokenURL({ asOpts, issuerOpts, metadata, }) {
|
|
185
|
-
if (!asOpts && !(metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) && !issuerOpts) {
|
|
186
|
-
throw new Error('Cannot determine token URL if no issuer, metadata and no Authorization Server values are present');
|
|
187
|
-
}
|
|
188
|
-
let url;
|
|
189
|
-
if (asOpts && asOpts.as) {
|
|
190
|
-
url = this.creatTokenURLFromURL(asOpts.as, asOpts === null || asOpts === void 0 ? void 0 : asOpts.allowInsecureEndpoints, asOpts.tokenEndpoint);
|
|
191
|
-
}
|
|
192
|
-
else if (metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) {
|
|
193
|
-
url = metadata.token_endpoint;
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
if (!(issuerOpts === null || issuerOpts === void 0 ? void 0 : issuerOpts.issuer)) {
|
|
197
|
-
throw Error('Either authorization server options, a token endpoint or issuer options are required at this point');
|
|
198
|
-
}
|
|
199
|
-
url = this.creatTokenURLFromURL(issuerOpts.issuer, asOpts === null || asOpts === void 0 ? void 0 : asOpts.allowInsecureEndpoints, issuerOpts.tokenEndpoint);
|
|
200
|
-
}
|
|
201
|
-
if (!url || !ssi_types_1.ObjectUtils.isString(url)) {
|
|
202
|
-
throw new Error('No authorization server token URL present. Cannot acquire access token');
|
|
203
|
-
}
|
|
204
|
-
debug(`Token endpoint determined to be ${url}`);
|
|
205
|
-
return url;
|
|
206
|
-
}
|
|
207
|
-
static creatTokenURLFromURL(url, allowInsecureEndpoints, tokenEndpoint) {
|
|
208
|
-
if (allowInsecureEndpoints !== true && url.startsWith('http:')) {
|
|
209
|
-
throw Error(`Unprotected token endpoints are not allowed ${url}. Adjust settings if you really need this (dev/test settings only!!)`);
|
|
210
|
-
}
|
|
211
|
-
const hostname = url.replace(/https?:\/\//, '').replace(/\/$/, '');
|
|
212
|
-
const endpoint = tokenEndpoint ? (tokenEndpoint.startsWith('/') ? tokenEndpoint : tokenEndpoint.substring(1)) : '/token';
|
|
213
|
-
const scheme = url.split('://')[0];
|
|
214
|
-
return `${scheme ? scheme + '://' : 'https://'}${hostname}${endpoint}`;
|
|
215
|
-
}
|
|
216
|
-
throwNotSupportedFlow() {
|
|
217
|
-
debug(`Only pre-authorized or authorization code flows supported.`);
|
|
218
|
-
throw new Error('Only pre-authorized-code or authorization code flows are supported');
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
exports.AccessTokenClient = AccessTokenClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AccessTokenClient = void 0;
|
|
16
|
+
const oid4vci_common_1 = require("@sphereon/oid4vci-common");
|
|
17
|
+
const ssi_types_1 = require("@sphereon/ssi-types");
|
|
18
|
+
const debug_1 = __importDefault(require("debug"));
|
|
19
|
+
const MetadataClient_1 = require("./MetadataClient");
|
|
20
|
+
const functions_1 = require("./functions");
|
|
21
|
+
const debug = (0, debug_1.default)('sphereon:oid4vci:token');
|
|
22
|
+
class AccessTokenClient {
|
|
23
|
+
acquireAccessToken(opts) {
|
|
24
|
+
var _a;
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const { asOpts, pin, codeVerifier, code, redirectUri, metadata } = opts;
|
|
27
|
+
const credentialOffer = opts.credentialOffer ? yield (0, oid4vci_common_1.assertedUniformCredentialOffer)(opts.credentialOffer) : undefined;
|
|
28
|
+
const isPinRequired = credentialOffer && this.isPinRequiredValue(credentialOffer.credential_offer);
|
|
29
|
+
const issuer = (_a = opts.credentialIssuer) !== null && _a !== void 0 ? _a : (credentialOffer ? (0, oid4vci_common_1.getIssuerFromCredentialOfferPayload)(credentialOffer.credential_offer) : metadata === null || metadata === void 0 ? void 0 : metadata.issuer);
|
|
30
|
+
if (!issuer) {
|
|
31
|
+
throw Error('Issuer required at this point');
|
|
32
|
+
}
|
|
33
|
+
const issuerOpts = {
|
|
34
|
+
issuer,
|
|
35
|
+
};
|
|
36
|
+
return yield this.acquireAccessTokenUsingRequest({
|
|
37
|
+
accessTokenRequest: yield this.createAccessTokenRequest({
|
|
38
|
+
credentialOffer,
|
|
39
|
+
asOpts,
|
|
40
|
+
codeVerifier,
|
|
41
|
+
code,
|
|
42
|
+
redirectUri,
|
|
43
|
+
pin,
|
|
44
|
+
}),
|
|
45
|
+
isPinRequired,
|
|
46
|
+
metadata,
|
|
47
|
+
asOpts,
|
|
48
|
+
issuerOpts,
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
acquireAccessTokenUsingRequest({ accessTokenRequest, isPinRequired, metadata, asOpts, issuerOpts, }) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
this.validate(accessTokenRequest, isPinRequired);
|
|
55
|
+
const requestTokenURL = AccessTokenClient.determineTokenURL({
|
|
56
|
+
asOpts,
|
|
57
|
+
issuerOpts,
|
|
58
|
+
metadata: metadata
|
|
59
|
+
? metadata
|
|
60
|
+
: (issuerOpts === null || issuerOpts === void 0 ? void 0 : issuerOpts.fetchMetadata)
|
|
61
|
+
? yield MetadataClient_1.MetadataClient.retrieveAllMetadata([issuerOpts.issuer], { errorOnNotFound: false }) // TODO multi-server support?
|
|
62
|
+
: undefined,
|
|
63
|
+
});
|
|
64
|
+
return this.sendAuthCode(requestTokenURL, accessTokenRequest, asOpts === null || asOpts === void 0 ? void 0 : asOpts.tokenProxyUrl);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
createAccessTokenRequest(opts) {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const { asOpts, pin, codeVerifier, code, redirectUri } = opts;
|
|
71
|
+
const credentialOfferRequest = opts.credentialOffer ? yield (0, oid4vci_common_1.toUniformCredentialOfferRequest)(opts.credentialOffer) : undefined;
|
|
72
|
+
const request = {};
|
|
73
|
+
if (asOpts === null || asOpts === void 0 ? void 0 : asOpts.clientId) {
|
|
74
|
+
request.client_id = asOpts.clientId;
|
|
75
|
+
}
|
|
76
|
+
if (credentialOfferRequest === null || credentialOfferRequest === void 0 ? void 0 : credentialOfferRequest.supportedFlows.includes(oid4vci_common_1.AuthzFlowType.PRE_AUTHORIZED_CODE_FLOW)) {
|
|
77
|
+
this.assertNumericPin(this.isPinRequiredValue(credentialOfferRequest.credential_offer), pin);
|
|
78
|
+
request.user_pin = pin;
|
|
79
|
+
request.grant_type = oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE;
|
|
80
|
+
// we actually know it is there because of the isPreAuthCode call
|
|
81
|
+
request[oid4vci_common_1.PRE_AUTH_CODE_LITERAL] =
|
|
82
|
+
(_b = (_a = credentialOfferRequest === null || credentialOfferRequest === void 0 ? void 0 : credentialOfferRequest.credential_offer.grants) === null || _a === void 0 ? void 0 : _a['urn:ietf:params:oauth:grant-type:pre-authorized_code']) === null || _b === void 0 ? void 0 : _b[oid4vci_common_1.PRE_AUTH_CODE_LITERAL];
|
|
83
|
+
return request;
|
|
84
|
+
}
|
|
85
|
+
if (!credentialOfferRequest || credentialOfferRequest.supportedFlows.includes(oid4vci_common_1.AuthzFlowType.AUTHORIZATION_CODE_FLOW)) {
|
|
86
|
+
request.grant_type = oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE;
|
|
87
|
+
request.code = code;
|
|
88
|
+
request.redirect_uri = redirectUri;
|
|
89
|
+
if (codeVerifier) {
|
|
90
|
+
request.code_verifier = codeVerifier;
|
|
91
|
+
}
|
|
92
|
+
return request;
|
|
93
|
+
}
|
|
94
|
+
throw new Error('Credential offer request does not follow neither pre-authorized code nor authorization code flow requirements.');
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
assertPreAuthorizedGrantType(grantType) {
|
|
98
|
+
if (oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE !== grantType) {
|
|
99
|
+
throw new Error("grant type must be 'urn:ietf:params:oauth:grant-type:pre-authorized_code'");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
assertAuthorizationGrantType(grantType) {
|
|
103
|
+
if (oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE !== grantType) {
|
|
104
|
+
throw new Error("grant type must be 'authorization_code'");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
isPinRequiredValue(requestPayload) {
|
|
108
|
+
var _a, _b, _c;
|
|
109
|
+
let isPinRequired = false;
|
|
110
|
+
if (!requestPayload) {
|
|
111
|
+
throw new Error(oid4vci_common_1.TokenErrorResponse.invalid_request);
|
|
112
|
+
}
|
|
113
|
+
const issuer = (0, oid4vci_common_1.getIssuerFromCredentialOfferPayload)(requestPayload);
|
|
114
|
+
if ((_a = requestPayload.grants) === null || _a === void 0 ? void 0 : _a['urn:ietf:params:oauth:grant-type:pre-authorized_code']) {
|
|
115
|
+
isPinRequired = (_c = (_b = requestPayload.grants['urn:ietf:params:oauth:grant-type:pre-authorized_code']) === null || _b === void 0 ? void 0 : _b.user_pin_required) !== null && _c !== void 0 ? _c : false;
|
|
116
|
+
}
|
|
117
|
+
debug(`Pin required for issuer ${issuer}: ${isPinRequired}`);
|
|
118
|
+
return isPinRequired;
|
|
119
|
+
}
|
|
120
|
+
assertNumericPin(isPinRequired, pin) {
|
|
121
|
+
if (isPinRequired) {
|
|
122
|
+
if (!pin || !/^\d{1,8}$/.test(pin)) {
|
|
123
|
+
debug(`Pin is not 1 to 8 digits long`);
|
|
124
|
+
throw new Error('A valid pin consisting of maximal 8 numeric characters must be present.');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (pin) {
|
|
128
|
+
debug(`Pin set, whilst not required`);
|
|
129
|
+
throw new Error('Cannot set a pin, when the pin is not required.');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
assertNonEmptyPreAuthorizedCode(accessTokenRequest) {
|
|
133
|
+
if (!accessTokenRequest[oid4vci_common_1.PRE_AUTH_CODE_LITERAL]) {
|
|
134
|
+
debug(`No pre-authorized code present, whilst it is required`);
|
|
135
|
+
throw new Error('Pre-authorization must be proven by presenting the pre-authorized code. Code must be present.');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/*
|
|
139
|
+
private assertNonEmptyCodeVerifier(accessTokenRequest: AccessTokenRequest): void {
|
|
140
|
+
if (!accessTokenRequest.code_verifier) {
|
|
141
|
+
debug('No code_verifier present, whilst it is required');
|
|
142
|
+
throw new Error('Authorization flow requires the code_verifier to be present');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
*/
|
|
146
|
+
assertNonEmptyCode(accessTokenRequest) {
|
|
147
|
+
if (!accessTokenRequest.code) {
|
|
148
|
+
debug('No code present, whilst it is required');
|
|
149
|
+
throw new Error('Authorization flow requires the code to be present');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
assertNonEmptyRedirectUri(accessTokenRequest) {
|
|
153
|
+
if (!accessTokenRequest.redirect_uri) {
|
|
154
|
+
debug('No redirect_uri present, whilst it is required');
|
|
155
|
+
throw new Error('Authorization flow requires the redirect_uri to be present');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
validate(accessTokenRequest, isPinRequired) {
|
|
159
|
+
if (accessTokenRequest.grant_type === oid4vci_common_1.GrantTypes.PRE_AUTHORIZED_CODE) {
|
|
160
|
+
this.assertPreAuthorizedGrantType(accessTokenRequest.grant_type);
|
|
161
|
+
this.assertNonEmptyPreAuthorizedCode(accessTokenRequest);
|
|
162
|
+
this.assertNumericPin(isPinRequired, accessTokenRequest.user_pin);
|
|
163
|
+
}
|
|
164
|
+
else if (accessTokenRequest.grant_type === oid4vci_common_1.GrantTypes.AUTHORIZATION_CODE) {
|
|
165
|
+
this.assertAuthorizationGrantType(accessTokenRequest.grant_type);
|
|
166
|
+
// this.assertNonEmptyCodeVerifier(accessTokenRequest); TODO figure out of always required, for MOSIP I do not have it
|
|
167
|
+
this.assertNonEmptyCode(accessTokenRequest);
|
|
168
|
+
this.assertNonEmptyRedirectUri(accessTokenRequest);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
this.throwNotSupportedFlow;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
sendAuthCode(requestTokenURL, accessTokenRequest, tokenProxyUrl) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
if (!tokenProxyUrl) {
|
|
177
|
+
return yield (0, functions_1.formPost)(requestTokenURL, (0, functions_1.convertJsonToURI)(accessTokenRequest));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
return yield (0, functions_1.formPost)(tokenProxyUrl, JSON.stringify(Object.assign(Object.assign({}, accessTokenRequest), { issuer_url: requestTokenURL })), { contentType: 'application/json' }); // TODO maybe switch to x-www-form-urlencoded in proxy backend ot support both
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
static determineTokenURL({ asOpts, issuerOpts, metadata, }) {
|
|
185
|
+
if (!asOpts && !(metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) && !issuerOpts) {
|
|
186
|
+
throw new Error('Cannot determine token URL if no issuer, metadata and no Authorization Server values are present');
|
|
187
|
+
}
|
|
188
|
+
let url;
|
|
189
|
+
if (asOpts && asOpts.as) {
|
|
190
|
+
url = this.creatTokenURLFromURL(asOpts.as, asOpts === null || asOpts === void 0 ? void 0 : asOpts.allowInsecureEndpoints, asOpts.tokenEndpoint);
|
|
191
|
+
}
|
|
192
|
+
else if (metadata === null || metadata === void 0 ? void 0 : metadata.token_endpoint) {
|
|
193
|
+
url = metadata.token_endpoint;
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
if (!(issuerOpts === null || issuerOpts === void 0 ? void 0 : issuerOpts.issuer)) {
|
|
197
|
+
throw Error('Either authorization server options, a token endpoint or issuer options are required at this point');
|
|
198
|
+
}
|
|
199
|
+
url = this.creatTokenURLFromURL(issuerOpts.issuer, asOpts === null || asOpts === void 0 ? void 0 : asOpts.allowInsecureEndpoints, issuerOpts.tokenEndpoint);
|
|
200
|
+
}
|
|
201
|
+
if (!url || !ssi_types_1.ObjectUtils.isString(url)) {
|
|
202
|
+
throw new Error('No authorization server token URL present. Cannot acquire access token');
|
|
203
|
+
}
|
|
204
|
+
debug(`Token endpoint determined to be ${url}`);
|
|
205
|
+
return url;
|
|
206
|
+
}
|
|
207
|
+
static creatTokenURLFromURL(url, allowInsecureEndpoints, tokenEndpoint) {
|
|
208
|
+
if (allowInsecureEndpoints !== true && url.startsWith('http:')) {
|
|
209
|
+
throw Error(`Unprotected token endpoints are not allowed ${url}. Adjust settings if you really need this (dev/test settings only!!)`);
|
|
210
|
+
}
|
|
211
|
+
const hostname = url.replace(/https?:\/\//, '').replace(/\/$/, '');
|
|
212
|
+
const endpoint = tokenEndpoint ? (tokenEndpoint.startsWith('/') ? tokenEndpoint : tokenEndpoint.substring(1)) : '/token';
|
|
213
|
+
const scheme = url.split('://')[0];
|
|
214
|
+
return `${scheme ? scheme + '://' : 'https://'}${hostname}${endpoint}`;
|
|
215
|
+
}
|
|
216
|
+
throwNotSupportedFlow() {
|
|
217
|
+
debug(`Only pre-authorized or authorization code flows supported.`);
|
|
218
|
+
throw new Error('Only pre-authorized-code or authorization code flows are supported');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.AccessTokenClient = AccessTokenClient;
|
|
222
222
|
//# sourceMappingURL=AccessTokenClient.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { AuthorizationDetailsJwtVcJson, OID4VCICredentialFormat } from '@sphereon/oid4vci-common';
|
|
2
|
-
export declare class AuthorizationDetailsBuilder {
|
|
3
|
-
private readonly authorizationDetails;
|
|
4
|
-
constructor();
|
|
5
|
-
withType(type: string): AuthorizationDetailsBuilder;
|
|
6
|
-
withFormats(format: OID4VCICredentialFormat): AuthorizationDetailsBuilder;
|
|
7
|
-
withLocations(locations: string[]): AuthorizationDetailsBuilder;
|
|
8
|
-
addLocation(location: string): AuthorizationDetailsBuilder;
|
|
9
|
-
buildJwtVcJson(): AuthorizationDetailsJwtVcJson;
|
|
10
|
-
}
|
|
1
|
+
import { AuthorizationDetailsJwtVcJson, OID4VCICredentialFormat } from '@sphereon/oid4vci-common';
|
|
2
|
+
export declare class AuthorizationDetailsBuilder {
|
|
3
|
+
private readonly authorizationDetails;
|
|
4
|
+
constructor();
|
|
5
|
+
withType(type: string): AuthorizationDetailsBuilder;
|
|
6
|
+
withFormats(format: OID4VCICredentialFormat): AuthorizationDetailsBuilder;
|
|
7
|
+
withLocations(locations: string[]): AuthorizationDetailsBuilder;
|
|
8
|
+
addLocation(location: string): AuthorizationDetailsBuilder;
|
|
9
|
+
buildJwtVcJson(): AuthorizationDetailsJwtVcJson;
|
|
10
|
+
}
|
|
11
11
|
//# sourceMappingURL=AuthorizationDetailsBuilder.d.ts.map
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AuthorizationDetailsBuilder = void 0;
|
|
4
|
-
//todo: refactor this builder to be able to create ldp details as well
|
|
5
|
-
class AuthorizationDetailsBuilder {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.authorizationDetails = {};
|
|
8
|
-
}
|
|
9
|
-
withType(type) {
|
|
10
|
-
this.authorizationDetails.type = type;
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
|
-
withFormats(format) {
|
|
14
|
-
this.authorizationDetails.format = format;
|
|
15
|
-
return this;
|
|
16
|
-
}
|
|
17
|
-
withLocations(locations) {
|
|
18
|
-
if (this.authorizationDetails.locations) {
|
|
19
|
-
this.authorizationDetails.locations.push(...locations);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
this.authorizationDetails.locations = locations;
|
|
23
|
-
}
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
addLocation(location) {
|
|
27
|
-
if (this.authorizationDetails.locations) {
|
|
28
|
-
this.authorizationDetails.locations.push(location);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
this.authorizationDetails.locations = [location];
|
|
32
|
-
}
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
//todo: we have to consider one thing, if this is a general purpose builder, we want to support ldp types here as well. and for that we need a few checks.
|
|
36
|
-
buildJwtVcJson() {
|
|
37
|
-
if (this.authorizationDetails.format && this.authorizationDetails.type) {
|
|
38
|
-
return this.authorizationDetails;
|
|
39
|
-
}
|
|
40
|
-
throw new Error('Type and format are required properties');
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
exports.AuthorizationDetailsBuilder = AuthorizationDetailsBuilder;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthorizationDetailsBuilder = void 0;
|
|
4
|
+
//todo: refactor this builder to be able to create ldp details as well
|
|
5
|
+
class AuthorizationDetailsBuilder {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.authorizationDetails = {};
|
|
8
|
+
}
|
|
9
|
+
withType(type) {
|
|
10
|
+
this.authorizationDetails.type = type;
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
withFormats(format) {
|
|
14
|
+
this.authorizationDetails.format = format;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
withLocations(locations) {
|
|
18
|
+
if (this.authorizationDetails.locations) {
|
|
19
|
+
this.authorizationDetails.locations.push(...locations);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.authorizationDetails.locations = locations;
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
addLocation(location) {
|
|
27
|
+
if (this.authorizationDetails.locations) {
|
|
28
|
+
this.authorizationDetails.locations.push(location);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
this.authorizationDetails.locations = [location];
|
|
32
|
+
}
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
//todo: we have to consider one thing, if this is a general purpose builder, we want to support ldp types here as well. and for that we need a few checks.
|
|
36
|
+
buildJwtVcJson() {
|
|
37
|
+
if (this.authorizationDetails.format && this.authorizationDetails.type) {
|
|
38
|
+
return this.authorizationDetails;
|
|
39
|
+
}
|
|
40
|
+
throw new Error('Type and format are required properties');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.AuthorizationDetailsBuilder = AuthorizationDetailsBuilder;
|
|
44
44
|
//# sourceMappingURL=AuthorizationDetailsBuilder.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { CredentialOfferRequestWithBaseUrl, OpenId4VCIVersion } from '@sphereon/oid4vci-common';
|
|
2
|
-
export declare class CredentialOfferClient {
|
|
3
|
-
static fromURI(uri: string, opts?: {
|
|
4
|
-
resolve?: boolean;
|
|
5
|
-
}): Promise<CredentialOfferRequestWithBaseUrl>;
|
|
6
|
-
static toURI(requestWithBaseUrl: CredentialOfferRequestWithBaseUrl, opts?: {
|
|
7
|
-
version?: OpenId4VCIVersion;
|
|
8
|
-
}): string;
|
|
9
|
-
}
|
|
1
|
+
import { CredentialOfferRequestWithBaseUrl, OpenId4VCIVersion } from '@sphereon/oid4vci-common';
|
|
2
|
+
export declare class CredentialOfferClient {
|
|
3
|
+
static fromURI(uri: string, opts?: {
|
|
4
|
+
resolve?: boolean;
|
|
5
|
+
}): Promise<CredentialOfferRequestWithBaseUrl>;
|
|
6
|
+
static toURI(requestWithBaseUrl: CredentialOfferRequestWithBaseUrl, opts?: {
|
|
7
|
+
version?: OpenId4VCIVersion;
|
|
8
|
+
}): string;
|
|
9
|
+
}
|
|
10
10
|
//# sourceMappingURL=CredentialOfferClient.d.ts.map
|