Skip to content

Commit

Permalink
use the new parsing interface
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Aug 5, 2024
1 parent 93f541a commit 4bcfc4e
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions basculejwt/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ type Claims interface {
Subject() string
}

// Token is the interface implemented by JWT-based tokens supplied by this package.
// User-defined claims can be accessed through the bascule.Attributes interface.
//
// Note that the Princpal method returns the same value as the Subject claim.
type Token interface {
bascule.Token
bascule.Attributes
Claims
}

// token is the internal implementation of the JWT Token interface. It fronts
// a lestrrat-go Token.
type token struct {
Expand All @@ -59,24 +49,24 @@ func (t *token) Principal() string {

// tokenParser is the canonical parser for bascule that deals with JWTs.
// This parser does not use the source.
type tokenParser[S any] struct {
type tokenParser struct {
options []jwt.ParseOption
}

// NewTokenParser constructs a parser using the supplied set of parse options.
// The returned parser will produce tokens that implement the Token interface
// in this package.
func NewTokenParser[S any](options ...jwt.ParseOption) (bascule.TokenParser[S], error) {
return &tokenParser[S]{
func NewTokenParser(options ...jwt.ParseOption) (bascule.TokenParser[string], error) {
return &tokenParser{

Check warning on line 58 in basculejwt/token.go

View check run for this annotation

Codecov / codecov/patch

basculejwt/token.go#L57-L58

Added lines #L57 - L58 were not covered by tests
options: append(
make([]jwt.ParseOption, 0, len(options)),
options...,
),
}, nil
}

func (tp *tokenParser[S]) Parse(_ context.Context, _ S, c bascule.Credentials) (bascule.Token, error) {
jwtToken, err := jwt.ParseString(c.Value, tp.options...)
// Parse parses the value as a JWT, using the parsing options passed to NewTokenParser.
// The returned Token will implement the bascule.Attributes and Claims interfaces.
func (tp *tokenParser) Parse(ctx context.Context, value string) (bascule.Token, error) {
jwtToken, err := jwt.ParseString(value, tp.options...)

Check warning on line 69 in basculejwt/token.go

View check run for this annotation

Codecov / codecov/patch

basculejwt/token.go#L68-L69

Added lines #L68 - L69 were not covered by tests
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4bcfc4e

Please sign in to comment.