@sphereon/ssi-sdk-ext.jwt-service 0.24.1-unstable.130 → 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.
@@ -1,167 +1,330 @@
1
1
  import {
2
- ExternalIdentifierDidOpts,
3
- ExternalIdentifierResult,
4
- ExternalIdentifierX5cOpts,
5
- IIdentifierResolution,
6
- ManagedIdentifierOptsOrResult,
7
- ManagedIdentifierResult,
2
+ ExternalIdentifierDidOpts,
3
+ ExternalIdentifierResult,
4
+ ExternalIdentifierX5cOpts,
5
+ IIdentifierResolution,
6
+ ManagedIdentifierOptsOrResult,
7
+ ManagedIdentifierResult,
8
8
  } from '@sphereon/ssi-sdk-ext.identifier-resolution'
9
- import { ClientIdScheme } from '@sphereon/ssi-sdk-ext.x509-utils'
10
- import { IValidationResult, JoseSignatureAlgorithm, JoseSignatureAlgorithmString, JWK } from '@sphereon/ssi-types'
11
- import { IAgentContext, IKeyManager, IPluginMethodMap } from '@veramo/core'
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
- 'jwtPrepareJws',
17
- 'jwtCreateJwsJsonGeneralSignature',
18
- 'jwtCreateJwsJsonFlattenedSignature',
19
- 'jwtCreateJwsCompactSignature',
20
- 'jwtVerifyJwsSignature',
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
- jwtPrepareJws(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<PreparedJwsObject>
32
+ jwtPrepareJws(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<PreparedJwsObject>
33
+
34
+ jwtCreateJwsJsonGeneralSignature(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<JwsJsonGeneral>
25
35
 
26
- jwtCreateJwsJsonGeneralSignature(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<JwsJsonGeneral>
36
+ jwtCreateJwsJsonFlattenedSignature(args: CreateJwsFlattenedArgs, context: IRequiredContext): Promise<JwsJsonFlattened>
27
37
 
28
- jwtCreateJwsJsonFlattenedSignature(args: CreateJwsFlattenedArgs, context: IRequiredContext): Promise<JwsJsonFlattened>
38
+ jwtCreateJwsCompactSignature(args: CreateJwsCompactArgs, context: IRequiredContext): Promise<JwtCompactResult>
29
39
 
30
- jwtCreateJwsCompactSignature(args: CreateJwsCompactArgs, context: IRequiredContext): Promise<JwsCompactResult>
40
+ jwtVerifyJwsSignature(args: VerifyJwsArgs, context: IRequiredContext): Promise<IJwsValidationResult>
31
41
 
32
- jwtVerifyJwsSignature(args: VerifyJwsArgs, context: IRequiredContext): Promise<IJwsValidationResult>
42
+ jwtEncryptJweCompactJwt(args: EncryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult>
33
43
 
34
- // TODO: JWE/encryption
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
- jws: JwsJsonGeneralWithIdentifiers // We always translate to general as that is the most flexible format allowing multiple sigs
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
- protectedHeader: JwtHeader
43
- payload: Uint8Array
44
- unprotectedHeader?: JwtHeader // only for jws json and also then optional
45
- existingSignatures?: Array<JwsJsonSignature> // only for jws json and also then optional
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
- protected: string
50
- header?: JwtHeader
51
- signature: string
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
- payload: string
60
- protected: string
61
- header?: JwtHeader
62
- signature: string
112
+ payload: string
113
+ protected: string
114
+ header?: JwsHeader
115
+ signature: string
63
116
  }
64
117
 
65
118
  export interface JwsJsonGeneral {
66
- payload: string
67
- signatures: Array<JwsJsonSignature>
119
+ payload: string
120
+ signatures: Array<JwsJsonSignature>
68
121
  }
69
122
 
70
123
  export interface JwsJsonGeneralWithIdentifiers extends JwsJsonGeneral {
71
- signatures: Array<JwsJsonSignatureWithIdentifier>
124
+ signatures: Array<JwsJsonSignatureWithIdentifier>
72
125
  }
73
126
 
74
127
  export interface JwsJsonSignatureWithIdentifier extends JwsJsonSignature {
75
- identifier: ExternalIdentifierResult
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
- jws: PreparedJws
80
- b64: { payload: string; protectedHeader: string } // header is always json, as it can only be used in JwsJson
81
- identifier: ManagedIdentifierResult
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
- typ?: string
86
- alg?: string
87
- kid?: string
170
+ typ?: string
171
+ alg?: string
172
+ kid?: string
88
173
  }
89
174
 
90
175
  export interface BaseJwtPayload {
91
- iss?: string
92
- sub?: string
93
- aud?: string[] | string
94
- exp?: number
95
- nbf?: number
96
- iat?: number
97
- jti?: string
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 JwtHeader extends BaseJwtHeader {
101
- kid?: string
102
- jwk?: JWK
103
- x5c?: string[]
185
+ export interface JwsHeader extends BaseJwtHeader {
186
+ kid?: string
187
+ jwk?: JWK
188
+ x5c?: string[]
104
189
 
105
- [key: string]: unknown
190
+ [key: string]: unknown
106
191
  }
107
192
 
108
- export interface JwtPayload extends BaseJwtPayload {
109
- [key: string]: unknown
193
+ export interface JwsPayload extends BaseJwtPayload {
194
+ [key: string]: unknown
110
195
  }
111
196
 
112
197
  export interface JwsHeaderOpts {
113
- alg: JoseSignatureAlgorithm | JoseSignatureAlgorithmString
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
- mode?: JwsIdentifierMode
120
- issuer: ManagedIdentifierOptsOrResult & {
121
- noIssPayloadUpdate?: boolean
122
- noIdentifierInHeader?: boolean
123
- }
124
- clientId?: string
125
- clientIdScheme?: ClientIdScheme | 'did' | string
126
- protectedHeader: JwtHeader
127
- payload: JwtPayload | Uint8Array | string
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
- jws: Jws
136
- jwk?: JWK // Jwk will be resolved from jws, but you can also provide one
137
- opts?: { x5c?: Omit<ExternalIdentifierX5cOpts, 'identifier'>; did?: Omit<ExternalIdentifierDidOpts, 'identifier'> }
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
- unprotectedHeader?: JwtHeader // only for jws json
145
- existingSignatures?: Array<JwsJsonSignature> // Only for jws json
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 JwsCompactResult {
152
- jwt: JwsCompact
275
+ export interface JwtCompactResult {
276
+ jwt: JwsCompact | JweCompact
153
277
  }
154
278
 
155
279
  export function isJwsCompact(jws: Jws): jws is JwsCompact {
156
- return typeof jws === 'string' && jws.split('~')[0].match(COMPACT_JWS_REGEX) !== null
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
- return typeof jws === 'object' && 'signature' in jws && 'protected' in jws
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
- return typeof jws === 'object' && 'signatures' in jws
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
+