@sphereon/ssi-sdk.siopv2-oid4vp-rp-auth 0.34.1-fix.167 → 0.34.1-fix.169

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.
@@ -4,24 +4,28 @@ import {
4
4
  AuthorizationResponseState,
5
5
  AuthorizationResponseStateStatus,
6
6
  AuthorizationResponseStateWithVerifiedData,
7
- decodeUriAsJson,
7
+ decodeUriAsJson, EncodedDcqlPresentationVpToken,
8
8
  VerifiedAuthorizationResponse
9
9
  } from '@sphereon/did-auth-siop'
10
10
  import { getAgentResolver } from '@sphereon/ssi-sdk-ext.did-utils'
11
11
  import { shaHasher as defaultHasher } from '@sphereon/ssi-sdk.core'
12
12
  import {
13
- //AdditionalClaims,
13
+ AdditionalClaims,
14
14
  CredentialMapper,
15
+ //decodeSdJwtVc,
15
16
  HasherSync,
16
- //ICredentialSubject,
17
- //IPresentation,
18
- //IVerifiableCredential,
19
- //IVerifiablePresentation,
20
- //JwtDecodedVerifiablePresentation,
21
- //MdocDeviceResponse,
22
- //MdocOid4vpMdocVpToken,
17
+ ICredentialSubject,
18
+ IPresentation,
19
+ //IProofPurpose,
20
+ //IProofType,
21
+ IVerifiableCredential,
22
+ IVerifiablePresentation,
23
+ JwtDecodedVerifiablePresentation,
24
+ MdocDeviceResponse,
25
+ MdocOid4vpMdocVpToken,
23
26
  OriginalVerifiablePresentation,
24
- //SdJwtDecodedVerifiableCredential,
27
+ SdJwtDecodedVerifiableCredential,
28
+ //sha256
25
29
  } from '@sphereon/ssi-types'
26
30
  import { IAgentPlugin } from '@veramo/core'
27
31
  import { DcqlQuery } from 'dcql'
@@ -40,11 +44,11 @@ import {
40
44
  ISiopv2RPOpts,
41
45
  IUpdateRequestStateArgs,
42
46
  IVerifyAuthResponseStateArgs,
43
- schema,
44
- // VerifiedDataMode,
47
+ schema
45
48
  } from '../index'
46
49
  import { RPInstance } from '../RPInstance'
47
50
  import { ISIOPv2RP } from '../types/ISIOPv2RP'
51
+ //import { jwtDecode } from 'jwt-decode'
48
52
 
49
53
  export class SIOPv2RP implements IAgentPlugin {
50
54
  private readonly opts: ISiopv2RPOpts
@@ -126,12 +130,7 @@ export class SIOPv2RP implements IAgentPlugin {
126
130
  }
127
131
 
128
132
  const responseState = authorizationResponseState as AuthorizationResponseStateWithVerifiedData
129
- if (responseState.status === AuthorizationResponseStateStatus.VERIFIED
130
-
131
- //&&
132
- // args.includeVerifiedData &&
133
- // args.includeVerifiedData !== VerifiedDataMode.NONE
134
- ) {
133
+ if (responseState.status === AuthorizationResponseStateStatus.VERIFIED) {
135
134
  let hasher: HasherSync | undefined
136
135
  if (
137
136
  CredentialMapper.isSdJwtEncoded(responseState.response.payload.vp_token as OriginalVerifiablePresentation) &&
@@ -139,25 +138,84 @@ export class SIOPv2RP implements IAgentPlugin {
139
138
  ) {
140
139
  hasher = defaultHasher
141
140
  }
142
- // todo this should also include mdl-mdoc
143
- const presentationDecoded = CredentialMapper.decodeVerifiablePresentation(
144
- responseState.response.payload.vp_token as OriginalVerifiablePresentation,
145
- //todo: later we want to conditionally pass in options for mdl-mdoc here
146
- hasher,
147
- )
148
- console.log(`presentationDecoded: ${JSON.stringify(presentationDecoded)}`)
141
+
142
+ const vpToken = responseState.response.payload.vp_token && JSON.parse(responseState.response.payload.vp_token as EncodedDcqlPresentationVpToken)
143
+ const claims = []
144
+ for (const [key, value] of Object.entries(vpToken)) {
145
+ const presentationDecoded = CredentialMapper.decodeVerifiablePresentation(
146
+ value as OriginalVerifiablePresentation,
147
+ //todo: later we want to conditionally pass in options for mdl-mdoc here
148
+ hasher,
149
+ )
150
+ console.log(`presentationDecoded: ${JSON.stringify(presentationDecoded)}`)
151
+
152
+ const allClaims: AdditionalClaims = {}
153
+ for (const credential of this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []) {
154
+ const vc = credential as IVerifiableCredential
155
+ const schemaValidationResult = await context.agent.cvVerifySchema({
156
+ credential,
157
+ hasher,
158
+ validationPolicy: rpInstance.rpOptions.verificationPolicies?.schemaValidation,
159
+ })
160
+ if (!schemaValidationResult.result) {
161
+ responseState.status = AuthorizationResponseStateStatus.ERROR
162
+ responseState.error = new Error(schemaValidationResult.error)
163
+ return responseState
164
+ }
165
+
166
+ const credentialSubject = vc.credentialSubject as ICredentialSubject & AdditionalClaims
167
+ if (!('id' in allClaims)) {
168
+ allClaims['id'] = credentialSubject.id
169
+ }
170
+
171
+ Object.entries(credentialSubject).forEach(([key, value]) => {
172
+ if (!(key in allClaims)) {
173
+ allClaims[key] = value
174
+ }
175
+ })
176
+
177
+ claims.push({
178
+ id: key,
179
+ type: vc.type[0],
180
+ claims: allClaims
181
+ })
182
+ }
183
+ }
184
+
185
+ // const claimsPromises = responseState.response.payload.vp_token && JSON.parse(responseState.response.payload.vp_token as EncodedDcqlPresentationVpToken)
186
+ // .map(async (presentation: OriginalVerifiablePresentation) => {
187
+ // const presentationDecoded = CredentialMapper.decodeVerifiablePresentation(
188
+ // presentation,
189
+ // //todo: later we want to conditionally pass in options for mdl-mdoc here
190
+ // hasher,
191
+ // )
192
+ //
193
+ //
194
+ //
195
+ // return {
196
+ // id: presentationDecoded.id
197
+ // }
198
+ //
199
+ // })
200
+
201
+ // // todo this should also include mdl-mdoc
202
+ // const presentationDecoded = CredentialMapper.decodeVerifiablePresentation(
203
+ // responseState.response.payload.vp_token as OriginalVerifiablePresentation,
204
+ // //todo: later we want to conditionally pass in options for mdl-mdoc here
205
+ // hasher,
206
+ // )
207
+ // console.log(`presentationDecoded: ${JSON.stringify(presentationDecoded)}`)
149
208
 
150
209
  responseState.verifiedData = {
151
210
  ...(responseState.response.payload.vp_token && {
152
211
  authorization_response: {
153
- vp_token: typeof responseState.response.payload.vp_token === 'string'
212
+ vp_token: typeof responseState.response.payload.vp_token === 'string' // TODO we might not need this string check
154
213
  ? JSON.parse(responseState.response.payload.vp_token)
155
214
  : responseState.response.payload.vp_token
156
215
  }
157
216
  }),
158
217
 
159
- // TODO use ??
160
- credential_claims: []//(this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []).map()
218
+ credential_claims: claims//(this.presentationOrClaimsFrom(presentationDecoded).verifiableCredential || []).map()
161
219
  }
162
220
 
163
221
  // switch (args.includeVerifiedData) {
@@ -197,18 +255,17 @@ export class SIOPv2RP implements IAgentPlugin {
197
255
  return responseState
198
256
  }
199
257
 
200
- // private presentationOrClaimsFrom = (
201
- // presentationDecoded:
202
- // | JwtDecodedVerifiablePresentation
203
- // | IVerifiablePresentation
204
- // | SdJwtDecodedVerifiableCredential
205
- // | MdocOid4vpMdocVpToken
206
- // | MdocDeviceResponse
207
- // | DcqlPresentation
208
- // ): AdditionalClaims | IPresentation =>
209
- // CredentialMapper.isSdJwtDecodedCredential(presentationDecoded)
210
- // ? presentationDecoded.decodedPayload
211
- // : CredentialMapper.toUniformPresentation(presentationDecoded as OriginalVerifiablePresentation)
258
+ private presentationOrClaimsFrom = (
259
+ presentationDecoded:
260
+ | JwtDecodedVerifiablePresentation
261
+ | IVerifiablePresentation
262
+ | SdJwtDecodedVerifiableCredential
263
+ | MdocOid4vpMdocVpToken
264
+ | MdocDeviceResponse
265
+ ): AdditionalClaims | IPresentation =>
266
+ CredentialMapper.isSdJwtDecodedCredential(presentationDecoded)
267
+ ? presentationDecoded.decodedPayload
268
+ : CredentialMapper.toUniformPresentation(presentationDecoded as OriginalVerifiablePresentation)
212
269
 
213
270
  private async siopUpdateRequestState(args: IUpdateRequestStateArgs, context: IRequiredContext): Promise<AuthorizationRequestState> {
214
271
  if (args.state !== 'authorization_request_created') {