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
/
Opts.ts
executable file
·102 lines (93 loc) · 3.69 KB
/
Opts.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
import { Resolvable } from 'did-resolver';
import { VerifyAuthorizationRequestOpts } from '../authorization-request';
import { AuthorizationResponseOpts } from '../authorization-response';
import { getResolverUnion, mergeAllDidMethods } from '../did';
import { LanguageTagUtils } from '../helpers';
import { AuthorizationResponseOptsSchema } from '../schemas';
import { InternalVerification, PassBy, ResponseRegistrationOpts, VerificationMode } from '../types';
import { OPBuilder } from './OPBuilder';
export const createResponseOptsFromBuilderOrExistingOpts = (opts: {
builder?: OPBuilder;
responseOpts?: AuthorizationResponseOpts;
}): AuthorizationResponseOpts => {
if (opts?.builder?.resolvers.size && opts.builder?.responseRegistration?.subject_syntax_types_supported) {
opts.builder.responseRegistration.subject_syntax_types_supported = mergeAllDidMethods(
opts.builder.responseRegistration.subject_syntax_types_supported,
opts.builder.resolvers
);
}
let responseOpts: AuthorizationResponseOpts;
if (opts.builder) {
responseOpts = {
registration: {
issuer: opts.builder.issuer,
...(opts.builder.responseRegistration as ResponseRegistrationOpts),
},
expiresIn: opts.builder.expiresIn,
signature: opts.builder.signature,
responseMode: opts.builder.responseMode,
...(responseOpts?.version
? { version: responseOpts.version }
: Array.isArray(opts.builder.supportedVersions) && opts.builder.supportedVersions.length > 0
? { version: opts.builder.supportedVersions[0] }
: {}),
};
if (!responseOpts.registration.passBy) {
responseOpts.registration.passBy = PassBy.VALUE;
}
const languageTagEnabledFieldsNames = ['clientName', 'clientPurpose'];
const languageTaggedFields: Map<string, string> = LanguageTagUtils.getLanguageTaggedProperties(
opts.builder.responseRegistration,
languageTagEnabledFieldsNames
);
languageTaggedFields.forEach((value: string, key: string) => {
responseOpts.registration[key] = value;
});
} else {
responseOpts = {
...opts.responseOpts,
};
}
const valid = AuthorizationResponseOptsSchema(responseOpts);
if (!valid) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
throw new Error('OP builder validation error: ' + JSON.stringify(AuthorizationResponseOptsSchema.errors));
}
return responseOpts;
};
export const createVerifyRequestOptsFromBuilderOrExistingOpts = (opts: {
builder?: OPBuilder;
verifyOpts?: VerifyAuthorizationRequestOpts;
}): VerifyAuthorizationRequestOpts => {
if (opts?.builder?.resolvers.size && opts.builder?.responseRegistration) {
opts.builder.responseRegistration.subject_syntax_types_supported = mergeAllDidMethods(
opts.builder.responseRegistration.subject_syntax_types_supported,
opts.builder.resolvers
);
}
let resolver: Resolvable;
if (opts.builder) {
resolver = getResolverUnion(
opts.builder.customResolver,
opts.builder.responseRegistration.subject_syntax_types_supported,
opts.builder.resolvers
);
}
return opts.builder
? {
hasher: opts.builder.hasher,
verification: {
mode: VerificationMode.INTERNAL,
checkLinkedDomain: opts.builder.checkLinkedDomain,
wellknownDIDVerifyCallback: opts.builder.wellknownDIDVerifyCallback,
resolveOpts: {
subjectSyntaxTypesSupported: opts.builder.responseRegistration.subject_syntax_types_supported,
resolver: resolver,
},
} as InternalVerification,
supportedVersions: opts.builder.supportedVersions,
correlationId: undefined,
}
: opts.verifyOpts;
};