From 4bcfc4eaf1026ef21ce5c71b098530eecd86c2d7 Mon Sep 17 00:00:00 2001 From: johnabass Date: Mon, 5 Aug 2024 16:09:27 -0700 Subject: [PATCH] use the new parsing interface --- basculejwt/token.go | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/basculejwt/token.go b/basculejwt/token.go index b53bec9..e0dcb2d 100644 --- a/basculejwt/token.go +++ b/basculejwt/token.go @@ -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 { @@ -59,15 +49,13 @@ 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{ options: append( make([]jwt.ParseOption, 0, len(options)), options..., @@ -75,8 +63,10 @@ func NewTokenParser[S any](options ...jwt.ParseOption) (bascule.TokenParser[S], }, 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...) if err != nil { return nil, err }