-
Notifications
You must be signed in to change notification settings - Fork 25
/
AuthenticationResponse.ts
296 lines (271 loc) · 12.9 KB
/
AuthenticationResponse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { JWK } from 'jose/types';
import { JWTHeader } from '../did-jwt-fork/JWT';
import AuthenticationRequest from './AuthenticationRequest';
import { createDiscoveryMetadataPayload } from './AuthenticationResponseRegistration';
import { PresentationExchange } from './PresentationExchange';
import { DIDJwt, DIDres, State } from './functions';
import { signDidJwtPayload, verifyDidJWT } from './functions/DidJWT';
import { getPublicJWKFromHexPrivateKey, getThumbprint } from './functions/Keys';
import { JWT, SIOP, SIOPErrors } from './types';
import {
AuthenticationResponsePayload,
PresentationDefinitionWithLocation,
PresentationLocation,
SubjectIdentifierType,
VerifiablePresentationPayload,
VerifiedAuthenticationResponseWithJWT,
VerifyAuthenticationResponseOpts,
} from './types/SIOP.types';
export default class AuthenticationResponse {
/**
* Creates a SIOP Response Object
*
* @param requestJwt
* @param responseOpts
* @param verifyOpts
*/
static async createJWTFromRequestJWT(
requestJwt: string,
responseOpts: SIOP.AuthenticationResponseOpts,
verifyOpts: SIOP.VerifyAuthenticationRequestOpts
): Promise<SIOP.AuthenticationResponseWithJWT> {
assertValidResponseOpts(responseOpts);
if (!requestJwt || !requestJwt.startsWith('ey')) {
throw new Error(SIOPErrors.NO_JWT);
}
const verifiedJWT = await AuthenticationRequest.verifyJWT(requestJwt, verifyOpts);
return AuthenticationResponse.createJWTFromVerifiedRequest(verifiedJWT, responseOpts);
}
static async createJWTFromVerifiedRequest(
verifiedJwt: SIOP.VerifiedAuthenticationRequestWithJWT,
responseOpts: SIOP.AuthenticationResponseOpts
): Promise<SIOP.AuthenticationResponseWithJWT> {
const payload = await createSIOPResponsePayload(verifiedJwt, responseOpts);
await assertValidVerifiablePresentations(verifiedJwt.presentationDefinitions, payload);
const jwt = await signDidJwtPayload(payload, responseOpts);
return {
jwt,
state: payload.state,
nonce: payload.nonce,
payload,
responseOpts,
};
// todo add uri generation support in separate method, like in the AuthRequest class
/*if (isInternalSignature(responseOpts.signatureType)) {
return DIDJwt.signDidJwtInternal(payload, ResponseIss.SELF_ISSUED_V2, responseOpts.signatureType.hexPrivateKey, responseOpts.signatureType.kid);
} else if (isExternalSignature(responseOpts.signatureType)) {
return DIDJwt.signDidJwtExternal(payload, responseOpts.signatureType.signatureUri, responseOpts.signatureType.authZToken, responseOpts.signatureType.kid);
} else {
throw new Error("INVALID_SIGNATURE_TYPE");
}*/
/*const params = `id_token=${JWT}`;
const uriResponse = {
encodedUri: "",
bodyEncoded: "",
encodingFormat: SIOP.UrlEncodingFormat.FORM_URL_ENCODED,
responseMode: didAuthResponseCall.responseMode
? didAuthResponseCall.responseMode
: SIOP.ResponseMode.FRAGMENT, // FRAGMENT is the default
};
if (didAuthResponseCall.responseMode === SIOP.ResponseMode.FORM_POST) {
uriResponse.encodedUri = encodeURI(didAuthResponseCall.redirectUri);
uriResponse.bodyEncoded = encodeURI(params);
} else if (didAuthResponseCall.responseMode === SIOP.ResponseMode.QUERY) {
uriResponse.encodedUri = encodeURI(`${didAuthResponseCall.redirectUri}?${params}`);
} else {
uriResponse.responseMode = SIOP.ResponseMode.FRAGMENT;
uriResponse.encodedUri = encodeURI(`${didAuthResponseCall.redirectUri}#${params}`);
}
return uriResponse;*/
}
/**
* Verifies a SIOP ID Response JWT on the RP Side
*
* @param jwt ID token to be validated
* @param verifyOpts
*/
static async verifyJWT(jwt: string, verifyOpts: VerifyAuthenticationResponseOpts): Promise<VerifiedAuthenticationResponseWithJWT> {
if (!jwt) {
throw new Error(SIOPErrors.NO_JWT);
}
assertValidVerifyOpts(verifyOpts);
const { header, payload } = DIDJwt.parseJWT(jwt);
assertValidResponseJWT({ header, payload });
const verifiedJWT = await verifyDidJWT(jwt, DIDres.getResolver(verifyOpts.verification.resolveOpts), {
audience: verifyOpts.audience,
});
const issuerDid = DIDJwt.getIssuerDidFromPayload(payload);
const verPayload = verifiedJWT.payload as AuthenticationResponsePayload;
assertValidResponseJWT({ header, verPayload: verPayload, audience: verifyOpts.audience });
await assertValidVerifiablePresentations(verifyOpts?.claims?.presentationDefinitions, verPayload);
return {
signer: verifiedJWT.signer,
didResolutionResult: verifiedJWT.didResolutionResult,
jwt,
verifyOpts,
issuer: issuerDid,
payload: {
...verPayload,
},
};
}
}
function assertValidResponseJWT(opts: {
header: JWTHeader;
payload?: JWT.JWTPayload;
verPayload?: AuthenticationResponsePayload;
audience?: string;
}) {
if (!opts.header) {
throw new Error(SIOPErrors.BAD_PARAMS);
}
if (opts.payload) {
if (opts.payload.iss !== SIOP.ResponseIss.SELF_ISSUED_V2) {
throw new Error(`${SIOPErrors.NO_SELFISSUED_ISS}, got: ${opts.payload.iss}`);
}
}
if (opts.verPayload) {
if (!opts.verPayload.nonce) {
throw Error(SIOPErrors.NO_NONCE);
} else if (!opts.verPayload.sub_type) {
throw Error(SIOPErrors.NO_SUB_TYPE);
} else if (!opts.verPayload.exp || opts.verPayload.exp < Date.now() / 1000) {
throw Error(SIOPErrors.EXPIRED);
/*} else if (!opts.verPayload.iat || opts.verPayload.iat > (Date.now() / 1000)) {
throw Error(SIOPErrors.EXPIRED);*/
// todo: Add iat check
}
if ((opts.verPayload.aud && !opts.audience) || (!opts.verPayload.aud && opts.audience)) {
throw Error(SIOPErrors.BAD_PARAMS);
} else if (opts.audience && opts.audience != opts.verPayload.aud) {
throw Error(SIOPErrors.INVALID_AUDIENCE);
}
}
}
async function createThumbprintAndJWK(resOpts: SIOP.AuthenticationResponseOpts): Promise<{ thumbprint: string; subJwk: JWK }> {
let thumbprint;
let subJwk;
if (SIOP.isInternalSignature(resOpts.signatureType)) {
thumbprint = getThumbprint(resOpts.signatureType.hexPrivateKey, resOpts.did);
subJwk = getPublicJWKFromHexPrivateKey(
resOpts.signatureType.hexPrivateKey,
resOpts.signatureType.kid || `${resOpts.signatureType.did}#key-1`,
resOpts.did
);
/* } else if (SIOP.isExternalSignature(resOpts.signatureType)) {
const didDocument = await fetchDidDocument(resOpts.registration.registrationBy.referenceUri as string);
if (!didDocument.verificationMethod || didDocument.verificationMethod.length == 0) {
throw Error(SIOPErrors.VERIFY_BAD_PARAMS);
}
thumbprint = getThumbprintFromJwk(didDocument.verificationMethod[0].publicKeyJwk as JWK, resOpts.did);
subJwk = didDocument.verificationMethod[0].publicKeyJwk as JWK;*/
} else if (SIOP.isSuppliedSignature(resOpts.signatureType)) {
return { thumbprint, subJwk };
} else {
throw new Error(SIOPErrors.SIGNATURE_OBJECT_TYPE_NOT_SET);
}
return { thumbprint, subJwk };
}
function extractPresentations(resOpts: SIOP.AuthenticationResponseOpts) {
const presentationPayloads =
resOpts.vp && resOpts.vp.length > 0
? resOpts.vp
.filter((vp) => vp.location === PresentationLocation.ID_TOKEN)
.map<VerifiablePresentationPayload>((vp) => vp as VerifiablePresentationPayload)
: undefined;
const vp_tokens =
resOpts.vp && resOpts.vp.length > 0
? resOpts.vp
.filter((vp) => vp.location === PresentationLocation.VP_TOKEN)
.map<VerifiablePresentationPayload>((vp) => vp as VerifiablePresentationPayload)
: undefined;
let vp_token;
if (vp_tokens) {
if (vp_tokens.length == 1) {
vp_token = vp_tokens[0];
} else if (vp_tokens.length > 1) {
throw new Error(SIOPErrors.REQUEST_CLAIMS_PRESENTATION_DEFINITION_NOT_VALID);
}
}
const verifiable_presentations = presentationPayloads && presentationPayloads.length > 0 ? presentationPayloads : undefined;
return {
verifiable_presentations,
vp_token,
};
}
async function createSIOPResponsePayload(
verifiedJwt: SIOP.VerifiedAuthenticationRequestWithJWT,
resOpts: SIOP.AuthenticationResponseOpts
): Promise<SIOP.AuthenticationResponsePayload> {
assertValidResponseOpts(resOpts);
if (!verifiedJwt || !verifiedJwt.jwt) {
throw new Error(SIOPErrors.VERIFY_BAD_PARAMS);
}
const isDidSupported = verifiedJwt.payload.registration?.subject_identifiers_supported?.includes(SubjectIdentifierType.DID);
const { thumbprint, subJwk } = await createThumbprintAndJWK(resOpts);
const state = resOpts.state || State.getState(verifiedJwt.payload.state);
const nonce = resOpts.nonce || State.getNonce(state, resOpts.nonce);
const registration = createDiscoveryMetadataPayload(resOpts.registration);
// *********************************************************************************
// todo We are missing a wrapper object. Actually the current object is the id_token
// *********************************************************************************
const { verifiable_presentations, vp_token } = extractPresentations(resOpts);
return {
iss: SIOP.ResponseIss.SELF_ISSUED_V2,
sub: isDidSupported && resOpts.did ? resOpts.did : thumbprint,
aud: verifiedJwt.payload.redirect_uri,
did: resOpts.did,
sub_type: isDidSupported && resOpts.did ? SubjectIdentifierType.DID : SubjectIdentifierType.JKT,
sub_jwk: subJwk,
state,
nonce,
iat: Date.now() / 1000,
exp: Date.now() / 1000 + (resOpts.expiresIn || 600),
registration,
vp_token,
verifiable_presentations,
};
}
function assertValidResponseOpts(opts: SIOP.AuthenticationResponseOpts) {
if (!opts /*|| !opts.redirectUri*/ || !opts.signatureType /*|| !opts.nonce*/ || !opts.did) {
throw new Error(SIOPErrors.BAD_PARAMS);
} else if (
!(SIOP.isInternalSignature(opts.signatureType) || SIOP.isExternalSignature(opts.signatureType) || SIOP.isSuppliedSignature(opts.signatureType))
) {
throw new Error(SIOPErrors.SIGNATURE_OBJECT_TYPE_NOT_SET);
}
}
function assertValidVerifyOpts(opts: SIOP.VerifyAuthenticationResponseOpts) {
if (!opts || !opts.verification || (!SIOP.isExternalVerification(opts.verification) && !SIOP.isInternalVerification(opts.verification))) {
throw new Error(SIOPErrors.VERIFY_BAD_PARAMS);
}
}
async function assertValidVerifiablePresentations(definitions: PresentationDefinitionWithLocation[], verPayload: AuthenticationResponsePayload) {
if ((!definitions || definitions.length == 0) && !verPayload) {
return;
}
// const definitions: PresentationDefinitionWithLocation[] = verifyOpts?.claims?.presentationDefinitions;
PresentationExchange.assertValidPresentationDefintionWithLocations(definitions);
let presentationPayloads: VerifiablePresentationPayload[];
if (verPayload.verifiable_presentations && verPayload.verifiable_presentations.length > 0) {
presentationPayloads = verPayload.verifiable_presentations;
}
if (verPayload.vp_token) {
if (!presentationPayloads) {
presentationPayloads = [verPayload.vp_token];
} else {
presentationPayloads.push(verPayload.vp_token);
}
}
/*console.log('pd:', JSON.stringify(definitions));
console.log('vps:', JSON.stringify(presentationPayloads));*/
if (definitions && !presentationPayloads) {
throw new Error(SIOPErrors.AUTH_REQUEST_EXPECTS_VP);
} else if (!definitions && presentationPayloads) {
throw new Error(SIOPErrors.AUTH_REQUEST_DOESNT_EXPECT_VP);
} else if (definitions && presentationPayloads && definitions.length != presentationPayloads.length) {
throw new Error(SIOPErrors.AUTH_REQUEST_EXPECTS_VP);
} else if (definitions && presentationPayloads) {
await PresentationExchange.validatePayloadsAgainstDefinitions(definitions, presentationPayloads);
}
}