Skip to content

Commit

Permalink
Add jwt sign cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe committed Mar 19, 2024
1 parent e830fb3 commit 5dcbe72
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/web5/cmd_jwt.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions cmd/web5/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
Expand Down

0 comments on commit 5dcbe72

Please sign in to comment.