Skip to content

Commit 2a5a883

Browse files
committed
changes for idproofing with JWT Working
1 parent 0150bb3 commit 2a5a883

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

pkg/controller/command/poc/command.go

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const (
7575

7676
// provider contains dependencies for the vdr controller command operations
7777
// and is typically created by using aries.Context().
78-
type provider interface {
78+
type Provider interface {
7979
StorageProvider() storage.Provider
8080
VDRegistry() vdr.Registry
8181
Crypto() crypto.Crypto
@@ -90,7 +90,10 @@ type Command struct {
9090
walletuid string
9191
walletpass string
9292
currentDID string //TODO UMU For retrieval of device DIDdoc, think about better implementation
93+
currentKeyPair vcwalletc.CreateKeyPairResponse
9394
idProofValidators []IdProofValidator
95+
ctx Provider
96+
9497
}
9598

9699

@@ -99,6 +102,8 @@ type Command struct {
99102
var verifyMem = uint64(0)
100103

101104

105+
106+
102107
// New returns new poc client controller command instance.
103108
func New(vdrcommand *vdrc.Command, vcwalletcommand *vcwalletc.Command) (*Command, error) {
104109
var idProofValidators []IdProofValidator
@@ -279,6 +284,7 @@ func (o *Command) NewDID(rw io.Writer, req io.Reader) command.Error {
279284
case "Authentication":
280285
doc.Authentication = append(doc.Authentication, did.Verification{VerificationMethod: verificationMethod,
281286
Relationship: did.Authentication})
287+
o.currentKeyPair = parsedResponse
282288
case "CapabilityDelegation":
283289
doc.CapabilityDelegation = append(doc.CapabilityDelegation, did.Verification{VerificationMethod: verificationMethod,
284290
Relationship: did.CapabilityDelegation})
@@ -288,6 +294,7 @@ func (o *Command) NewDID(rw io.Writer, req io.Reader) command.Error {
288294
default: //If nothing we assume authentication
289295
doc.Authentication = append(doc.AssertionMethod, did.Verification{VerificationMethod: verificationMethod,
290296
Relationship: did.Authentication})
297+
o.currentKeyPair = parsedResponse
291298
}
292299
}
293300
now := time.Now()
@@ -339,24 +346,129 @@ func (o *Command) NewDID(rw io.Writer, req io.Reader) command.Error {
339346
// finished
340347
command.WriteNillableResponse(rw, &NewDIDResult{DIDDoc: parsedResponse.DID}, logger)
341348
logutil.LogInfo(logger, CommandName, NewDIDCommandMethod, "success")
349+
//testing
350+
o.signJWT(token)
342351
return nil
343352
}
344353

345354

346355

347-
func getSignedProof()(string) {
356+
357+
func (o * Command) getSignedProof()(string) {
348358
randomString , err := generateRandomString(15)
349359
if err != nil {
350360
fmt.Println("Error generating random string:", err)
351361
return ""
352362
}
353363

364+
//Get DID/DIDDoc for specifying key, issuer...
365+
// reader, err := getReader(&vdrc.IDArg{
366+
// ID: o.currentDID,
367+
// })
368+
// var getResponse bytes.Buffer
369+
// err = o.vdrcommand.GetDID(&getResponse, reader)
370+
// if err != nil {
371+
// logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to get DID: "+err.Error())
372+
// }
373+
// var parsedDoc vdrc.Document
374+
// err = json.NewDecoder(&getResponse).Decode(&parsedDoc)
375+
// if err != nil {
376+
// logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to decode DID Document: "+err.Error())
377+
// }
378+
// didDoc, err := did.ParseDocument(parsedDoc.DID)
379+
// if err != nil {
380+
// logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to parse DID Document: "+err.Error())
381+
// }
382+
// fmt.Println("DID:", didDoc.ID)
383+
354384

355-
385+
message := []byte(randomString)
386+
387+
cryptoService := o.ctx.Crypto()
388+
// Sign a random string
389+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "keypairKEYID "+o.currentKeyPair.KeyID)
390+
signature, err := cryptoService.Sign(message, o.currentKeyPair.KeyID)
391+
if err != nil {
392+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to sign message: "+err.Error())
393+
}
394+
395+
fmt.Println("Signature:", signature)
356396

397+
// Verify the signature
398+
valid := cryptoService.Verify(signature,message, o.currentKeyPair.PublicKey)
399+
if valid == nil {
400+
fmt.Println("Signature verification successful!")
401+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "Signature verification successful!")
402+
} else {
403+
fmt.Println("Signature verification failed.")
404+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "Signature verification failed.")
405+
}
357406
return randomString
358407
}
359408

409+
func (o * Command) signJWT(token string)(string) {
410+
randomString , err := generateRandomString(15)
411+
if err != nil {
412+
fmt.Println("Error generating random string:", err)
413+
return ""
414+
}
415+
416+
request := vcwalletc.SignJWTRequest{
417+
WalletAuth: vcwalletc.WalletAuth{UserID: o.walletuid, Auth: token},
418+
Headers: nil,
419+
Claims: map[string]interface{}{
420+
"attrName": "DID",
421+
"attrValue": o.currentDID,
422+
},
423+
KID: o.currentDID+"#"+o.currentKeyPair.KeyID,
424+
}
425+
426+
reqData, err := json.Marshal(request)
427+
if err != nil {
428+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to marshal request: "+err.Error())
429+
}
430+
req := bytes.NewReader(reqData)
431+
// Capture the output
432+
var signBuf bytes.Buffer
433+
434+
// Sign the JWT
435+
if err := o.vcwalletcommand.SignJWT(&signBuf, req); err != nil {
436+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to sign JWT: "+err.Error())
437+
}
438+
439+
440+
var jwtResponse vcwalletc.SignJWTResponse
441+
442+
err = json.Unmarshal(signBuf.Bytes(), &jwtResponse)
443+
if err != nil {
444+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to unmarshal JWT: "+err.Error())
445+
}
446+
447+
448+
449+
450+
signedJWT := jwtResponse.JWT
451+
fmt.Println("Signed JWT:", signedJWT)
452+
453+
454+
// Verify JWT
455+
verifyReq := &vcwalletc.VerifyJWTRequest{
456+
WalletAuth: vcwalletc.WalletAuth{UserID: o.walletuid, Auth: token},
457+
JWT: signedJWT,
458+
}
459+
460+
verifyReqBytes, _ := json.Marshal(verifyReq)
461+
verifyReqReader := bytes.NewReader(verifyReqBytes)
462+
var verifyBuf bytes.Buffer
463+
464+
err = o.vcwalletcommand.VerifyJWT(&verifyBuf, verifyReqReader)
465+
if err != nil {
466+
logutil.LogInfo(logger, CommandName, AcceptEnrolmentCommandMethod, "failed to verify JWT: "+err.Error())
467+
}
468+
fmt.Println("Verification result:", verifyBuf.String())
469+
470+
return randomString
471+
}
360472

361473
// DoDeviceEnrolment Device completes an enrolment process against an issuer
362474
func (o *Command) DoDeviceEnrolment(rw io.Writer, req io.Reader) command.Error {

pkg/controller/rest/poc/operation.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010
"fmt"
1111
"net/http"
1212

13-
"github.com/hyperledger/aries-framework-go/pkg/controller/command/poc"
13+
poc"github.com/hyperledger/aries-framework-go/pkg/controller/command/poc"
1414
vcwalletc "github.com/hyperledger/aries-framework-go/pkg/controller/command/vcwallet"
1515
vdrc "github.com/hyperledger/aries-framework-go/pkg/controller/command/vdr"
16-
1716
"github.com/hyperledger/aries-framework-go/pkg/controller/internal/cmdutil"
1817
"github.com/hyperledger/aries-framework-go/pkg/controller/rest"
1918
)
@@ -29,6 +28,8 @@ const (
2928
TestingCallPath = PocOperationID + "/testingCall"
3029
)
3130

31+
32+
3233
// Operation contains basic common operations provided by controller REST API.
3334
type Operation struct {
3435
handlers []rest.Handler

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"holderEmail": "ex:holderEmail",
1919
"ou": "ex:ou",
2020
"physicalAddress": "ex:physicalAddress",
21+
"DID":"ex:DID",
2122
"macAddress": "ex:macAddress",
2223
"registrationDate": "ex:registrationDate",
2324
"orgIdentifier": "ex:orgIdentifier",

0 commit comments

Comments
 (0)