@sphereon/oid4vc-common 0.17.0 → 0.17.1-feature.esm.cjs.24
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/index.cjs +485 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +297 -0
- package/dist/index.d.ts +296 -8
- package/dist/index.js +425 -26
- package/dist/index.js.map +1 -1
- package/package.json +22 -13
- package/dist/dpop/DPoP.d.ts +0 -60
- package/dist/dpop/DPoP.d.ts.map +0 -1
- package/dist/dpop/DPoP.js +0 -184
- package/dist/dpop/DPoP.js.map +0 -1
- package/dist/dpop/index.d.ts +0 -2
- package/dist/dpop/index.d.ts.map +0 -1
- package/dist/dpop/index.js +0 -18
- package/dist/dpop/index.js.map +0 -1
- package/dist/hasher.d.ts +0 -3
- package/dist/hasher.d.ts.map +0 -1
- package/dist/hasher.js +0 -19
- package/dist/hasher.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/jwt/Jwk.types.d.ts +0 -36
- package/dist/jwt/Jwk.types.d.ts.map +0 -1
- package/dist/jwt/Jwk.types.js +0 -3
- package/dist/jwt/Jwk.types.js.map +0 -1
- package/dist/jwt/JwkThumbprint.d.ts +0 -6
- package/dist/jwt/JwkThumbprint.d.ts.map +0 -1
- package/dist/jwt/JwkThumbprint.js +0 -102
- package/dist/jwt/JwkThumbprint.js.map +0 -1
- package/dist/jwt/Jwt.types.d.ts +0 -23
- package/dist/jwt/Jwt.types.d.ts.map +0 -1
- package/dist/jwt/Jwt.types.js +0 -12
- package/dist/jwt/Jwt.types.js.map +0 -1
- package/dist/jwt/JwtIssuer.d.ts +0 -51
- package/dist/jwt/JwtIssuer.d.ts.map +0 -1
- package/dist/jwt/JwtIssuer.js +0 -3
- package/dist/jwt/JwtIssuer.js.map +0 -1
- package/dist/jwt/JwtVerifier.d.ts +0 -72
- package/dist/jwt/JwtVerifier.d.ts.map +0 -1
- package/dist/jwt/JwtVerifier.js +0 -80
- package/dist/jwt/JwtVerifier.js.map +0 -1
- package/dist/jwt/index.d.ts +0 -7
- package/dist/jwt/index.d.ts.map +0 -1
- package/dist/jwt/index.js +0 -23
- package/dist/jwt/index.js.map +0 -1
- package/dist/jwt/jwtUtils.d.ts +0 -26
- package/dist/jwt/jwtUtils.d.ts.map +0 -1
- package/dist/jwt/jwtUtils.js +0 -62
- package/dist/jwt/jwtUtils.js.map +0 -1
- package/dist/oauth/DynamicClientRegistration.types.d.ts +0 -19
- package/dist/oauth/DynamicClientRegistration.types.d.ts.map +0 -1
- package/dist/oauth/DynamicClientRegistration.types.js +0 -3
- package/dist/oauth/DynamicClientRegistration.types.js.map +0 -1
- package/dist/oauth/index.d.ts +0 -2
- package/dist/oauth/index.d.ts.map +0 -1
- package/dist/oauth/index.js +0 -18
- package/dist/oauth/index.js.map +0 -1
- package/dist/types/digest.types.d.ts +0 -2
- package/dist/types/digest.types.d.ts.map +0 -1
- package/dist/types/digest.types.js +0 -3
- package/dist/types/digest.types.js.map +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -18
- package/dist/types/index.js.map +0 -1
- package/lib/__tests__/dpop.spec.ts +0 -124
- package/lib/dpop/DPoP.ts +0 -242
- package/lib/dpop/index.ts +0 -1
- package/lib/hasher.ts +0 -18
- package/lib/index.ts +0 -11
- package/lib/jwt/Jwk.types.ts +0 -38
- package/lib/jwt/JwkThumbprint.ts +0 -65
- package/lib/jwt/Jwt.types.ts +0 -26
- package/lib/jwt/JwtIssuer.ts +0 -60
- package/lib/jwt/JwtVerifier.ts +0 -130
- package/lib/jwt/__tests__/JwkThumbprint.spec.ts +0 -16
- package/lib/jwt/index.ts +0 -6
- package/lib/jwt/jwtUtils.ts +0 -71
- package/lib/oauth/DynamicClientRegistration.types.ts +0 -20
- package/lib/oauth/index.ts +0 -1
- package/lib/types/digest.types.ts +0 -1
- package/lib/types/index.ts +0 -1
package/lib/dpop/DPoP.ts
DELETED
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import { jwtDecode } from 'jwt-decode';
|
|
2
|
-
import * as u8a from 'uint8arrays';
|
|
3
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
-
|
|
5
|
-
import { defaultHasher } from '../hasher';
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
calculateJwkThumbprint,
|
|
9
|
-
CreateJwtCallback,
|
|
10
|
-
epochTime,
|
|
11
|
-
getNowSkewed,
|
|
12
|
-
JWK,
|
|
13
|
-
JwtHeader,
|
|
14
|
-
JwtIssuerJwk,
|
|
15
|
-
JwtPayload,
|
|
16
|
-
parseJWT,
|
|
17
|
-
SigningAlgo,
|
|
18
|
-
VerifyJwtCallbackBase,
|
|
19
|
-
} from './../jwt';
|
|
20
|
-
|
|
21
|
-
export const dpopTokenRequestNonceError = 'use_dpop_nonce';
|
|
22
|
-
|
|
23
|
-
export interface DPoPJwtIssuerWithContext extends JwtIssuerJwk {
|
|
24
|
-
type: 'dpop';
|
|
25
|
-
dPoPSigningAlgValuesSupported?: string[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type DPoPJwtPayloadProps = {
|
|
29
|
-
htu: string;
|
|
30
|
-
iat: number;
|
|
31
|
-
htm: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT' | 'PATCH';
|
|
32
|
-
ath?: string;
|
|
33
|
-
nonce?: string;
|
|
34
|
-
jti: string;
|
|
35
|
-
};
|
|
36
|
-
export type DPoPJwtHeaderProps = { typ: 'dpop+jwt'; alg: SigningAlgo; jwk: JWK };
|
|
37
|
-
export type CreateDPoPJwtPayloadProps = Omit<DPoPJwtPayloadProps, 'iat' | 'jti' | 'ath'> & { accessToken?: string };
|
|
38
|
-
|
|
39
|
-
export interface CreateDPoPOpts<JwtPayloadProps = CreateDPoPJwtPayloadProps> {
|
|
40
|
-
createJwtCallback: CreateJwtCallback<DPoPJwtIssuerWithContext>;
|
|
41
|
-
jwtIssuer: Omit<JwtIssuerJwk, 'method' | 'type'>;
|
|
42
|
-
jwtPayloadProps: Record<string, unknown> & JwtPayloadProps;
|
|
43
|
-
dPoPSigningAlgValuesSupported?: (string | SigningAlgo)[];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type CreateDPoPClientOpts = CreateDPoPOpts<Omit<CreateDPoPJwtPayloadProps, 'htm' | 'htu'>>;
|
|
47
|
-
|
|
48
|
-
export function getCreateDPoPOptions(
|
|
49
|
-
createDPoPClientOpts: CreateDPoPClientOpts,
|
|
50
|
-
endPointUrl: string,
|
|
51
|
-
resourceRequestOpts?: { accessToken: string },
|
|
52
|
-
): CreateDPoPOpts {
|
|
53
|
-
const htu = endPointUrl.split('?')[0].split('#')[0];
|
|
54
|
-
return {
|
|
55
|
-
...createDPoPClientOpts,
|
|
56
|
-
jwtPayloadProps: {
|
|
57
|
-
...createDPoPClientOpts.jwtPayloadProps,
|
|
58
|
-
htu,
|
|
59
|
-
htm: 'POST',
|
|
60
|
-
...(resourceRequestOpts && { accessToken: resourceRequestOpts.accessToken }),
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export async function createDPoP(options: CreateDPoPOpts): Promise<string> {
|
|
66
|
-
const { createJwtCallback, jwtIssuer, jwtPayloadProps, dPoPSigningAlgValuesSupported } = options;
|
|
67
|
-
|
|
68
|
-
if (jwtPayloadProps.accessToken && (jwtPayloadProps.accessToken?.startsWith('DPoP ') || jwtPayloadProps.accessToken?.startsWith('Bearer '))) {
|
|
69
|
-
throw new Error('expected access token without scheme');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const ath = jwtPayloadProps.accessToken ? u8a.toString(defaultHasher(jwtPayloadProps.accessToken, 'sha256'), 'base64url') : undefined;
|
|
73
|
-
return createJwtCallback(
|
|
74
|
-
{ method: 'jwk', type: 'dpop', alg: jwtIssuer.alg, jwk: jwtIssuer.jwk, dPoPSigningAlgValuesSupported },
|
|
75
|
-
{
|
|
76
|
-
header: { ...jwtIssuer, typ: 'dpop+jwt', alg: jwtIssuer.alg, jwk: jwtIssuer.jwk },
|
|
77
|
-
payload: {
|
|
78
|
-
...jwtPayloadProps,
|
|
79
|
-
iat: epochTime(),
|
|
80
|
-
jti: uuidv4(),
|
|
81
|
-
...(ath && { ath }),
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export type DPoPVerifyJwtCallback = VerifyJwtCallbackBase<JwtIssuerJwk & { type: 'dpop' }>;
|
|
88
|
-
export interface DPoPVerifyOptions {
|
|
89
|
-
expectedNonce?: string;
|
|
90
|
-
acceptedAlgorithms?: (string | SigningAlgo)[];
|
|
91
|
-
// defaults to 300 seconds (5 minutes)
|
|
92
|
-
maxIatAgeInSeconds?: number;
|
|
93
|
-
expectAccessToken?: boolean;
|
|
94
|
-
jwtVerifyCallback: DPoPVerifyJwtCallback;
|
|
95
|
-
now?: number;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export async function verifyDPoP(
|
|
99
|
-
request: { headers: Record<string, string | string[] | undefined>; fullUrl: string } & Pick<Request, 'method'>,
|
|
100
|
-
options: DPoPVerifyOptions,
|
|
101
|
-
) {
|
|
102
|
-
// There is not more than one DPoP HTTP request header field.
|
|
103
|
-
const dpop = request.headers['dpop'];
|
|
104
|
-
if (!dpop || typeof dpop !== 'string') {
|
|
105
|
-
throw new Error('missing or invalid dpop header. Expected compact JWT');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// The DPoP HTTP request header field value is a single and well-formed JWT.
|
|
109
|
-
const { header: dPoPHeader, payload: dPoPPayload } = parseJWT<JwtHeader, JwtPayload & Partial<DPoPJwtPayloadProps>>(dpop);
|
|
110
|
-
|
|
111
|
-
// Ensure all required header claims are present
|
|
112
|
-
if (dPoPHeader.typ !== 'dpop+jwt' || !dPoPHeader.alg || !dPoPHeader.jwk || typeof dPoPHeader.jwk !== 'object' || dPoPHeader.jwk.d) {
|
|
113
|
-
throw new Error('invalid_dpop_proof. Invalid header claims');
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Ensure all required payload claims are present
|
|
117
|
-
if (!dPoPPayload.htm || !dPoPPayload.htu || !dPoPPayload.iat || !dPoPPayload.jti) {
|
|
118
|
-
throw new Error('invalid_dpop_proof. Missing required claims');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Validate alg is supported
|
|
122
|
-
if (options?.acceptedAlgorithms && !options.acceptedAlgorithms.includes(dPoPHeader.alg)) {
|
|
123
|
-
throw new Error(`invalid_dpop_proof. Invalid 'alg' claim '${dPoPHeader.alg}'. Only ${options.acceptedAlgorithms.join(', ')} are supported.`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Validate nonce if provided
|
|
127
|
-
if ((options?.expectedNonce && !dPoPPayload.nonce) || dPoPPayload.nonce !== options.expectedNonce) {
|
|
128
|
-
throw new Error('invalid_dpop_proof. Nonce mismatch');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Verify JWT signature
|
|
132
|
-
try {
|
|
133
|
-
const verificationResult = await options.jwtVerifyCallback(
|
|
134
|
-
{
|
|
135
|
-
method: 'jwk',
|
|
136
|
-
type: 'dpop',
|
|
137
|
-
jwk: dPoPHeader.jwk,
|
|
138
|
-
alg: dPoPHeader.alg,
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
header: dPoPHeader,
|
|
142
|
-
payload: dPoPPayload,
|
|
143
|
-
raw: dpop,
|
|
144
|
-
},
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
if (!verificationResult) {
|
|
148
|
-
throw new Error('invalid_dpop_proof. Invalid JWT signature');
|
|
149
|
-
}
|
|
150
|
-
} catch (error: unknown) {
|
|
151
|
-
throw new Error('invalid_dpop_proof. Invalid JWT signature. ' + (error instanceof Error ? error.message : 'Unknown error'));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Validate htm claim
|
|
155
|
-
if (dPoPPayload.htm !== request.method) {
|
|
156
|
-
throw new Error(`invalid_dpop_proof. Invalid htm claim. Must match request method '${request.method}'`);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// The htu claim matches the HTTP URI value for the HTTP request in which the JWT was received, ignoring any query and fragment parts.
|
|
160
|
-
const currentUri = request.fullUrl.split('?')[0].split('#')[0];
|
|
161
|
-
if (dPoPPayload.htu !== currentUri) {
|
|
162
|
-
throw new Error('invalid_dpop_proof. Invalid htu claim');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Validate nonce if provided
|
|
166
|
-
if ((options.expectedNonce && dPoPPayload.nonce !== options.expectedNonce) || (!options.expectedNonce && dPoPPayload.nonce)) {
|
|
167
|
-
throw new Error('invalid_dpop_proof. Nonce mismatch');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Validate iat claim
|
|
171
|
-
const { nowSkewedPast, nowSkewedFuture } = getNowSkewed(options.now);
|
|
172
|
-
if (
|
|
173
|
-
// iat claim is too far in the future
|
|
174
|
-
nowSkewedPast - (options.maxIatAgeInSeconds ?? 60) > dPoPPayload.iat ||
|
|
175
|
-
// iat claim is too old
|
|
176
|
-
nowSkewedFuture + (options.maxIatAgeInSeconds ?? 60) < dPoPPayload.iat
|
|
177
|
-
) {
|
|
178
|
-
// 5 minute window
|
|
179
|
-
throw new Error('invalid_dpop_proof. Invalid iat claim');
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// If access token is present, validate ath claim
|
|
183
|
-
const authorizationHeader = request.headers.authorization;
|
|
184
|
-
if (!options.expectAccessToken && authorizationHeader) {
|
|
185
|
-
throw new Error('invalid_dpop_proof. Received an unexpected authorization header.');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (options.expectAccessToken) {
|
|
189
|
-
if (!dPoPPayload.ath) {
|
|
190
|
-
throw new Error('invalid_dpop_proof. Missing expected ath claim.');
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// validate that the DPOP proof is made for the provided access token
|
|
194
|
-
if (!authorizationHeader || typeof authorizationHeader !== 'string' || !authorizationHeader.startsWith('DPoP ')) {
|
|
195
|
-
throw new Error('invalid_dpop_proof. Invalid authorization header.');
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
const accessToken = authorizationHeader.replace('DPoP ', '');
|
|
199
|
-
const expectedAth = u8a.toString(defaultHasher(accessToken, 'sha256'), 'base64url');
|
|
200
|
-
if (dPoPPayload.ath !== expectedAth) {
|
|
201
|
-
throw new Error('invalid_dpop_proof. Invalid ath claim');
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// validate that the access token is signed with the same key as the DPOP proof
|
|
205
|
-
const accessTokenPayload = jwtDecode<JwtPayload & { cnf?: { jkt?: string } }>(accessToken, { header: false });
|
|
206
|
-
if (!accessTokenPayload.cnf?.jkt) {
|
|
207
|
-
throw new Error('invalid_dpop_proof. Access token is missing the jkt claim');
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const thumprint = await calculateJwkThumbprint(dPoPHeader.jwk, 'sha256');
|
|
211
|
-
if (accessTokenPayload.cnf?.jkt !== thumprint) {
|
|
212
|
-
throw new Error('invalid_dpop_proof. JwkThumbprint mismatch');
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// If all validations pass, return the dpop jwk
|
|
217
|
-
return dPoPHeader.jwk;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* DPoP verifications for resource requests
|
|
222
|
-
* For Bearer token compatibility jwt's must have a token_type claim
|
|
223
|
-
* The access token itself must be validated before using this method
|
|
224
|
-
* If the token_type is not DPoP, then the request is not a DPoP request
|
|
225
|
-
* and we don't need to verify the DPoP proof
|
|
226
|
-
*/
|
|
227
|
-
export async function verifyResourceDPoP(
|
|
228
|
-
request: { headers: Record<string, string | string[] | undefined>; fullUrl: string } & Pick<Request, 'method'>,
|
|
229
|
-
options: Omit<DPoPVerifyOptions, 'expectAccessToken'>,
|
|
230
|
-
) {
|
|
231
|
-
if (!request.headers.authorization || typeof request.headers.authorization !== 'string') {
|
|
232
|
-
throw new Error('Received an invalid resource request. Missing authorization header.');
|
|
233
|
-
}
|
|
234
|
-
const tokenPayload = jwtDecode<JwtPayload & { token_type?: string }>(request.headers.authorization, { header: false });
|
|
235
|
-
const tokenType = tokenPayload.token_type;
|
|
236
|
-
|
|
237
|
-
if (tokenType !== 'DPoP') {
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return verifyDPoP(request, { ...options, expectAccessToken: true });
|
|
242
|
-
}
|
package/lib/dpop/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './DPoP';
|
package/lib/hasher.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Hasher } from '@sphereon/ssi-types';
|
|
2
|
-
import sha from 'sha.js';
|
|
3
|
-
|
|
4
|
-
const supportedAlgorithms = ['sha256', 'sha384', 'sha512'] as const;
|
|
5
|
-
type SupportedAlgorithms = (typeof supportedAlgorithms)[number];
|
|
6
|
-
|
|
7
|
-
export const defaultHasher: Hasher = (data, algorithm) => {
|
|
8
|
-
const sanitizedAlgorithm = algorithm.toLowerCase().replace(/[-_]/g, '');
|
|
9
|
-
if (!supportedAlgorithms.includes(sanitizedAlgorithm as SupportedAlgorithms)) {
|
|
10
|
-
throw new Error(`Unsupported hashing algorithm ${algorithm}`);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return new Uint8Array(
|
|
14
|
-
sha(sanitizedAlgorithm as SupportedAlgorithms)
|
|
15
|
-
.update(data)
|
|
16
|
-
.digest(),
|
|
17
|
-
);
|
|
18
|
-
};
|
package/lib/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Loggers } from '@sphereon/ssi-types';
|
|
2
|
-
|
|
3
|
-
export const VCI_LOGGERS = Loggers.DEFAULT;
|
|
4
|
-
export const VCI_LOG_COMMON = VCI_LOGGERS.get('sphereon:oid4vci:common');
|
|
5
|
-
|
|
6
|
-
export * from './jwt';
|
|
7
|
-
export * from './dpop';
|
|
8
|
-
export * from './oauth';
|
|
9
|
-
|
|
10
|
-
export { v4 as uuidv4 } from 'uuid';
|
|
11
|
-
export { defaultHasher } from './hasher';
|
package/lib/jwt/Jwk.types.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export interface BaseJWK {
|
|
2
|
-
kty?: string;
|
|
3
|
-
crv?: string;
|
|
4
|
-
x?: string;
|
|
5
|
-
y?: string;
|
|
6
|
-
e?: string;
|
|
7
|
-
n?: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface JWK extends BaseJWK {
|
|
11
|
-
alg?: string;
|
|
12
|
-
d?: string;
|
|
13
|
-
dp?: string;
|
|
14
|
-
dq?: string;
|
|
15
|
-
ext?: boolean;
|
|
16
|
-
k?: string;
|
|
17
|
-
key_ops?: string[];
|
|
18
|
-
kid?: string;
|
|
19
|
-
oth?: Array<{
|
|
20
|
-
d?: string;
|
|
21
|
-
r?: string;
|
|
22
|
-
t?: string;
|
|
23
|
-
}>;
|
|
24
|
-
p?: string;
|
|
25
|
-
q?: string;
|
|
26
|
-
qi?: string;
|
|
27
|
-
use?: string;
|
|
28
|
-
x5c?: string[];
|
|
29
|
-
x5t?: string;
|
|
30
|
-
'x5t#S256'?: string;
|
|
31
|
-
x5u?: string;
|
|
32
|
-
|
|
33
|
-
[propName: string]: unknown;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type JWKS = {
|
|
37
|
-
keys: JWK[];
|
|
38
|
-
};
|
package/lib/jwt/JwkThumbprint.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import * as u8a from 'uint8arrays';
|
|
2
|
-
|
|
3
|
-
import { defaultHasher } from '../hasher';
|
|
4
|
-
import { DigestAlgorithm } from '../types';
|
|
5
|
-
|
|
6
|
-
import { JWK } from '.';
|
|
7
|
-
|
|
8
|
-
const check = (value: unknown, description: string) => {
|
|
9
|
-
if (typeof value !== 'string' || !value) {
|
|
10
|
-
throw Error(`${description} missing or invalid`);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export async function calculateJwkThumbprint(jwk: JWK, digestAlgorithm?: DigestAlgorithm): Promise<string> {
|
|
15
|
-
if (!jwk || typeof jwk !== 'object') {
|
|
16
|
-
throw new TypeError('JWK must be an object');
|
|
17
|
-
}
|
|
18
|
-
const algorithm = digestAlgorithm ?? 'sha256';
|
|
19
|
-
if (algorithm !== 'sha256' && algorithm !== 'sha384' && algorithm !== 'sha512') {
|
|
20
|
-
throw new TypeError('digestAlgorithm must one of "sha256", "sha384", or "sha512"');
|
|
21
|
-
}
|
|
22
|
-
let components;
|
|
23
|
-
switch (jwk.kty) {
|
|
24
|
-
case 'EC':
|
|
25
|
-
check(jwk.crv, '"crv" (Curve) Parameter');
|
|
26
|
-
check(jwk.x, '"x" (X Coordinate) Parameter');
|
|
27
|
-
check(jwk.y, '"y" (Y Coordinate) Parameter');
|
|
28
|
-
components = { crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y };
|
|
29
|
-
break;
|
|
30
|
-
case 'OKP':
|
|
31
|
-
check(jwk.crv, '"crv" (Subtype of Key Pair) Parameter');
|
|
32
|
-
check(jwk.x, '"x" (Public Key) Parameter');
|
|
33
|
-
components = { crv: jwk.crv, kty: jwk.kty, x: jwk.x };
|
|
34
|
-
break;
|
|
35
|
-
case 'RSA':
|
|
36
|
-
check(jwk.e, '"e" (Exponent) Parameter');
|
|
37
|
-
check(jwk.n, '"n" (Modulus) Parameter');
|
|
38
|
-
components = { e: jwk.e, kty: jwk.kty, n: jwk.n };
|
|
39
|
-
break;
|
|
40
|
-
case 'oct':
|
|
41
|
-
check(jwk.k, '"k" (Key Value) Parameter');
|
|
42
|
-
components = { k: jwk.k, kty: jwk.kty };
|
|
43
|
-
break;
|
|
44
|
-
default:
|
|
45
|
-
throw Error('"kty" (Key Type) Parameter missing or unsupported');
|
|
46
|
-
}
|
|
47
|
-
return u8a.toString(defaultHasher(JSON.stringify(components), algorithm), 'base64url');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export async function getDigestAlgorithmFromJwkThumbprintUri(uri: string): Promise<DigestAlgorithm> {
|
|
51
|
-
const match = uri.match(/^urn:ietf:params:oauth:jwk-thumbprint:sha-(\w+):/);
|
|
52
|
-
if (!match) {
|
|
53
|
-
throw new Error(`Invalid JWK thumbprint URI structure ${uri}`);
|
|
54
|
-
}
|
|
55
|
-
const algorithm = `sha${match[1]}` as DigestAlgorithm;
|
|
56
|
-
if (algorithm !== 'sha256' && algorithm !== 'sha384' && algorithm !== 'sha512') {
|
|
57
|
-
throw new Error(`Invalid JWK thumbprint URI digest algorithm ${uri}`);
|
|
58
|
-
}
|
|
59
|
-
return algorithm;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function calculateJwkThumbprintUri(jwk: JWK, digestAlgorithm: DigestAlgorithm = 'sha256'): Promise<string> {
|
|
63
|
-
const thumbprint = await calculateJwkThumbprint(jwk, digestAlgorithm);
|
|
64
|
-
return `urn:ietf:params:oauth:jwk-thumbprint:sha-${digestAlgorithm.slice(-3)}:${thumbprint}`;
|
|
65
|
-
}
|
package/lib/jwt/Jwt.types.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { JwtHeader as jwtDecodeJwtHeader, JwtPayload as jwtDecodePayload } from 'jwt-decode';
|
|
2
|
-
|
|
3
|
-
import { JWK } from '.';
|
|
4
|
-
|
|
5
|
-
export type JwtHeader = jwtDecodeJwtHeader & {
|
|
6
|
-
alg?: string;
|
|
7
|
-
x5c?: string[];
|
|
8
|
-
kid?: string;
|
|
9
|
-
jwk?: JWK;
|
|
10
|
-
jwt?: string;
|
|
11
|
-
} & Record<string, unknown>;
|
|
12
|
-
|
|
13
|
-
export type JwtPayload = jwtDecodePayload & {
|
|
14
|
-
client_id?: string;
|
|
15
|
-
nonce?: string;
|
|
16
|
-
request_uri?: string;
|
|
17
|
-
client_id_scheme?: string;
|
|
18
|
-
} & Record<string, unknown>;
|
|
19
|
-
|
|
20
|
-
export enum SigningAlgo {
|
|
21
|
-
EDDSA = 'EdDSA',
|
|
22
|
-
RS256 = 'RS256',
|
|
23
|
-
PS256 = 'PS256',
|
|
24
|
-
ES256 = 'ES256',
|
|
25
|
-
ES256K = 'ES256K',
|
|
26
|
-
}
|
package/lib/jwt/JwtIssuer.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { JWK, JwtHeader, JwtPayload, JwtProtectionMethod, SigningAlgo } from '..';
|
|
2
|
-
|
|
3
|
-
export interface JwtIssuerBase {
|
|
4
|
-
method: JwtProtectionMethod;
|
|
5
|
-
/**
|
|
6
|
-
* Additional options for the issuance context
|
|
7
|
-
*/
|
|
8
|
-
options?: Record<string, unknown>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface JwtIssuerDid extends JwtIssuerBase {
|
|
12
|
-
method: 'did';
|
|
13
|
-
didUrl: string;
|
|
14
|
-
alg: SigningAlgo | string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface JwtIssuerX5c extends JwtIssuerBase {
|
|
18
|
-
method: 'x5c';
|
|
19
|
-
alg: SigningAlgo | string;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
*
|
|
23
|
-
* Array of base64-encoded certificate strings in the DER-format.
|
|
24
|
-
*
|
|
25
|
-
* The certificate containing the public key corresponding to the key used to digitally sign the JWS MUST be the first certificate.
|
|
26
|
-
*/
|
|
27
|
-
x5c: Array<string>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The issuer jwt
|
|
31
|
-
*
|
|
32
|
-
* This value will be used as the iss value of the issue jwt.
|
|
33
|
-
* It is also used as the client_id.
|
|
34
|
-
* And will also be set as the redirect_uri
|
|
35
|
-
*
|
|
36
|
-
* It must match an entry in the x5c certificate leaf entry dnsName / uriName
|
|
37
|
-
*/
|
|
38
|
-
issuer: string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface JwtIssuerJwk extends JwtIssuerBase {
|
|
42
|
-
method: 'jwk';
|
|
43
|
-
alg: SigningAlgo | string;
|
|
44
|
-
jwk: JWK;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface JwtIssuerCustom extends JwtIssuerBase {
|
|
48
|
-
method: 'custom';
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type JwtIssuer = JwtIssuerDid | JwtIssuerX5c | JwtIssuerJwk | JwtIssuerCustom;
|
|
52
|
-
|
|
53
|
-
export interface JwtIssuanceContextBase {
|
|
54
|
-
type: string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type CreateJwtCallback<T extends JwtIssuer & JwtIssuanceContextBase> = (
|
|
58
|
-
jwtIssuer: T,
|
|
59
|
-
jwt: { header: JwtHeader; payload: JwtPayload },
|
|
60
|
-
) => Promise<string>;
|
package/lib/jwt/JwtVerifier.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { JWK, JwtHeader, JwtPayload, SigningAlgo } from '..';
|
|
2
|
-
|
|
3
|
-
import { JwtProtectionMethod, JwtType } from './jwtUtils';
|
|
4
|
-
|
|
5
|
-
export interface JwtVerifierBase {
|
|
6
|
-
type: JwtType;
|
|
7
|
-
method: JwtProtectionMethod;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface DidJwtVerifier extends JwtVerifierBase {
|
|
11
|
-
method: 'did';
|
|
12
|
-
|
|
13
|
-
alg: SigningAlgo | string;
|
|
14
|
-
didUrl: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface X5cJwtVerifier extends JwtVerifierBase {
|
|
18
|
-
method: 'x5c';
|
|
19
|
-
|
|
20
|
-
alg: SigningAlgo | string;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* Array of base64-encoded certificate strings in the DER-format.
|
|
25
|
-
*
|
|
26
|
-
* The certificate containing the public key corresponding to the key used to digitally sign the JWS MUST be the first certificate.
|
|
27
|
-
*/
|
|
28
|
-
x5c: Array<string>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The jwt issuer
|
|
32
|
-
*/
|
|
33
|
-
issuer: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface OpenIdFederationJwtVerifier extends JwtVerifierBase {
|
|
37
|
-
method: 'openid-federation';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The OpenId federation Entity
|
|
41
|
-
*/
|
|
42
|
-
entityId: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface JwkJwtVerifier extends JwtVerifierBase {
|
|
46
|
-
method: 'jwk';
|
|
47
|
-
alg: SigningAlgo | string;
|
|
48
|
-
|
|
49
|
-
jwk: JWK;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface CustomJwtVerifier extends JwtVerifierBase {
|
|
53
|
-
method: 'custom';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type JwtVerifier = DidJwtVerifier | X5cJwtVerifier | CustomJwtVerifier | JwkJwtVerifier | OpenIdFederationJwtVerifier;
|
|
57
|
-
|
|
58
|
-
export const getDidJwtVerifier = (jwt: { header: JwtHeader; payload: JwtPayload }, options: { type: JwtType }): DidJwtVerifier => {
|
|
59
|
-
const { type } = options;
|
|
60
|
-
if (!jwt.header.kid) throw new Error(`Received an invalid JWT. Missing kid header.`);
|
|
61
|
-
if (!jwt.header.alg) throw new Error(`Received an invalid JWT. Missing alg header.`);
|
|
62
|
-
|
|
63
|
-
if (!jwt.header.kid.includes('#')) {
|
|
64
|
-
throw new Error(`Received an invalid JWT.. '${type}' contains an invalid kid header.`);
|
|
65
|
-
}
|
|
66
|
-
return { method: 'did', didUrl: jwt.header.kid, type: type, alg: jwt.header.alg };
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const getIssuer = (type: JwtType, payload: JwtPayload): string => {
|
|
70
|
-
// For 'request-object' the `iss` value is not required so we map the issuer to client_id
|
|
71
|
-
if (type === 'request-object') {
|
|
72
|
-
if (!payload.client_id) {
|
|
73
|
-
throw new Error('Missing required field client_id in request object JWT');
|
|
74
|
-
}
|
|
75
|
-
return payload.client_id as string;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (typeof payload.iss !== 'string') {
|
|
79
|
-
throw new Error(`Received an invalid JWT. '${type}' contains an invalid iss claim or it is missing.`);
|
|
80
|
-
}
|
|
81
|
-
return payload.iss;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export const getX5cVerifier = (jwt: { header: JwtHeader; payload: JwtPayload }, options: { type: JwtType }): X5cJwtVerifier => {
|
|
85
|
-
const { type } = options;
|
|
86
|
-
if (!jwt.header.x5c) throw new Error(`Received an invalid JWT. Missing x5c header.`);
|
|
87
|
-
if (!jwt.header.alg) throw new Error(`Received an invalid JWT. Missing alg header.`);
|
|
88
|
-
|
|
89
|
-
if (!Array.isArray(jwt.header.x5c) || jwt.header.x5c.length === 0 || !jwt.header.x5c.every((cert) => typeof cert === 'string')) {
|
|
90
|
-
throw new Error(`Received an invalid JWT.. '${type}' contains an invalid x5c header.`);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
method: 'x5c',
|
|
95
|
-
x5c: jwt.header.x5c,
|
|
96
|
-
issuer: getIssuer(type, jwt.payload),
|
|
97
|
-
type: type,
|
|
98
|
-
alg: jwt.header.alg,
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export const getJwkVerifier = async (jwt: { header: JwtHeader; payload: JwtPayload }, options: { type: JwtType }): Promise<JwkJwtVerifier> => {
|
|
103
|
-
const { type } = options;
|
|
104
|
-
if (!jwt.header.jwk) throw new Error(`Received an invalid JWT. Missing jwk header.`);
|
|
105
|
-
if (!jwt.header.alg) throw new Error(`Received an invalid JWT. Missing alg header.`);
|
|
106
|
-
|
|
107
|
-
if (typeof jwt.header.jwk !== 'object') {
|
|
108
|
-
throw new Error(`Received an invalid JWT. '${type}' contains an invalid jwk header.`);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return { method: 'jwk', type, jwk: jwt.header.jwk, alg: jwt.header.alg };
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
export const getJwtVerifierWithContext = async (
|
|
115
|
-
jwt: { header: JwtHeader; payload: JwtPayload },
|
|
116
|
-
options: { type: JwtType },
|
|
117
|
-
): Promise<JwtVerifier> => {
|
|
118
|
-
const { header, payload } = jwt;
|
|
119
|
-
|
|
120
|
-
if (header.kid?.startsWith('did:')) return getDidJwtVerifier({ header, payload }, options);
|
|
121
|
-
else if (jwt.header.x5c) return getX5cVerifier({ header, payload }, options);
|
|
122
|
-
else if (jwt.header.jwk) return getJwkVerifier({ header, payload }, options);
|
|
123
|
-
|
|
124
|
-
return { method: 'custom', type: options.type };
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
export type VerifyJwtCallbackBase<T extends JwtVerifier> = (
|
|
128
|
-
jwtVerifier: T,
|
|
129
|
-
jwt: { header: JwtHeader; payload: JwtPayload; raw: string },
|
|
130
|
-
) => Promise<boolean>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { calculateJwkThumbprint } from '../JwkThumbprint';
|
|
2
|
-
|
|
3
|
-
describe('JwkThumbprint', () => {
|
|
4
|
-
test('correctly calculates jwk thumbprint', async () => {
|
|
5
|
-
// Based on https://www.rfc-editor.org/rfc/rfc7638.html#section-3.1
|
|
6
|
-
expect(
|
|
7
|
-
await calculateJwkThumbprint({
|
|
8
|
-
kty: 'RSA',
|
|
9
|
-
n: '0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw',
|
|
10
|
-
e: 'AQAB',
|
|
11
|
-
alg: 'RS256',
|
|
12
|
-
kid: '2011-04-29',
|
|
13
|
-
}),
|
|
14
|
-
).toEqual('NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs');
|
|
15
|
-
});
|
|
16
|
-
});
|