@@ -63,7 +63,7 @@ import {
6363 CredentialDataSupplier ,
6464 CredentialDataSupplierArgs ,
6565 CredentialIssuanceInput ,
66- CredentialSignerCallback
66+ CredentialSignerCallback , IssuerCorrelation
6767} from './types'
6868
6969import { LOG } from './index'
@@ -367,6 +367,7 @@ export class VcIssuer {
367367 */
368368 public async issueCredential ( opts : {
369369 credentialRequest : CredentialRequest
370+ issuerCorrelation : IssuerCorrelation
370371 credential ?: CredentialIssuanceInput
371372 credentialDataSupplier ?: CredentialDataSupplier
372373 credentialDataSupplierInput ?: CredentialDataSupplierInput
@@ -381,17 +382,16 @@ export class VcIssuer {
381382 throw new Error('credential request should be of spec version 1.0.13 or above')
382383 }*/
383384 const credentialRequest = opts . credentialRequest as CredentialRequestV1_0_15
384- let preAuthorizedCode : string | undefined
385- let issuerState : string | undefined
385+ const issuerCorrelation = opts . issuerCorrelation
386386 try {
387387 if ( ! ( 'credential_identifier' in credentialRequest ) && ! ( 'credential_configuration_id' in credentialRequest ) ) {
388- throw new Error ( 'credential request should have either credential_identifier or credential_configuration_id' )
388+ throw Error ( 'credential request should have either credential_identifier or credential_configuration_id' )
389389 }
390390
391391 // Validate the credential_configuration_id exists in metadata if used
392392 if ( 'credential_configuration_id' in credentialRequest && credentialRequest . credential_configuration_id ) {
393393 if ( ! this . _issuerMetadata . credential_configurations_supported ?. [ credentialRequest . credential_configuration_id ] ) {
394- throw new Error ( TokenErrorResponse . invalid_request )
394+ throw Error ( TokenErrorResponse . invalid_request )
395395 }
396396 }
397397 let format = this . lookupCredentialFormat ( credentialRequest )
@@ -400,8 +400,12 @@ export class VcIssuer {
400400 format,
401401 tokenExpiresIn : opts . tokenExpiresIn ?? 180
402402 } )
403- preAuthorizedCode = validated . preAuthorizedCode
404- issuerState = validated . issuerState
403+ if ( validated . preAuthorizedCode && ! issuerCorrelation . preAuthorizedCode ) {
404+ issuerCorrelation . preAuthorizedCode = validated . preAuthorizedCode
405+ }
406+ if ( validated . issuerState && ! issuerCorrelation . issuerState ) {
407+ issuerCorrelation . issuerState = validated . issuerState
408+ }
405409
406410 const { preAuthSession, authSession, cNonceState, jwtVerifyResult } = validated
407411 const did = jwtVerifyResult . did
@@ -422,7 +426,7 @@ export class VcIssuer {
422426 let credential : CredentialIssuanceInput | undefined
423427
424428 let signerCallback : CredentialSignerCallback | undefined = opts . credentialSignerCallback
425- const session : CredentialOfferSession | undefined = preAuthorizedCode && preAuthSession ? preAuthSession : authSession
429+ const session : CredentialOfferSession | undefined = issuerCorrelation . preAuthorizedCode && preAuthSession ? preAuthSession : authSession
426430 if ( opts . credential ) {
427431 credential = opts . credential
428432 } else {
@@ -521,17 +525,17 @@ export class VcIssuer {
521525
522526 let notification_id : string | undefined
523527
524- if ( preAuthorizedCode && preAuthSession ) {
528+ if ( issuerCorrelation . preAuthorizedCode && preAuthSession ) {
525529 preAuthSession . lastUpdatedAt = + new Date ( )
526530 preAuthSession . status = IssueStatus . CREDENTIAL_ISSUED
527531 notification_id = preAuthSession . notification_id
528- await this . _credentialOfferSessions . set ( preAuthorizedCode , preAuthSession )
529- } else if ( issuerState && authSession ) {
532+ await this . _credentialOfferSessions . set ( issuerCorrelation . preAuthorizedCode , preAuthSession )
533+ } else if ( issuerCorrelation . issuerState && authSession ) {
530534 // If both were set we used the pre auth flow above as well, hence the else if
531535 authSession . lastUpdatedAt = + new Date ( )
532536 authSession . status = IssueStatus . CREDENTIAL_ISSUED
533537 notification_id = authSession . notification_id
534- await this . _credentialOfferSessions . set ( issuerState , authSession )
538+ await this . _credentialOfferSessions . set ( issuerCorrelation . issuerState , authSession )
535539 }
536540
537541 const response : CredentialResponse = {
@@ -553,7 +557,7 @@ export class VcIssuer {
553557 }
554558 return response
555559 } catch ( error : unknown ) {
556- await this . updateSession ( { preAuthorizedCode, issuerState, error } )
560+ await this . updateSession ( { preAuthorizedCode : issuerCorrelation . preAuthorizedCode , issuerState : issuerCorrelation . issuerState , error } )
557561 throw error
558562 }
559563 }
@@ -651,18 +655,19 @@ export class VcIssuer {
651655
652656 private async validateCredentialRequestProof ( {
653657 credentialRequest,
658+ issuerCorrelation,
654659 format,
655660 jwtVerifyCallback,
656661 tokenExpiresIn
657662 } : {
658663 credentialRequest : CredentialRequest ,
664+ issuerCorrelation : IssuerCorrelation
659665 format ?: OID4VCICredentialFormat ,
660666 tokenExpiresIn : number // expiration duration in seconds
661667 // grants?: Grant,
662668 clientId ?: string
663669 jwtVerifyCallback ?: JWTVerifyCallback
664670 } ) {
665- let preAuthorizedCode : string | undefined
666671 let issuerState : string | undefined
667672
668673 const supportedIssuanceFormats = [ 'jwt_vc_json' , 'jwt_vc_json-ld' , 'dc+sd-jwt' , 'ldp_vc' , 'mso_mdoc' ]
@@ -683,24 +688,24 @@ export class VcIssuer {
683688 const { didDocument, did, jwt } = jwtVerifyResult
684689 const { header, payload } = jwt
685690 const { iss, aud, iat, nonce } = payload
686- const issuer_state = 'issuer_state' in credentialRequest && credentialRequest . issuer_state ? credentialRequest . issuer_state : undefined
691+ const issuer_state = 'issuer_state' in credentialRequest && credentialRequest . issuer_state
692+ ? credentialRequest . issuer_state : issuerCorrelation . issuerState
687693 if ( ! nonce && ! issuer_state ) {
688- throw Error ( 'No nonce was found in the Proof of Possession' )
694+ throw Error ( 'No nonce or issuer_state was found in the Proof of Possession' )
689695 }
690- let createdAt : number
696+
697+ let createdAt : number = + new Date ( )
691698 let cNonceState : CNonceState | undefined
692699 if ( nonce ) {
693700 cNonceState = await this . cNonces . getAsserted ( nonce )
694- preAuthorizedCode = cNonceState . preAuthorizedCode
695- issuerState = cNonceState . issuerState
696701 createdAt = cNonceState . createdAt
697- } else if ( issuer_state ) {
702+ }
703+ if ( issuer_state ) {
698704 const session = await this . _credentialOfferSessions . getAsserted ( issuer_state as string )
699705 issuerState = issuer_state as string | undefined
700706 createdAt = session . createdAt
701- } else {
702- throw Error ( 'No nonce or issuer_state was found in the Proof of Possession' )
703707 }
708+
704709 // The verify callback should set the correct values, but let's look at the JWT ourselves to to be sure
705710 const alg = jwtVerifyResult . alg ?? header . alg
706711 const kid = jwtVerifyResult . kid ?? header . kid
@@ -728,18 +733,19 @@ export class VcIssuer {
728733 throw Error ( DID_NO_DIDDOC_ERROR )
729734 }
730735
731- const preAuthSession = preAuthorizedCode ? await this . credentialOfferSessions . get ( preAuthorizedCode ) : undefined
736+ const preAuthSession = issuerCorrelation . preAuthorizedCode
737+ ? await this . credentialOfferSessions . get ( issuerCorrelation . preAuthorizedCode ) : undefined
732738 const authSession = issuerState ? await this . credentialOfferSessions . get ( issuerState ) : undefined
733739 if ( ! preAuthSession && ! authSession ) {
734740 throw Error ( 'Either a pre-authorized code or issuer state needs to be present' )
735741 }
736742 if ( preAuthSession ) {
737- if ( ! preAuthSession . preAuthorizedCode || preAuthSession . preAuthorizedCode !== preAuthorizedCode ) {
743+ if ( ! preAuthSession . preAuthorizedCode || preAuthSession . preAuthorizedCode !== issuerCorrelation . preAuthorizedCode ) {
738744 throw Error ( 'Invalid pre-authorized code' )
739745 }
740746 preAuthSession . lastUpdatedAt = + new Date ( )
741747 preAuthSession . status = IssueStatus . CREDENTIAL_REQUEST_RECEIVED
742- await this . _credentialOfferSessions . set ( preAuthorizedCode , preAuthSession )
748+ await this . _credentialOfferSessions . set ( issuerCorrelation . preAuthorizedCode , preAuthSession )
743749 }
744750 if ( authSession ) {
745751 if ( ! authSession . issuerState || authSession . issuerState !== issuerState ) {
@@ -781,9 +787,9 @@ export class VcIssuer {
781787 }
782788 // todo: Add a check of iat against current TS on server with a skew
783789
784- return { jwtVerifyResult, preAuthorizedCode, preAuthSession, issuerState, authSession, cNonceState }
790+ return { jwtVerifyResult, preAuthorizedCode : issuerCorrelation . preAuthorizedCode , preAuthSession, issuerState, authSession, cNonceState }
785791 } catch ( error : unknown ) {
786- await this . updateSession ( { preAuthorizedCode, issuerState, error } )
792+ await this . updateSession ( { preAuthorizedCode : issuerCorrelation . preAuthorizedCode , issuerState, error } )
787793 throw error
788794 }
789795 }
0 commit comments