Skip to content

Commit 368552a

Browse files
committed
Changes to add DID as id proof
1 parent 4914252 commit 368552a

File tree

4 files changed

+177
-45
lines changed

4 files changed

+177
-45
lines changed

pkg/controller/command/poc/command.go

Lines changed: 159 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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
418465
func (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
542662
func (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
877998
func (o *Command) GetTrustedIssuerList(rw io.Writer, req io.Reader) command.Error {
878999
//TODO UMU: Implement
8791000
trustedIssuer := TrustedIssuer{

pkg/controller/command/poc/models.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ type GenerateVPArgs struct {
6969
QueryByFrame QueryByFrame `json:"querybyframe,omitempty"`
7070
}
7171

72+
// Model for GetVCredential method input
73+
type GetVCredentialArgs struct {
74+
CredId string `json:"credId,omitempty"`
75+
}
7276

7377
type RequestBodyVP struct {
7478
CredId string `json:"credId,omitempty"`
@@ -117,6 +121,10 @@ type GenerateVPResultCustom struct {
117121

118122
}
119123

124+
type GetVCredentialResult struct {
125+
Credential json.RawMessage `json:"credential,omitempty"`
126+
}
127+
120128
// Model for AcceptEnrolment method input
121129
type AcceptEnrolmentArgs struct {
122130
IdProofs []IdProof `json:"idProofs,omitempty"`

pkg/controller/rest/poc/operation.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (o *Operation) registerHandler() {
7171
}
7272
}
7373

74-
// NewDID swagger:route POST /poc/newDID poc newDIDReq
74+
// NewDID swagger:route POST /fluidos/idm/generateDID poc newDIDReq
7575
//
7676
// Create DID with keys/purposes as specified in request
7777
//
@@ -82,7 +82,7 @@ func (o *Operation) NewDID(rw http.ResponseWriter, req *http.Request) {
8282
rest.Execute(o.command.NewDID, rw, req.Body)
8383
}
8484

85-
// DoDeviceEnrolment swagger:route POST /poc/doDeviceEnrolment poc DoDeviceEnrolmentReq
85+
// DoEnrolment swagger:route POST /fluidos/idm/doEnrolment poc DoDeviceEnrolmentReq
8686
//
8787
// Do an enrolment process against the issuer, obtaining a new credential
8888
//
@@ -93,7 +93,7 @@ func (o *Operation) DoDeviceEnrolment(rw http.ResponseWriter, req *http.Request)
9393
rest.Execute(o.command.DoDeviceEnrolment, rw, req.Body)
9494
}
9595

96-
// GenerateVp swagger:route POST /poc/generateVp poc GenerateVpReq
96+
// GenerateVp swagger:route POST /fluidos/idm/generateVp poc GenerateVpReq
9797
//
9898
// Generate a VPresentation (for now VCredential?) for an authorization process
9999
//
@@ -104,7 +104,7 @@ func (o *Operation) GenerateVp(rw http.ResponseWriter, req *http.Request) {
104104
rest.Execute(o.command.GenerateVP, rw, req.Body)
105105
}
106106

107-
// AcceptDeviceEnrolment swagger:route POST /poc/acceptDeviceEnrolment poc AcceptDeviceEnrolmentReq
107+
// AcceptDeviceEnrolment swagger:route POST /fluidos/idm/acceptEnrolment poc AcceptDeviceEnrolmentReq
108108
//
109109
// Accepts enrolment requests, and if successful generates a Verifiable Credential for the enrolled device
110110
//
@@ -115,7 +115,7 @@ func (o *Operation) AcceptDeviceEnrolment(rw http.ResponseWriter, req *http.Requ
115115
rest.Execute(o.command.AcceptEnrolment, rw, req.Body)
116116
}
117117

118-
// VerifyCredential swagger:route POST /poc/VerifyCredential poc VerifyCredentialReq
118+
// VerifyCredential swagger:route POST /fluidos/idm/VerifyCredential poc VerifyCredentialReq
119119
//
120120
// Verify a Verifiable Credential, returns boolean of the verification result
121121
//
@@ -133,7 +133,7 @@ func (o *Operation) TestingCall(rw http.ResponseWriter, req *http.Request) {
133133
rest.Execute(o.command.TestingCall, rw, req.Body)
134134
}
135135

136-
// GetTrustedIssuerList swagger:route GET /poc/trustedIssuers poc GetTrustedIssuerListReq
136+
// GetTrustedIssuerList swagger:route GET /fluidos/idm/trustedIssuers poc GetTrustedIssuerListReq
137137
//
138138
// Get the list of trusted issuers
139139
//

pkg/doc/ldcontext/embed/third_party/umu/poc.jsonld

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"fluidosRole": "ex:fluidosRole",
1616
"association": "ex:association",
1717
"deviceType": "ex:deviceType",
18+
"deviceName": "ex:deviceName",
1819
"fluidosID": "ex:fluidosID",
1920
"holderAddress": "ex:holderAddress",
2021
"holderEmail": "ex:holderEmail",
@@ -32,7 +33,9 @@
3233
"state": "ex:state",
3334
"province": "ex:province",
3435
"country": "ex:country",
35-
"dn": "ex:dn"
36+
"dn": "ex:dn",
37+
"flavourList": "ex:flavourList",
38+
"aggregator": "ex:aggregator"
3639
}
3740
]
3841
}

0 commit comments

Comments
 (0)