@sphereon/ssi-sdk-ext.jwt-service 0.26.0 → 0.26.1-feature.OIDF.69.22

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,29 +1,29 @@
1
- import {IAgentPlugin} from '@veramo/core'
1
+ import { IAgentPlugin } from '@veramo/core'
2
2
  import {
3
- createJwsCompact,
4
- CreateJwsCompactArgs,
5
- CreateJwsFlattenedArgs,
6
- CreateJwsJsonArgs,
7
- createJwsJsonFlattened,
8
- createJwsJsonGeneral,
9
- DecryptJweCompactJwtArgs,
10
- EncryptJweCompactJwtArgs,
11
- IJwsValidationResult,
12
- IJwtService,
13
- IRequiredContext,
14
- jweAlg,
15
- jweEnc,
16
- JwsJsonFlattened,
17
- JwsJsonGeneral,
18
- JwtCompactResult,
19
- JwtLogger,
20
- PreparedJwsObject,
21
- prepareJwsObject,
22
- schema,
23
- verifyJws,
24
- VerifyJwsArgs,
3
+ createJwsCompact,
4
+ CreateJwsCompactArgs,
5
+ CreateJwsFlattenedArgs,
6
+ CreateJwsJsonArgs,
7
+ createJwsJsonFlattened,
8
+ createJwsJsonGeneral,
9
+ DecryptJweCompactJwtArgs,
10
+ EncryptJweCompactJwtArgs,
11
+ IJwsValidationResult,
12
+ IJwtService,
13
+ IRequiredContext,
14
+ jweAlg,
15
+ jweEnc,
16
+ JwsJsonFlattened,
17
+ JwsJsonGeneral,
18
+ JwtCompactResult,
19
+ JwtLogger,
20
+ PreparedJwsObject,
21
+ prepareJwsObject,
22
+ schema,
23
+ verifyJws,
24
+ VerifyJwsArgs,
25
25
  } from '..'
26
- import {CompactJwtEncrypter} from "../functions/JWE";
26
+ import { CompactJwtEncrypter } from '../functions/JWE'
27
27
 
28
28
  import * as u8a from 'uint8arrays'
29
29
 
@@ -31,81 +31,89 @@ import * as u8a from 'uint8arrays'
31
31
  * @public
32
32
  */
33
33
  export class JwtService implements IAgentPlugin {
34
- readonly schema = schema.IJwtService
35
- readonly methods: IJwtService = {
36
- jwtPrepareJws: this.jwtPrepareJws.bind(this),
37
- jwtCreateJwsJsonGeneralSignature: this.jwtCreateJwsJsonGeneralSignature.bind(this),
38
- jwtCreateJwsJsonFlattenedSignature: this.jwtCreateJwsJsonFlattenedSignature.bind(this),
39
- jwtCreateJwsCompactSignature: this.jwtCreateJwsCompactSignature.bind(this),
40
- jwtVerifyJwsSignature: this.jwtVerifyJwsSignature.bind(this),
41
- jwtEncryptJweCompactJwt: this.jwtEncryptJweCompactJwt.bind(this),
42
- jwtDecryptJweCompactJwt: this.jwtDecryptJweCompactJwt.bind(this)
43
- }
44
-
45
- private async jwtPrepareJws(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<PreparedJwsObject> {
46
- return await prepareJwsObject(args, context)
47
- }
34
+ readonly schema = schema.IJwtService
35
+ readonly methods: IJwtService = {
36
+ jwtPrepareJws: this.jwtPrepareJws.bind(this),
37
+ jwtCreateJwsJsonGeneralSignature: this.jwtCreateJwsJsonGeneralSignature.bind(this),
38
+ jwtCreateJwsJsonFlattenedSignature: this.jwtCreateJwsJsonFlattenedSignature.bind(this),
39
+ jwtCreateJwsCompactSignature: this.jwtCreateJwsCompactSignature.bind(this),
40
+ jwtVerifyJwsSignature: this.jwtVerifyJwsSignature.bind(this),
41
+ jwtEncryptJweCompactJwt: this.jwtEncryptJweCompactJwt.bind(this),
42
+ jwtDecryptJweCompactJwt: this.jwtDecryptJweCompactJwt.bind(this),
43
+ }
48
44
 
49
- private async jwtCreateJwsJsonGeneralSignature(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<JwsJsonGeneral> {
50
- return await createJwsJsonGeneral(args, context)
51
- }
52
-
53
- private async jwtCreateJwsJsonFlattenedSignature(args: CreateJwsFlattenedArgs, context: IRequiredContext): Promise<JwsJsonFlattened> {
54
- return await createJwsJsonFlattened(args, context)
55
- }
45
+ private async jwtPrepareJws(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<PreparedJwsObject> {
46
+ return await prepareJwsObject(args, context)
47
+ }
56
48
 
57
- private async jwtCreateJwsCompactSignature(args: CreateJwsCompactArgs, context: IRequiredContext): Promise<JwtCompactResult> {
58
- // We wrap it in a json object for remote REST calls
59
- return {jwt: await createJwsCompact(args, context)}
60
- }
49
+ private async jwtCreateJwsJsonGeneralSignature(args: CreateJwsJsonArgs, context: IRequiredContext): Promise<JwsJsonGeneral> {
50
+ return await createJwsJsonGeneral(args, context)
51
+ }
61
52
 
62
- private async jwtVerifyJwsSignature(args: VerifyJwsArgs, context: IRequiredContext): Promise<IJwsValidationResult> {
63
- return await verifyJws(args, context)
64
- }
53
+ private async jwtCreateJwsJsonFlattenedSignature(args: CreateJwsFlattenedArgs, context: IRequiredContext): Promise<JwsJsonFlattened> {
54
+ return await createJwsJsonFlattened(args, context)
55
+ }
65
56
 
66
- private async jwtEncryptJweCompactJwt(args: EncryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult> {
67
- const {payload, protectedHeader = {alg: args.alg, enc: args.enc}, recipientKey, issuer, expirationTime, audience} = args
57
+ private async jwtCreateJwsCompactSignature(args: CreateJwsCompactArgs, context: IRequiredContext): Promise<JwtCompactResult> {
58
+ // We wrap it in a json object for remote REST calls
59
+ return { jwt: await createJwsCompact(args, context) }
60
+ }
68
61
 
69
- console.log(JSON.stringify(args, null, 2))
62
+ private async jwtVerifyJwsSignature(args: VerifyJwsArgs, context: IRequiredContext): Promise<IJwsValidationResult> {
63
+ return await verifyJws(args, context)
64
+ }
70
65
 
71
- const alg = jweAlg(args.alg) ?? jweAlg(protectedHeader.alg) ?? 'ECDH-ES'
72
- const enc = jweEnc(args.enc) ?? jweEnc(protectedHeader.enc) ?? 'A256GCM'
73
- const encJwks = recipientKey.jwks.length === 1 ? [recipientKey.jwks[0]] : recipientKey.jwks.filter(jwk => (jwk.kid && (jwk.kid === jwk.jwk.kid || jwk.kid === jwk.jwkThumbprint)) || jwk.jwk.use === 'enc')
74
- if (encJwks.length === 0) {
75
- return Promise.reject(Error(`No public JWK found that can be used to encrypt against`))
76
- }
77
- const jwkInfo = encJwks[0]
78
- if (encJwks.length > 0) {
79
- JwtLogger.warning(`More than one JWK with 'enc' usage found. Selected the first one as no 'kid' was provided`, encJwks)
80
- }
81
- if (jwkInfo.jwk.kty?.startsWith('EC') !== true || !alg.startsWith('ECDH')) {
82
- return Promise.reject(Error(`Currently only ECDH-ES is supported for encryption. JWK alg ${jwkInfo.jwk.kty}, header alg ${alg}`)) // TODO: Probably we support way more already
83
- }
84
- const apuVal = protectedHeader.apu ?? args.apu
85
- const apu = apuVal ? u8a.fromString(apuVal, 'base64url') : undefined
86
- const apvVal = protectedHeader.apv ?? args.apv
87
- const apv = apvVal ? u8a.fromString(apvVal, 'base64url') : undefined
66
+ private async jwtEncryptJweCompactJwt(args: EncryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult> {
67
+ const { payload, protectedHeader = { alg: args.alg, enc: args.enc }, recipientKey, issuer, expirationTime, audience } = args
88
68
 
89
- const pubKey = await crypto.subtle.importKey('jwk', jwkInfo.jwk, {
90
- name: 'ECDH',
91
- namedCurve: 'P-256',
92
- }, true, [])
93
- const encrypter = new CompactJwtEncrypter({
94
- enc,
95
- alg,
96
- keyManagementParams: {apu, apv},
97
- key: pubKey,
98
- issuer,
99
- expirationTime,
100
- audience
101
- })
69
+ console.log(JSON.stringify(args, null, 2))
102
70
 
103
- const jwe = await encrypter.encryptCompactJWT(payload, {})
104
- return {jwt: jwe}
71
+ const alg = jweAlg(args.alg) ?? jweAlg(protectedHeader.alg) ?? 'ECDH-ES'
72
+ const enc = jweEnc(args.enc) ?? jweEnc(protectedHeader.enc) ?? 'A256GCM'
73
+ const encJwks =
74
+ recipientKey.jwks.length === 1
75
+ ? [recipientKey.jwks[0]]
76
+ : recipientKey.jwks.filter((jwk) => (jwk.kid && (jwk.kid === jwk.jwk.kid || jwk.kid === jwk.jwkThumbprint)) || jwk.jwk.use === 'enc')
77
+ if (encJwks.length === 0) {
78
+ return Promise.reject(Error(`No public JWK found that can be used to encrypt against`))
79
+ }
80
+ const jwkInfo = encJwks[0]
81
+ if (encJwks.length > 0) {
82
+ JwtLogger.warning(`More than one JWK with 'enc' usage found. Selected the first one as no 'kid' was provided`, encJwks)
105
83
  }
84
+ if (jwkInfo.jwk.kty?.startsWith('EC') !== true || !alg.startsWith('ECDH')) {
85
+ return Promise.reject(Error(`Currently only ECDH-ES is supported for encryption. JWK alg ${jwkInfo.jwk.kty}, header alg ${alg}`)) // TODO: Probably we support way more already
86
+ }
87
+ const apuVal = protectedHeader.apu ?? args.apu
88
+ const apu = apuVal ? u8a.fromString(apuVal, 'base64url') : undefined
89
+ const apvVal = protectedHeader.apv ?? args.apv
90
+ const apv = apvVal ? u8a.fromString(apvVal, 'base64url') : undefined
106
91
 
107
- private async jwtDecryptJweCompactJwt(args: DecryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult> {
92
+ const pubKey = await crypto.subtle.importKey(
93
+ 'jwk',
94
+ jwkInfo.jwk,
95
+ {
96
+ name: 'ECDH',
97
+ namedCurve: 'P-256',
98
+ },
99
+ true,
100
+ []
101
+ )
102
+ const encrypter = new CompactJwtEncrypter({
103
+ enc,
104
+ alg,
105
+ keyManagementParams: { apu, apv },
106
+ key: pubKey,
107
+ issuer,
108
+ expirationTime,
109
+ audience,
110
+ })
108
111
 
109
- return {jwt: "FIXME"}
110
- }
112
+ const jwe = await encrypter.encryptCompactJWT(payload, {})
113
+ return { jwt: jwe }
114
+ }
115
+
116
+ private async jwtDecryptJweCompactJwt(args: DecryptJweCompactJwtArgs, context: IRequiredContext): Promise<JwtCompactResult> {
117
+ return { jwt: 'FIXME' }
118
+ }
111
119
  }