@sphereon/ssi-sdk-ext.jwt-service 0.24.1-unstable.124 → 0.24.1-unstable.132
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/agent/JwtService.d.ts +2 -0
- package/dist/agent/JwtService.d.ts.map +1 -1
- package/dist/agent/JwtService.js +71 -0
- package/dist/agent/JwtService.js.map +1 -1
- package/dist/functions/JWE.d.ts +75 -0
- package/dist/functions/JWE.d.ts.map +1 -0
- package/dist/functions/JWE.js +280 -0
- package/dist/functions/JWE.js.map +1 -0
- package/dist/functions/index.d.ts +3 -3
- package/dist/functions/index.js +4 -4
- package/dist/functions/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/types/IJwtService.d.ts +116 -13
- package/dist/types/IJwtService.d.ts.map +1 -1
- package/dist/types/IJwtService.js +36 -3
- package/dist/types/IJwtService.js.map +1 -1
- package/package.json +12 -10
- package/plugin.schema.json +2569 -960
- package/src/agent/JwtService.ts +103 -47
- package/src/functions/JWE.ts +358 -0
- package/src/functions/index.ts +15 -15
- package/src/index.ts +4 -0
- package/src/types/IJwtService.ts +239 -76
package/src/types/IJwtService.ts
CHANGED
|
@@ -1,167 +1,330 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
ExternalIdentifierDidOpts,
|
|
3
|
+
ExternalIdentifierResult,
|
|
4
|
+
ExternalIdentifierX5cOpts,
|
|
5
|
+
IIdentifierResolution,
|
|
6
|
+
ManagedIdentifierOptsOrResult,
|
|
7
|
+
ManagedIdentifierResult,
|
|
8
8
|
} from '@sphereon/ssi-sdk-ext.identifier-resolution'
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
9
|
+
import {ClientIdScheme} from '@sphereon/ssi-sdk-ext.x509-utils'
|
|
10
|
+
import {
|
|
11
|
+
BaseJWK,
|
|
12
|
+
IValidationResult,
|
|
13
|
+
JoseSignatureAlgorithm,
|
|
14
|
+
JoseSignatureAlgorithmString,
|
|
15
|
+
JWK
|
|
16
|
+
} from '@sphereon/ssi-types'
|
|
17
|
+
import {IAgentContext, IKeyManager, IPluginMethodMap} from '@veramo/core'
|
|
12
18
|
|
|
13
19
|
export type IRequiredContext = IAgentContext<IIdentifierResolution & IKeyManager> // could we still interop with Veramo?
|
|
14
20
|
|
|
15
21
|
export const jwtServiceContextMethods: Array<string> = [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
'jwtPrepareJws',
|
|
23
|
+
'jwtCreateJwsJsonGeneralSignature',
|
|
24
|
+
'jwtCreateJwsJsonFlattenedSignature',
|
|
25
|
+
'jwtCreateJwsCompactSignature',
|
|
26
|
+
'jwtVerifyJwsSignature',
|
|
27
|
+
'jwtEncryptJweCompactJwt',
|
|
28
|
+
'jwtDecryptJweCompactJwt'
|
|
21
29
|
]
|
|
22
30
|
|
|
23
31
|
export interface IJwtService extends IPluginMethodMap {
|
|
24
|
-
|
|
32
|
+
jwtPrepareJws(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<PreparedJwsObject>
|
|
33
|
+
|
|
34
|
+
jwtCreateJwsJsonGeneralSignature(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<JwsJsonGeneral>
|
|
25
35
|
|
|
26
|
-
|
|
36
|
+
jwtCreateJwsJsonFlattenedSignature(args: CreateJwsFlattenedArgs, context: IRequiredContext): Promise<JwsJsonFlattened>
|
|
27
37
|
|
|
28
|
-
|
|
38
|
+
jwtCreateJwsCompactSignature(args: CreateJwsCompactArgs, context: IRequiredContext): Promise<JwtCompactResult>
|
|
29
39
|
|
|
30
|
-
|
|
40
|
+
jwtVerifyJwsSignature(args: VerifyJwsArgs, context: IRequiredContext): Promise<IJwsValidationResult>
|
|
31
41
|
|
|
32
|
-
|
|
42
|
+
jwtEncryptJweCompactJwt(args: EncryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult>
|
|
33
43
|
|
|
34
|
-
|
|
44
|
+
jwtDecryptJweCompactJwt(args: DecryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult>
|
|
45
|
+
|
|
46
|
+
// TODO: JWE/encryption general methods
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
export type IJwsValidationResult = IValidationResult & {
|
|
38
|
-
|
|
50
|
+
jws: JwsJsonGeneralWithIdentifiers // We always translate to general as that is the most flexible format allowing multiple sigs
|
|
39
51
|
}
|
|
40
52
|
|
|
41
53
|
export interface PreparedJws {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
protectedHeader: JwsHeader
|
|
55
|
+
payload: Uint8Array
|
|
56
|
+
unprotectedHeader?: JwsHeader // only for jws json and also then optional
|
|
57
|
+
existingSignatures?: Array<JwsJsonSignature> // only for jws json and also then optional
|
|
46
58
|
}
|
|
47
59
|
|
|
48
60
|
export interface JwsJsonSignature {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
61
|
+
protected: string
|
|
62
|
+
header?: JwsHeader
|
|
63
|
+
signature: string
|
|
52
64
|
}
|
|
53
65
|
|
|
66
|
+
/**
|
|
67
|
+
* The JWK representation of an ephemeral public key.
|
|
68
|
+
* See https://www.rfc-editor.org/rfc/rfc7518.html#section-6
|
|
69
|
+
*/
|
|
70
|
+
// todo split into separate objects
|
|
71
|
+
export type EphemeralPublicKey = Omit<BaseJWK, 'alg'>
|
|
72
|
+
|
|
73
|
+
// export function isEcJWK(v)
|
|
74
|
+
|
|
75
|
+
export interface JweHeader extends Omit<BaseJwtHeader, 'alg'> {
|
|
76
|
+
alg: string,
|
|
77
|
+
enc: string,
|
|
78
|
+
jku?: string,
|
|
79
|
+
jwk?: BaseJWK,
|
|
80
|
+
epk?: EphemeralPublicKey,
|
|
81
|
+
x5u?: string
|
|
82
|
+
x5c?: string[]
|
|
83
|
+
x5t?: string,
|
|
84
|
+
cty?: string,
|
|
85
|
+
crit?: string[]
|
|
86
|
+
|
|
87
|
+
[k: string]: any
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
export interface JweRecipientUnprotectedHeader {
|
|
93
|
+
alg: string
|
|
94
|
+
iv: string
|
|
95
|
+
tag: string
|
|
96
|
+
epk?: EphemeralPublicKey
|
|
97
|
+
kid?: string
|
|
98
|
+
apv?: string
|
|
99
|
+
apu?: string
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface JweProtectedHeader extends Partial<JweHeader> {
|
|
103
|
+
zip?: 'DEF' | string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
54
107
|
export type Jws = JwsCompact | JwsJsonFlattened | JwsJsonGeneral
|
|
55
108
|
|
|
56
109
|
export type JwsCompact = string
|
|
57
110
|
|
|
58
111
|
export interface JwsJsonFlattened {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
112
|
+
payload: string
|
|
113
|
+
protected: string
|
|
114
|
+
header?: JwsHeader
|
|
115
|
+
signature: string
|
|
63
116
|
}
|
|
64
117
|
|
|
65
118
|
export interface JwsJsonGeneral {
|
|
66
|
-
|
|
67
|
-
|
|
119
|
+
payload: string
|
|
120
|
+
signatures: Array<JwsJsonSignature>
|
|
68
121
|
}
|
|
69
122
|
|
|
70
123
|
export interface JwsJsonGeneralWithIdentifiers extends JwsJsonGeneral {
|
|
71
|
-
|
|
124
|
+
signatures: Array<JwsJsonSignatureWithIdentifier>
|
|
72
125
|
}
|
|
73
126
|
|
|
74
127
|
export interface JwsJsonSignatureWithIdentifier extends JwsJsonSignature {
|
|
75
|
-
|
|
128
|
+
identifier: ExternalIdentifierResult
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
export type Jwe = JweCompact | JweJsonFlattened | JweJsonGeneral
|
|
133
|
+
export type JweCompact = string
|
|
134
|
+
|
|
135
|
+
export interface JweJsonFlattened {
|
|
136
|
+
protected: string
|
|
137
|
+
unprotected: JweHeader
|
|
138
|
+
header: JweHeader | JweRecipientUnprotectedHeader
|
|
139
|
+
encrypted_key?: string
|
|
140
|
+
aad?: string
|
|
141
|
+
iv: string
|
|
142
|
+
ciphertext: string
|
|
143
|
+
tag?: string
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
export interface JweRecipient {
|
|
148
|
+
header?: JweRecipientUnprotectedHeader
|
|
149
|
+
encrypted_key?: string
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface JweJsonGeneral {
|
|
153
|
+
protected: string
|
|
154
|
+
unprotected?: JweHeader
|
|
155
|
+
recipients: Array<JweRecipient>
|
|
156
|
+
aad?: string
|
|
157
|
+
iv: string
|
|
158
|
+
ciphertext: string
|
|
159
|
+
tag?: string
|
|
76
160
|
}
|
|
77
161
|
|
|
162
|
+
|
|
78
163
|
export interface PreparedJwsObject {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
164
|
+
jws: PreparedJws
|
|
165
|
+
b64: { payload: string; protectedHeader: string } // header is always json, as it can only be used in JwsJson
|
|
166
|
+
identifier: ManagedIdentifierResult
|
|
82
167
|
}
|
|
83
168
|
|
|
84
169
|
export interface BaseJwtHeader {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
170
|
+
typ?: string
|
|
171
|
+
alg?: string
|
|
172
|
+
kid?: string
|
|
88
173
|
}
|
|
89
174
|
|
|
90
175
|
export interface BaseJwtPayload {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
176
|
+
iss?: string
|
|
177
|
+
sub?: string
|
|
178
|
+
aud?: string[] | string
|
|
179
|
+
exp?: number
|
|
180
|
+
nbf?: number
|
|
181
|
+
iat?: number
|
|
182
|
+
jti?: string
|
|
98
183
|
}
|
|
99
184
|
|
|
100
|
-
export interface
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
185
|
+
export interface JwsHeader extends BaseJwtHeader {
|
|
186
|
+
kid?: string
|
|
187
|
+
jwk?: JWK
|
|
188
|
+
x5c?: string[]
|
|
104
189
|
|
|
105
|
-
|
|
190
|
+
[key: string]: unknown
|
|
106
191
|
}
|
|
107
192
|
|
|
108
|
-
export interface
|
|
109
|
-
|
|
193
|
+
export interface JwsPayload extends BaseJwtPayload {
|
|
194
|
+
[key: string]: unknown
|
|
110
195
|
}
|
|
111
196
|
|
|
112
197
|
export interface JwsHeaderOpts {
|
|
113
|
-
|
|
198
|
+
alg: JoseSignatureAlgorithm | JoseSignatureAlgorithmString
|
|
114
199
|
}
|
|
115
200
|
|
|
116
201
|
export type JwsIdentifierMode = 'x5c' | 'kid' | 'jwk' | 'did' | 'auto'
|
|
117
202
|
|
|
203
|
+
export type EncryptJweCompactJwtArgs = {
|
|
204
|
+
payload: JwsPayload,
|
|
205
|
+
protectedHeader?: JweProtectedHeader | undefined,
|
|
206
|
+
aad?: Uint8Array | undefined
|
|
207
|
+
recipientKey: ExternalIdentifierResult & { kid?: string}
|
|
208
|
+
alg?: JweAlg
|
|
209
|
+
enc?: JweEnc
|
|
210
|
+
apu?: string // base64url
|
|
211
|
+
apv?: string // base64url
|
|
212
|
+
expirationTime?: number | string | Date
|
|
213
|
+
issuer?: string
|
|
214
|
+
audience?: string | string[]
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export type DecryptJweCompactJwtArgs = {
|
|
218
|
+
jwe: JweCompact
|
|
219
|
+
idOpts: ManagedIdentifierOptsOrResult
|
|
220
|
+
}
|
|
221
|
+
|
|
118
222
|
export type CreateJwsArgs = {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
223
|
+
mode?: JwsIdentifierMode
|
|
224
|
+
issuer: ManagedIdentifierOptsOrResult & {
|
|
225
|
+
noIssPayloadUpdate?: boolean
|
|
226
|
+
noIdentifierInHeader?: boolean
|
|
227
|
+
}
|
|
228
|
+
clientId?: string
|
|
229
|
+
clientIdScheme?: ClientIdScheme | 'did' | string
|
|
230
|
+
protectedHeader: JwsHeader
|
|
231
|
+
payload: JwsPayload | Uint8Array | string
|
|
128
232
|
}
|
|
129
233
|
|
|
234
|
+
|
|
235
|
+
export type CreateJweArgs = {
|
|
236
|
+
mode?: JwsIdentifierMode
|
|
237
|
+
issuer: ManagedIdentifierOptsOrResult & {
|
|
238
|
+
noIssPayloadUpdate?: boolean
|
|
239
|
+
noIdentifierInHeader?: boolean
|
|
240
|
+
}
|
|
241
|
+
protectedHeader: JweProtectedHeader
|
|
242
|
+
encryptedKey: string | EphemeralPublicKey // In case it is a string it is already encrypted; otherwise encrypt //TODO ??
|
|
243
|
+
iv: string
|
|
244
|
+
ciphertext: string
|
|
245
|
+
tag: string
|
|
246
|
+
}
|
|
130
247
|
export type CreateJwsCompactArgs = CreateJwsArgs
|
|
131
248
|
|
|
132
249
|
export type CreateJwsFlattenedArgs = Exclude<CreateJwsJsonArgs, 'existingSignatures'>
|
|
133
250
|
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
134
254
|
export type VerifyJwsArgs = {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
255
|
+
jws: Jws
|
|
256
|
+
jwk?: JWK // Jwk will be resolved from jws, but you can also provide one
|
|
257
|
+
opts?: { x5c?: Omit<ExternalIdentifierX5cOpts, 'identifier'>; did?: Omit<ExternalIdentifierDidOpts, 'identifier'> }
|
|
138
258
|
}
|
|
139
259
|
|
|
140
260
|
/**
|
|
141
261
|
* @public
|
|
142
262
|
*/
|
|
143
263
|
export type CreateJwsJsonArgs = CreateJwsArgs & {
|
|
144
|
-
|
|
145
|
-
|
|
264
|
+
unprotectedHeader?: JwsHeader // only for jws json
|
|
265
|
+
existingSignatures?: Array<JwsJsonSignature> // Only for jws json
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export type CreateJweJsonArgs = CreateJweArgs & {
|
|
269
|
+
unprotectedHeader?: JweHeader
|
|
146
270
|
}
|
|
147
271
|
|
|
148
272
|
/**
|
|
149
273
|
* @public
|
|
150
274
|
*/
|
|
151
|
-
export interface
|
|
152
|
-
|
|
275
|
+
export interface JwtCompactResult {
|
|
276
|
+
jwt: JwsCompact | JweCompact
|
|
153
277
|
}
|
|
154
278
|
|
|
155
279
|
export function isJwsCompact(jws: Jws): jws is JwsCompact {
|
|
156
|
-
|
|
280
|
+
return typeof jws === 'string' && jws.split('~')[0].match(COMPACT_JWS_REGEX) !== null
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function isJweCompact(jwe: Jwe): jwe is JweCompact {
|
|
284
|
+
return typeof jwe === 'string' && jwe.split('~')[0].match(COMPACT_JWE_REGEX) !== null
|
|
157
285
|
}
|
|
158
286
|
|
|
287
|
+
|
|
159
288
|
export function isJwsJsonFlattened(jws: Jws): jws is JwsJsonFlattened {
|
|
160
|
-
|
|
289
|
+
return typeof jws === 'object' && 'signature' in jws && 'protected' in jws && !('ciphertext' in jws)
|
|
161
290
|
}
|
|
162
291
|
|
|
163
292
|
export function isJwsJsonGeneral(jws: Jws): jws is JwsJsonGeneral {
|
|
164
|
-
|
|
293
|
+
return typeof jws === 'object' && 'signatures' in jws && !('ciphertext' in jws)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
export function isJweJsonFlattened(jwe: Jwe): jwe is JweJsonFlattened {
|
|
298
|
+
return typeof jwe === 'object' && 'signature' in jwe && 'ciphertext' in jwe && !('payload' in jwe)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function isJweJsonGeneral(jwe: Jwe): jwe is JweJsonGeneral {
|
|
302
|
+
return typeof jwe === 'object' && 'signatures' in jwe && 'ciphertext' in jwe && !('payload' in jwe)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
export function isJwsHeader(header: BaseJwtHeader & Record<string, any>): header is JwsHeader {
|
|
307
|
+
return header && !isJweHeader(header)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function isJweHeader(header: BaseJwtHeader & Record<string, any>): header is JweHeader {
|
|
311
|
+
return ('enc' in header && header.enc && jweEnc(header.enc)) || (header.alg && jweAlg(header.alg))
|
|
165
312
|
}
|
|
166
313
|
|
|
167
314
|
export const COMPACT_JWS_REGEX = /^([a-zA-Z0-9_=-]+).([a-zA-Z0-9_=-]+)?.([a-zA-Z0-9_=-]+)?$/
|
|
315
|
+
export const COMPACT_JWE_REGEX = /^([a-zA-Z0-9_=-]+)\.([a-zA-Z0-9_=-]+)?\.([a-zA-Z0-9_=-]+)\.([a-zA-Z0-9_=-]+)?\.([a-zA-Z0-9_=-]+)?$/
|
|
316
|
+
|
|
317
|
+
export const JweAlgs = ['RSA1_5', 'RSA-OAEP', 'RSA-OAEP-256', 'A128KW', 'A192KW', 'A256KW', 'dir', 'ECDH-ES'/*interop value*/, 'ECDH-ES+A128KW', 'ECDH-ES+A192KW', 'ECDH-ES+A256KW', 'A128GCMKW', 'A192GCMKW', 'A256GCMKW', 'PBES2-HS256+A128KW', 'PBES2-HS384+A192KW', 'PBES2-HS512+A256KW'] as const;
|
|
318
|
+
export type JweAlg = typeof JweAlgs[number]
|
|
319
|
+
export function jweAlg(alg?: string | JweAlg): JweAlg | undefined {
|
|
320
|
+
return JweAlgs.find((supportedVal) => supportedVal === alg);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
export const JweEncs = ['A128CBC-HS256', 'A192CBC-HS384', 'A256CBC-HS512', 'A128GCM', 'A192GCM', 'A256GCM'/*interop value*/] as const
|
|
325
|
+
export type JweEnc = typeof JweEncs[number]
|
|
326
|
+
|
|
327
|
+
export function jweEnc(alg?: string | JweEnc): JweEnc | undefined {
|
|
328
|
+
return JweEncs.find((supportedVal) => supportedVal === alg);
|
|
329
|
+
}
|
|
330
|
+
|