diff --git a/example_test.go b/example_test.go index d52f37a..bf4796a 100644 --- a/example_test.go +++ b/example_test.go @@ -52,13 +52,16 @@ func Example() { fmt.Printf("cid of derived UCAN: %s\n", id.String()) p := exampleParser() - _, err = p.ParseAndVerify(context.Background(), origin.Raw) + tok, err := p.ParseAndVerify(context.Background(), origin.Raw) panicIfError(err) + fmt.Printf("issuer DID key type: %s\n", tok.Issuer.Type().String()) + // Output: // cid of root UCAN: bafkreih6guuxohv47s2e366l6jn6stlsukgoerkdvtsni3kxr4jjmkaf3y // scope of ucan attenuations must be less than it's parent // cid of derived UCAN: bafkreihpk5474uoolkqrge3yk5uy2s7rarhn5xwxfoiobcy6ye7vfxetgm + // issuer DID key type: RSA } func panicIfError(err error) { diff --git a/token.go b/token.go index b3b0048..2cf5b0e 100644 --- a/token.go +++ b/token.go @@ -46,7 +46,9 @@ const ( // token a UCAN type Token struct { // Entire UCAN as a signed JWT string - Raw string + Raw string + Issuer didkey.ID + Subject didkey.ID // the "inputs" to this token, a chain UCAN tokens with broader scopes & // deadlines than this token Proofs []Proof `json:"prf,omitempty"` @@ -313,6 +315,30 @@ func (p *TokenParser) parseAndVerify(ctx context.Context, raw string, child *Tok return nil, fmt.Errorf("parser fail") } + var iss didkey.ID + // TODO(b5): we're double parsing here b/c the jwt lib we're using doesn't expose + // an API (that I know of) for storing parsed issuer / subjects + if issStr, ok := mc["iss"].(string); ok { + iss, err = didkey.Parse(issStr) + if err != nil { + return nil, err + } + } else { + return nil, fmt.Errorf(`"iss" key is not in claims`) + } + + var sub didkey.ID + // TODO(b5): we're double parsing here b/c the jwt lib we're using doesn't expose + // an API (that I know of) for storing parsed issuer / subjects + if subStr, ok := mc["sub"].(string); ok { + sub, err = didkey.Parse(subStr) + if err != nil { + return nil, err + } + } else { + return nil, fmt.Errorf(`"sub" key is not in claims`) + } + var att Attenuations if acci, ok := mc[AttKey].([]interface{}); ok { for i, a := range acci { @@ -345,6 +371,8 @@ func (p *TokenParser) parseAndVerify(ctx context.Context, raw string, child *Tok return &Token{ Raw: raw, + Issuer: iss, + Subject: sub, Attenuations: att, Proofs: prf, }, nil