This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRP.ts
378 lines (352 loc) · 15.2 KB
/
RP.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { EventEmitter } from 'events';
import { Hasher } from '@sphereon/ssi-types';
import { v4 as uuidv4 } from 'uuid';
import {
AuthorizationRequest,
ClaimPayloadCommonOpts,
CreateAuthorizationRequestOpts,
PropertyTarget,
RequestObjectPayloadOpts,
RequestPropertyWithTargets,
URI,
} from '../authorization-request';
import { mergeVerificationOpts } from '../authorization-request/Opts';
import { AuthorizationResponse, PresentationDefinitionWithLocation, VerifyAuthorizationResponseOpts } from '../authorization-response';
import { getNonce, getState } from '../helpers';
import {
AuthorizationEvent,
AuthorizationEvents,
AuthorizationResponsePayload,
CheckLinkedDomain,
ExternalVerification,
InternalVerification,
PassBy,
RegisterEventListener,
ResponseURIType,
SIOPErrors,
SupportedVersion,
VerifiedAuthorizationResponse,
} from '../types';
import { createRequestOptsFromBuilderOrExistingOpts, createVerifyResponseOptsFromBuilderOrExistingOpts, isTargetOrNoTargets } from './Opts';
import { RPBuilder } from './RPBuilder';
import { IRPSessionManager } from './types';
export class RP {
get sessionManager(): IRPSessionManager {
return this._sessionManager;
}
private readonly _createRequestOptions: CreateAuthorizationRequestOpts;
private readonly _verifyResponseOptions: Partial<VerifyAuthorizationResponseOpts>;
private readonly _eventEmitter?: EventEmitter;
private readonly _sessionManager?: IRPSessionManager;
private constructor(opts: {
builder?: RPBuilder;
createRequestOpts?: CreateAuthorizationRequestOpts;
verifyResponseOpts?: VerifyAuthorizationResponseOpts;
}) {
// const claims = opts.builder?.claims || opts.createRequestOpts?.payload.claims;
this._createRequestOptions = createRequestOptsFromBuilderOrExistingOpts(opts);
this._verifyResponseOptions = { ...createVerifyResponseOptsFromBuilderOrExistingOpts(opts) };
this._eventEmitter = opts.builder?.eventEmitter;
this._sessionManager = opts.builder?.sessionManager;
}
public static fromRequestOpts(opts: CreateAuthorizationRequestOpts): RP {
return new RP({ createRequestOpts: opts });
}
public static builder(opts?: { requestVersion?: SupportedVersion }): RPBuilder {
return RPBuilder.newInstance(opts?.requestVersion);
}
public async createAuthorizationRequest(opts: {
correlationId: string;
nonce: string | RequestPropertyWithTargets<string>;
state: string | RequestPropertyWithTargets<string>;
claims?: ClaimPayloadCommonOpts | RequestPropertyWithTargets<ClaimPayloadCommonOpts>;
version?: SupportedVersion;
requestByReferenceURI?: string;
responseURI?: string;
responseURIType?: ResponseURIType;
}): Promise<AuthorizationRequest> {
const authorizationRequestOpts = this.newAuthorizationRequestOpts(opts);
return AuthorizationRequest.fromOpts(authorizationRequestOpts)
.then((authorizationRequest: AuthorizationRequest) => {
void this.emitEvent(AuthorizationEvents.ON_AUTH_REQUEST_CREATED_SUCCESS, {
correlationId: opts.correlationId,
subject: authorizationRequest,
});
return authorizationRequest;
})
.catch((error: Error) => {
void this.emitEvent(AuthorizationEvents.ON_AUTH_REQUEST_CREATED_FAILED, {
correlationId: opts.correlationId,
error,
});
throw error;
});
}
public async createAuthorizationRequestURI(opts: {
correlationId: string;
nonce: string | RequestPropertyWithTargets<string>;
state: string | RequestPropertyWithTargets<string>;
claims?: ClaimPayloadCommonOpts | RequestPropertyWithTargets<ClaimPayloadCommonOpts>;
version?: SupportedVersion;
requestByReferenceURI?: string;
responseURI?: string;
responseURIType?: ResponseURIType;
}): Promise<URI> {
const authorizationRequestOpts = this.newAuthorizationRequestOpts(opts);
return await URI.fromOpts(authorizationRequestOpts)
.then(async (uri: URI) => {
void this.emitEvent(AuthorizationEvents.ON_AUTH_REQUEST_CREATED_SUCCESS, {
correlationId: opts.correlationId,
subject: await AuthorizationRequest.fromOpts(authorizationRequestOpts),
});
return uri;
})
.catch((error: Error) => {
void this.emitEvent(AuthorizationEvents.ON_AUTH_REQUEST_CREATED_FAILED, {
correlationId: opts.correlationId,
error,
});
throw error;
});
}
public async signalAuthRequestRetrieved(opts: { correlationId: string; error?: Error }) {
if (!this.sessionManager) {
throw Error(`Cannot signal auth request retrieval when no session manager is registered`);
}
const state = await this.sessionManager.getRequestStateByCorrelationId(opts.correlationId, true);
void this.emitEvent(opts?.error ? AuthorizationEvents.ON_AUTH_REQUEST_SENT_FAILED : AuthorizationEvents.ON_AUTH_REQUEST_SENT_SUCCESS, {
correlationId: opts.correlationId,
...(!opts?.error ? { subject: state.request } : {}),
...(opts?.error ? { error: opts.error } : {}),
});
}
public async verifyAuthorizationResponse(
authorizationResponsePayload: AuthorizationResponsePayload,
opts?: {
correlationId?: string;
hasher?: Hasher;
audience?: string;
state?: string;
nonce?: string;
verification?: InternalVerification | ExternalVerification;
presentationDefinitions?: PresentationDefinitionWithLocation | PresentationDefinitionWithLocation[];
},
): Promise<VerifiedAuthorizationResponse> {
const state = opts?.state || this.verifyResponseOptions.state;
let correlationId: string | undefined = opts?.correlationId || state;
let authorizationResponse: AuthorizationResponse;
try {
authorizationResponse = await AuthorizationResponse.fromPayload(authorizationResponsePayload);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
void this.emitEvent(AuthorizationEvents.ON_AUTH_RESPONSE_RECEIVED_FAILED, {
correlationId: correlationId ?? uuidv4(), // correlation id cannot be derived from state in payload possible, hence a uuid as fallback
subject: authorizationResponsePayload,
error,
});
throw error;
}
try {
const verifyAuthenticationResponseOpts = await this.newVerifyAuthorizationResponseOpts(authorizationResponse, {
...opts,
correlationId,
});
correlationId = verifyAuthenticationResponseOpts.correlationId ?? correlationId;
void this.emitEvent(AuthorizationEvents.ON_AUTH_RESPONSE_RECEIVED_SUCCESS, {
correlationId,
subject: authorizationResponse,
});
const verifiedAuthorizationResponse = await authorizationResponse.verify(verifyAuthenticationResponseOpts);
void this.emitEvent(AuthorizationEvents.ON_AUTH_RESPONSE_VERIFIED_SUCCESS, {
correlationId,
subject: authorizationResponse,
});
return verifiedAuthorizationResponse;
} catch (error) {
void this.emitEvent(AuthorizationEvents.ON_AUTH_RESPONSE_VERIFIED_FAILED, {
correlationId,
subject: authorizationResponse,
error,
});
throw error;
}
}
get createRequestOptions(): CreateAuthorizationRequestOpts {
return this._createRequestOptions;
}
get verifyResponseOptions(): Partial<VerifyAuthorizationResponseOpts> {
return this._verifyResponseOptions;
}
private newAuthorizationRequestOpts(opts: {
correlationId: string;
nonce: string | RequestPropertyWithTargets<string>;
state: string | RequestPropertyWithTargets<string>;
claims?: ClaimPayloadCommonOpts | RequestPropertyWithTargets<ClaimPayloadCommonOpts>;
version?: SupportedVersion;
requestByReferenceURI?: string;
responseURIType?: ResponseURIType;
responseURI?: string;
}): CreateAuthorizationRequestOpts {
const nonceWithTarget =
typeof opts.nonce === 'string'
? { propertyValue: opts.nonce, targets: PropertyTarget.REQUEST_OBJECT }
: (opts?.nonce as RequestPropertyWithTargets<string>);
const stateWithTarget =
typeof opts.state === 'string'
? { propertyValue: opts.state, targets: PropertyTarget.REQUEST_OBJECT }
: (opts?.state as RequestPropertyWithTargets<string>);
const claimsWithTarget =
opts?.claims && !('propertyValue' in opts.claims)
? { propertyValue: opts.claims, targets: PropertyTarget.REQUEST_OBJECT }
: (opts?.claims as RequestPropertyWithTargets<ClaimPayloadCommonOpts>);
const version = opts?.version ?? this._createRequestOptions.version;
if (!version) {
throw Error(SIOPErrors.NO_REQUEST_VERSION);
}
const referenceURI = opts.requestByReferenceURI ?? this._createRequestOptions?.requestObject?.reference_uri;
let responseURIType: ResponseURIType = opts?.responseURIType;
let responseURI = this._createRequestOptions.requestObject.payload?.redirect_uri ?? this._createRequestOptions.payload?.redirect_uri;
if (responseURI) {
responseURIType = 'redirect_uri';
} else {
responseURI =
opts.responseURI ?? this._createRequestOptions.requestObject.payload?.response_uri ?? this._createRequestOptions.payload?.response_uri;
responseURIType = opts?.responseURIType ?? 'response_uri';
}
if (!responseURI) {
throw Error(`A response or redirect URI is required at this point`);
} else {
if (responseURIType === 'redirect_uri') {
if (this._createRequestOptions?.requestObject?.payload) {
this._createRequestOptions.requestObject.payload.redirect_uri = responseURI;
}
if (!referenceURI && !this._createRequestOptions.payload?.redirect_uri) {
this._createRequestOptions.payload.redirect_uri = responseURI;
}
} else if (responseURIType === 'response_uri') {
if (this._createRequestOptions?.requestObject?.payload) {
this._createRequestOptions.requestObject.payload.response_uri = responseURI;
}
if (!referenceURI && !this._createRequestOptions.payload?.response_uri) {
this._createRequestOptions.payload.response_uri = responseURI;
}
}
}
const newOpts = { ...this._createRequestOptions, version };
newOpts.requestObject.payload = newOpts.requestObject.payload ?? ({} as RequestObjectPayloadOpts<ClaimPayloadCommonOpts>);
newOpts.payload = newOpts.payload ?? {};
if (referenceURI) {
if (newOpts.requestObject.passBy && newOpts.requestObject.passBy !== PassBy.REFERENCE) {
throw Error(`Cannot pass by reference with uri ${referenceURI} when mode is ${newOpts.requestObject.passBy}`);
}
newOpts.requestObject.reference_uri = referenceURI;
newOpts.requestObject.passBy = PassBy.REFERENCE;
}
const state = getState(stateWithTarget.propertyValue);
if (stateWithTarget.propertyValue) {
if (isTargetOrNoTargets(PropertyTarget.AUTHORIZATION_REQUEST, stateWithTarget.targets)) {
newOpts.payload.state = state;
}
if (isTargetOrNoTargets(PropertyTarget.REQUEST_OBJECT, stateWithTarget.targets)) {
newOpts.requestObject.payload.state = state;
}
}
const nonce = getNonce(state, nonceWithTarget.propertyValue);
if (nonceWithTarget.propertyValue) {
if (isTargetOrNoTargets(PropertyTarget.AUTHORIZATION_REQUEST, nonceWithTarget.targets)) {
newOpts.payload.nonce = nonce;
}
if (isTargetOrNoTargets(PropertyTarget.REQUEST_OBJECT, nonceWithTarget.targets)) {
newOpts.requestObject.payload.nonce = nonce;
}
}
if (claimsWithTarget?.propertyValue) {
if (isTargetOrNoTargets(PropertyTarget.AUTHORIZATION_REQUEST, claimsWithTarget.targets)) {
newOpts.payload.claims = { ...newOpts.payload.claims, ...claimsWithTarget.propertyValue };
}
if (isTargetOrNoTargets(PropertyTarget.REQUEST_OBJECT, claimsWithTarget.targets)) {
newOpts.requestObject.payload.claims = { ...newOpts.requestObject.payload.claims, ...claimsWithTarget.propertyValue };
}
}
return newOpts;
}
private async newVerifyAuthorizationResponseOpts(
authorizationResponse: AuthorizationResponse,
opts: {
correlationId: string;
hasher?: Hasher;
state?: string;
nonce?: string;
verification?: InternalVerification | ExternalVerification;
audience?: string;
checkLinkedDomain?: CheckLinkedDomain;
presentationDefinitions?: PresentationDefinitionWithLocation | PresentationDefinitionWithLocation[];
},
): Promise<VerifyAuthorizationResponseOpts> {
let correlationId = opts?.correlationId ?? this._verifyResponseOptions.correlationId;
let state = opts?.state ?? this._verifyResponseOptions.state;
let nonce = opts?.nonce ?? this._verifyResponseOptions.nonce;
if (this.sessionManager) {
const resNonce = (await authorizationResponse.getMergedProperty('nonce', {
consistencyCheck: false,
hasher: opts.hasher ?? this._verifyResponseOptions.hasher,
})) as string;
const resState = (await authorizationResponse.getMergedProperty('state', {
consistencyCheck: false,
hasher: opts.hasher ?? this._verifyResponseOptions.hasher,
})) as string;
if (resNonce && !correlationId) {
correlationId = await this.sessionManager.getCorrelationIdByNonce(resNonce, false);
}
if (!correlationId) {
correlationId = await this.sessionManager.getCorrelationIdByState(resState, false);
}
if (!correlationId) {
correlationId = nonce;
}
const requestState = await this.sessionManager.getRequestStateByCorrelationId(correlationId, false);
if (requestState) {
const reqNonce: string = await requestState.request.getMergedProperty('nonce');
const reqState: string = await requestState.request.getMergedProperty('state');
nonce = nonce ?? reqNonce;
state = state ?? reqState;
}
}
return {
...this._verifyResponseOptions,
...opts,
correlationId,
audience:
opts?.audience ??
this._verifyResponseOptions.audience ??
this._verifyResponseOptions.verification.resolveOpts.jwtVerifyOpts.audience ??
this._createRequestOptions.payload.client_id,
state,
nonce,
verification: mergeVerificationOpts(this._verifyResponseOptions, opts),
presentationDefinitions: opts?.presentationDefinitions ?? this._verifyResponseOptions.presentationDefinitions,
};
}
private async emitEvent(
type: AuthorizationEvents,
payload: { correlationId: string; subject?: AuthorizationRequest | AuthorizationResponse | AuthorizationResponsePayload; error?: Error },
): Promise<void> {
if (this._eventEmitter) {
try {
this._eventEmitter.emit(type, new AuthorizationEvent(payload));
} catch (e) {
//Let's make sure events do not cause control flow issues
console.log(`Could not emit event ${type} for ${payload.correlationId} initial error if any: ${payload?.error}`);
}
}
}
public addEventListener(register: RegisterEventListener) {
if (!this._eventEmitter) {
throw Error('Cannot add listeners if no event emitter is available');
}
const events = Array.isArray(register.event) ? register.event : [register.event];
for (const event of events) {
this._eventEmitter.addListener(event, register.listener);
}
}
}