@@ -52,7 +52,6 @@ export class WaltIdPolicyHandler extends PolicyHandler {
5252 const url = new URL ( `/openid4vc/verify` , process . env . WALTID_VERIFIER_URL )
5353
5454 const requestCredentialsBody = this . parseRequestCredentials ( requestPayload )
55-
5655 const headers = {
5756 stateId : uuid ,
5857 successRedirectUri,
@@ -671,6 +670,7 @@ export class WaltIdPolicyHandler extends PolicyHandler {
671670 private parseRequestCredentials ( requestPayload : any ) : any {
672671 const credentialSubject = requestPayload ?. ddo ?. credentialSubject
673672 const targetType = 'SSIpolicy'
673+
674674 const credentialSubjectCredentials =
675675 credentialSubject ?. credentials ?. allow
676676 ?. filter ( ( item : any ) => item ?. type === targetType )
@@ -699,7 +699,29 @@ export class WaltIdPolicyHandler extends PolicyHandler {
699699 const combinedCredentials = [
700700 ...new Set ( [ ...credentialSubjectCredentials , ...serviceCredentials ] )
701701 ]
702- const normalizePolicies = ( arr : any [ ] ) : string [ ] =>
702+
703+ const parsePolicies = ( policies : any [ ] | undefined ) => {
704+ if ( ! Array . isArray ( policies ) ) return [ ]
705+ return policies
706+ . map ( ( policy ) => {
707+ if ( policy && typeof policy === 'object' ) {
708+ return policy
709+ }
710+ if ( typeof policy === 'string' ) {
711+ try {
712+ return JSON . parse ( policy )
713+ } catch ( error ) {
714+ console . error ( error )
715+ logError ( error )
716+ return undefined
717+ }
718+ }
719+ return undefined
720+ } )
721+ . filter ( ( p ) => p !== undefined )
722+ }
723+
724+ const normalizePolicyNames = ( arr : any [ ] ) : string [ ] =>
703725 arr
704726 . map ( ( p ) =>
705727 typeof p === 'string'
@@ -713,48 +735,78 @@ export class WaltIdPolicyHandler extends PolicyHandler {
713735 const vp_policies = new Set < string > ( )
714736 const vc_policies = new Set < string > ( )
715737
716- const envvp_policies = process . env . DEFAULT_VP_POLICIES
717- ? JSON . parse ( process . env . DEFAULT_VP_POLICIES )
718- : [ ]
719- const envvc_policies = process . env . DEFAULT_VC_POLICIES
720- ? JSON . parse ( process . env . DEFAULT_VC_POLICIES )
721- : [ ]
722- normalizePolicies ( envvp_policies ) . forEach ( ( pol ) => vp_policies . add ( pol ) )
723- normalizePolicies ( envvc_policies ) . forEach ( ( pol ) => vc_policies . add ( pol ) )
738+ let envvp_policies : any [ ] = [ ]
739+ let envvc_policies : any [ ] = [ ]
740+ try {
741+ envvp_policies = process . env . DEFAULT_VP_POLICIES
742+ ? JSON . parse ( process . env . DEFAULT_VP_POLICIES )
743+ : [ ]
744+ } catch ( e ) {
745+ console . error ( 'Failed to parse DEFAULT_VP_POLICIES' , e )
746+ logError ( e )
747+ }
748+ try {
749+ envvc_policies = process . env . DEFAULT_VC_POLICIES
750+ ? JSON . parse ( process . env . DEFAULT_VC_POLICIES )
751+ : [ ]
752+ } catch ( e ) {
753+ console . error ( 'Failed to parse DEFAULT_VC_POLICIES' , e )
754+ logError ( e )
755+ }
756+
757+ normalizePolicyNames ( envvp_policies ) . forEach ( ( pol ) => vp_policies . add ( pol ) )
758+ normalizePolicyNames ( envvc_policies ) . forEach ( ( pol ) => vc_policies . add ( pol ) )
724759
725760 const request_credentialsMap = new Map < string , any > ( )
726761
727762 combinedCredentials . forEach ( ( entry : any ) => {
728- if ( entry . vp_policies )
729- entry . vp_policies . forEach ( ( policy : string ) => vp_policies . add ( policy ) )
730- if ( entry . vc_policies )
731- entry . vc_policies . forEach ( ( policy : string ) => vc_policies . add ( policy ) )
763+ if ( entry ?. vp_policies ) {
764+ ; ( Array . isArray ( entry . vp_policies )
765+ ? entry . vp_policies
766+ : [ entry . vp_policies ]
767+ ) . forEach ( ( policy : any ) => {
768+ if ( typeof policy === 'string' ) vp_policies . add ( policy )
769+ else if (
770+ policy &&
771+ typeof policy === 'object' &&
772+ typeof policy . policy === 'string'
773+ ) {
774+ vp_policies . add ( policy . policy )
775+ }
776+ } )
777+ }
778+
779+ if ( entry ?. vc_policies ) {
780+ ; ( Array . isArray ( entry . vc_policies )
781+ ? entry . vc_policies
782+ : [ entry . v_cpolicies ]
783+ ) . forEach ( ( policy : any ) => {
784+ if ( typeof policy === 'string' ) vc_policies . add ( policy )
785+ else if (
786+ policy &&
787+ typeof policy === 'object' &&
788+ typeof policy . policy === 'string'
789+ ) {
790+ vc_policies . add ( policy . policy )
791+ }
792+ } )
793+ }
794+
795+ const reqCreds : any [ ] = entry ?. request_credentials ?? [ ]
796+ reqCreds . forEach ( ( credentialRequest : any ) => {
797+ const parsedPolicies = parsePolicies ( credentialRequest ?. policies )
732798
733- entry . request_credentials . forEach ( ( credentialRequest : any ) => {
734799 const uniqueKey = JSON . stringify ( {
735- type : credentialRequest . type ,
736- format : credentialRequest . format ,
737- policies : credentialRequest . policies
800+ type : credentialRequest ? .type ,
801+ format : credentialRequest ? .format ,
802+ policies : parsedPolicies
738803 } )
739804
740805 if ( ! request_credentialsMap . has ( uniqueKey ) ) {
741806 request_credentialsMap . set ( uniqueKey , {
742- type : credentialRequest . type ,
743- format : credentialRequest . format ,
744- policies : credentialRequest . policies
745- ?. map ( ( policy : any ) => {
746- if ( typeof policy === 'string' ) {
747- return policy
748- }
749- if ( typeof policy === 'object' && policy . policy ) {
750- return {
751- policy : policy . policy ,
752- args : policy . args
753- }
754- }
755- return undefined
756- } )
757- . filter ( Boolean )
807+ type : credentialRequest ?. type ,
808+ format : credentialRequest ?. format ,
809+ policies : parsedPolicies
758810 } )
759811 }
760812 } )
0 commit comments