From 5dcbe72183f1a025cfbb9dd8ee9e86ed0ba1a063 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Tue, 19 Mar 2024 08:35:39 -0700 Subject: [PATCH] Add jwt sign cmd --- cmd/web5/cmd_jwt.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ cmd/web5/main.go | 4 ++++ 2 files changed, 56 insertions(+) create mode 100644 cmd/web5/cmd_jwt.go diff --git a/cmd/web5/cmd_jwt.go b/cmd/web5/cmd_jwt.go new file mode 100644 index 0000000..0cb1d0d --- /dev/null +++ b/cmd/web5/cmd_jwt.go @@ -0,0 +1,52 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/tbd54566975/web5-go/dids/did" + "github.com/tbd54566975/web5-go/jwt" +) + +type jwtSignCMD struct { + Claims string `arg:"" help:"The JWT Claims. Value is a JSON string."` + PortableDID string `arg:"" help:"The Portable DID to sign with. Value is a JSON string."` + Purpose string `help:"Used to specify which key from the given DID Document should be used."` + Type string `help:"Used to set the JWS Header 'typ' property"` +} + +func (c *jwtSignCMD) Run() error { + var claims jwt.Claims + err := json.Unmarshal([]byte(c.Claims), &claims) + if err != nil { + return fmt.Errorf("%s: %w", "invalid credential", err) + } + + var portableDID did.PortableDID + err = json.Unmarshal([]byte(c.PortableDID), &portableDID) + if err != nil { + return fmt.Errorf("%s: %w", "invalid portable DID", err) + } + + bearerDID, err := did.FromPortableDID(portableDID) + if err != nil { + return err + } + + opts := []jwt.SignOpt{} + if c.Purpose != "" { + opts = append(opts, jwt.Purpose(c.Purpose)) + } + if c.Type != "" { + opts = append(opts, jwt.Type(c.Type)) + } + + signed, err := jwt.Sign(claims, bearerDID, opts...) + if err != nil { + return err + } + + fmt.Println(signed) + + return nil +} diff --git a/cmd/web5/main.go b/cmd/web5/main.go index c2d60bc..15032aa 100644 --- a/cmd/web5/main.go +++ b/cmd/web5/main.go @@ -11,6 +11,10 @@ import ( // // [kong documentation]: https://github.com/alecthomas/kong type CLI struct { + JWT struct { + Sign jwtSignCMD `cmd:"" help:"Sign a JWT."` + // todo decode and verify + } `cmd:"" help:"Interface with JWT's."` DID struct { Resolve didResolveCMD `cmd:"" help:"Resolve a DID."` Create didCreateCMD `cmd:"" help:"Create a DID."`