@@ -56,6 +56,7 @@ const (
5656 NewDIDCommandMethod = "NewDID"
5757 DoDeviceEnrolmentCommandMethod = "DoDeviceEnrolment"
5858 GenerateVPCommandMethod = "GenerateVP"
59+ GetVCredentialCommandMethod = "GetVCredential"
5960 AcceptEnrolmentCommandMethod = "AcceptEnrolment"
6061 VerifyCredentialCommandMethod = "ValidateVP"
6162 TestingCallMethod = "TestingCall"
@@ -65,6 +66,8 @@ const (
6566 errEmptyUrl = "url is mandatory"
6667 errEmptyDID = "theirDid is mandatory"
6768 errEmptyIdProofs = "idProofs is mandatory"
69+ erremptyCredId = "credId is mandatory"
70+ errEmptyQueryByFrame = "querybyframe is mandatory"
6871
6972 // log constants.
7073 didID = "did"
@@ -237,6 +240,18 @@ func (o *Command) NewDID(rw io.Writer, req io.Reader) command.Error {
237240 logutil .LogInfo (logger , CommandName , NewDIDCommandMethod , "invalid key type" )
238241 return command .NewValidationError (InvalidRequestErrorCode , fmt .Errorf ("invalid key type" ))
239242 }
243+ //parse number of keypurpose.keytype.Attrs for increment in 1
244+ if (len (keyPurpose .KeyType .Attrs )> 0 ){
245+ nAttrs := keyPurpose .KeyType .Attrs [0 ]
246+ nAug ,err := strconv .Atoi (nAttrs )
247+ if err != nil {
248+ logutil .LogInfo (logger , CommandName , NewDIDCommandMethod , "parse number of key purpose key type attrs error" )
249+ return command .NewValidationError (NewDIDRequestErrorCode , fmt .Errorf ("parse number of key purpose key type attrs error: %w" , err ))
250+ }
251+ nAug = nAug + 1
252+ newAttrNumber := strconv .Itoa (nAug )
253+ keyPurpose .KeyType .Attrs [0 ] = newAttrNumber
254+ }
240255 reader , err = getReader (& vcwalletc.CreateKeyPairRequest {
241256 KeyType : kt ,
242257 WalletAuth : vcwalletc.WalletAuth {UserID : o .walletuid , Auth : token },
@@ -351,13 +366,12 @@ func (o *Command) NewDID(rw io.Writer, req io.Reader) command.Error {
351366 command .WriteNillableResponse (rw , & NewDIDResult {DIDDoc : parsedResponse .DID }, logger )
352367 logutil .LogInfo (logger , CommandName , NewDIDCommandMethod , "success" )
353368 //testing
354- o .signJWT (token )
355369 return nil
356370}
357371
358372
359373
360- func (o * Command ) signJWT (token string ) {
374+ func (o * Command ) signJWT (token string ) string {
361375
362376 request := vcwalletc.SignJWTRequest {
363377 WalletAuth : vcwalletc.WalletAuth {UserID : o .walletuid , Auth : token },
@@ -396,9 +410,29 @@ func (o * Command) signJWT(token string) {
396410 signedJWT := jwtResponse .JWT
397411 fmt .Println ("Signed JWT:" , signedJWT )
398412
413+ // Verify JWT
414+ // verifyReq := &vcwalletc.VerifyJWTRequest{
415+ // WalletAuth: vcwalletc.WalletAuth{UserID: o.walletuid, Auth: token},
416+ // JWT: signedJWT,
417+ // }
418+
419+ // verifyReqBytes, _ := json.Marshal(verifyReq)
420+ // verifyReqReader := bytes.NewReader(verifyReqBytes)
421+ // var verifyBuf bytes.Buffer
422+
423+ // err = o.vcwalletcommand.VerifyJWT(&verifyBuf, verifyReqReader)
424+ // if err != nil {
425+ // logutil.LogInfo(logger, CommandName, "SignJWT", "failed to verify JWT: "+err.Error())
426+ // }
427+ // fmt.Println("Verification result:", verifyBuf.String())
428+ return signedJWT
429+ }
430+
431+ //verifyJWT
432+ func (o * Command ) verifyJWT (token string , signedJWT string ) bool {
399433
400434 // Verify JWT
401- verifyReq := & vcwalletc.VerifyJWTRequest {
435+ verifyReq := & vcwalletc.VerifyJWTRequest {
402436 WalletAuth : vcwalletc.WalletAuth {UserID : o .walletuid , Auth : token },
403437 JWT : signedJWT ,
404438 }
@@ -407,13 +441,26 @@ func (o * Command) signJWT(token string) {
407441 verifyReqReader := bytes .NewReader (verifyReqBytes )
408442 var verifyBuf bytes.Buffer
409443
410- err = o .vcwalletcommand .VerifyJWT (& verifyBuf , verifyReqReader )
444+ err : = o .vcwalletcommand .VerifyJWT (& verifyBuf , verifyReqReader )
411445 if err != nil {
412446 logutil .LogInfo (logger , CommandName , "SignJWT" , "failed to verify JWT: " + err .Error ())
413447 }
414448 fmt .Println ("Verification result:" , verifyBuf .String ())
449+ //wrapp verifyBuf in VerifyJWTResponse
450+
451+ var jwtVerifyResponse vcwalletc.VerifyJWTResponse
452+
453+ errResp := json .Unmarshal (verifyBuf .Bytes (), & jwtVerifyResponse )
454+ if errResp != nil {
455+ logutil .LogInfo (logger , CommandName , "VerifyJWT" , "failed to unmarshal JWT Verify Response: " + err .Error ())
456+ }
457+
458+ isVerified := jwtVerifyResponse .Verified
459+ return isVerified
460+
415461}
416462
463+
417464// DoDeviceEnrolment Device completes an enrolment process against an issuer
418465func (o * Command ) DoDeviceEnrolment (rw io.Writer , req io.Reader ) command.Error {
419466 //Parse request
@@ -442,10 +489,42 @@ func (o *Command) DoDeviceEnrolment(rw io.Writer, req io.Reader) command.Error {
442489
443490 identityProods := request .IdProofs
444491
492+ //add current did to idProofs and sign with DID proofData with signJWT function
445493
494+ //Open wallet
495+ var l bytes.Buffer
496+ reader , err := getReader (& vcwalletc.UnlockWalletRequest {
497+ UserID : o .walletuid ,
498+ LocalKMSPassphrase : o .walletpass ,
499+ })
500+ if err != nil {
501+ return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
502+ }
503+ err = o .vcwalletcommand .Open (& l , reader )
504+ if err != nil {
505+ return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
506+ }
507+ token := getUnlockToken (l )
508+ if token == "" {
509+ logutil .LogInfo (logger , CommandName , DoDeviceEnrolmentCommandMethod , "could not get unlock token (empty token)" )
510+ return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error decoding token" ))
511+ }
512+ //Defer close wallet
513+ defer func () {
514+ var l2 bytes.Buffer
515+ reader , err = getReader (& vcwalletc.LockWalletRequest {
516+ UserID : o .walletuid ,
517+ })
518+ err = o .vcwalletcommand .Close (& l2 , reader )
519+ //TODO UMU See how to treat errors in this case
520+ }()
446521
447522
448523
524+ //proofData := o.signJWT(token)
525+ //proofDataBytes := json.RawMessage(proofData)
526+ identityProods = append (identityProods , IdProof {AttrName : "DID" , AttrValue : o .currentDID })
527+
449528 // Do a post for AcceptEnrolmentResult to specified url
450529 acceptEnrolmentRequest := AcceptEnrolmentArgs {IdProofs : identityProods }
451530 jsonBody , err := json .Marshal (acceptEnrolmentRequest )
@@ -478,33 +557,7 @@ func (o *Command) DoDeviceEnrolment(rw io.Writer, req io.Reader) command.Error {
478557 return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("credential issuance was not completed: %s" , res ))
479558 }
480559
481- //Open wallet
482- var l bytes.Buffer
483- reader , err := getReader (& vcwalletc.UnlockWalletRequest {
484- UserID : o .walletuid ,
485- LocalKMSPassphrase : o .walletpass ,
486- })
487- if err != nil {
488- return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
489- }
490- err = o .vcwalletcommand .Open (& l , reader )
491- if err != nil {
492- return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
493- }
494- token := getUnlockToken (l )
495- if token == "" {
496- logutil .LogInfo (logger , CommandName , DoDeviceEnrolmentCommandMethod , "could not get unlock token (empty token)" )
497- return command .NewValidationError (DoDeviceEnrolmentRequestErrorCode , fmt .Errorf ("open wallet error decoding token" ))
498- }
499- //Defer close wallet
500- defer func () {
501- var l2 bytes.Buffer
502- reader , err = getReader (& vcwalletc.LockWalletRequest {
503- UserID : o .walletuid ,
504- })
505- err = o .vcwalletcommand .Close (& l2 , reader )
506- //TODO UMU See how to treat errors in this case
507- }()
560+
508561 //Store cred in wallet
509562 serialCred , err := res .Credential .MarshalJSON ()
510563
@@ -538,6 +591,73 @@ func (o *Command) DoDeviceEnrolment(rw io.Writer, req io.Reader) command.Error {
538591}
539592
540593
594+ func (o * Command ) GetVCredential (rw io.Writer , req io.Reader ) command.Error {
595+ var request GetVCredentialArgs
596+ err := json .NewDecoder (req ).Decode (& request )
597+ if err != nil {
598+ logutil .LogInfo (logger , CommandName , GetVCredentialCommandMethod , err .Error ())
599+ return command .NewValidationError (InvalidRequestErrorCode , fmt .Errorf ("request decode : %w" , err ))
600+ }
601+ if request .CredId == "" {
602+ logutil .LogInfo (logger , CommandName , GetVCredentialCommandMethod , erremptyCredId )
603+ return command .NewValidationError (InvalidRequestErrorCode , fmt .Errorf (erremptyCredId ))
604+ }
605+ //Open wallet
606+ var l bytes.Buffer
607+ reader , err := getReader (& vcwalletc.UnlockWalletRequest {
608+ UserID : o .walletuid ,
609+ LocalKMSPassphrase : o .walletpass ,
610+ })
611+ if err != nil {
612+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
613+ }
614+ err = o .vcwalletcommand .Open (& l , reader )
615+ if err != nil {
616+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("open wallet error: %w" , err ))
617+ }
618+ token := getUnlockToken (l )
619+ if token == "" {
620+ logutil .LogInfo (logger , CommandName , GenerateVPCommandMethod , "failed to get unlock token (empty token)" )
621+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("open wallet error decoding token" ))
622+ }
623+ //Defer close wallet
624+ defer func () {
625+ var l2 bytes.Buffer
626+ reader , err = getReader (& vcwalletc.LockWalletRequest {
627+ UserID : o .walletuid ,
628+ })
629+ err = o .vcwalletcommand .Close (& l2 , reader )
630+ }()
631+ //Get stored credential from Id
632+ //var credID = request.CredId
633+ reader , err = getReader (& vcwalletc.GetContentRequest {
634+ ContentID : request .CredId ,
635+ ContentType : wallet .Credential ,
636+ WalletAuth : vcwalletc.WalletAuth {UserID : o .walletuid , Auth : token },
637+ })
638+
639+
640+ var getResponse bytes.Buffer
641+ err = o .vcwalletcommand .Get (& getResponse , reader )
642+ if err != nil {
643+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("retrieve credential error: %w" , err ))
644+ }
645+ var parsedResponse vcwalletc.GetContentResponse
646+ err = json .NewDecoder (& getResponse ).Decode (& parsedResponse )
647+ if err != nil {
648+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("retrieve credential error: %w" , err ))
649+ }
650+
651+
652+
653+ if err != nil {
654+ return command .NewValidationError (GenerateVPRequestErrorCode , fmt .Errorf ("failed to decode stored credential: %w" , err ))
655+ }
656+
657+ command .WriteNillableResponse (rw , & GetVCredentialResult {parsedResponse .Content }, logger )
658+ return nil
659+ }
660+
541661// GenerateVP Device generates VPresentation (or VCredential for now) for an authorization process
542662func (o * Command ) GenerateVP (rw io.Writer , req io.Reader ) command.Error {
543663 //TODO UMU For now we use ContentId, but we should do it through query or similar and might even be simpler
@@ -549,14 +669,15 @@ func (o *Command) GenerateVP(rw io.Writer, req io.Reader) command.Error {
549669 logutil .LogInfo (logger , CommandName , GenerateVPCommandMethod , err .Error ())
550670 return command .NewValidationError (InvalidRequestErrorCode , fmt .Errorf ("request decode : %w" , err ))
551671 }
552- if request .CredId == "" {
553- logutil .LogInfo (logger , CommandName , GenerateVPCommandMethod , errEmptyUrl )
554- return command .NewValidationError (InvalidRequestErrorCode , fmt .Errorf (errEmptyUrl ))
555- }
556- // if request.Frame.data == nil {
672+ // if request.CredId == "" {
673+ // logutil.LogInfo(logger, CommandName, GenerateVPCommandMethod, errEmptyUrl)
674+ // return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(erremptyCredId ))
675+ // }
676+ // if request.QueryByFrame == nil {
557677 // logutil.LogInfo(logger, CommandName, GenerateVPCommandMethod, errEmptyUrl)
558- // return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyUrl ))
678+ // return command.NewValidationError(InvalidRequestErrorCode, fmt.Errorf(errEmptyQueryByFrame ))
559679 // }
680+
560681 //Open wallet
561682 var l bytes.Buffer
562683 reader , err := getReader (& vcwalletc.UnlockWalletRequest {
@@ -873,7 +994,7 @@ func (o *Command) AcceptEnrolment(rw io.Writer, req io.Reader) command.Error {
873994 return nil
874995}
875996
876- // GetTrustedIssuerList returns the list of trusted issuers, mocked for nowq
997+ // GetTrustedIssuerList returns the list of trusted issuers, mocked for now
877998func (o * Command ) GetTrustedIssuerList (rw io.Writer , req io.Reader ) command.Error {
878999 //TODO UMU: Implement
8791000 trustedIssuer := TrustedIssuer {
0 commit comments