From ecc370ca6e459eccf38ddd7a756e4faeb9f63d88 Mon Sep 17 00:00:00 2001 From: johnabass Date: Mon, 12 Aug 2024 17:35:58 -0700 Subject: [PATCH 1/2] renamed Attributes for consistency with other parts of the API --- attributes.go | 10 +++++----- attributes_test.go | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/attributes.go b/attributes.go index abbdef6..f16905d 100644 --- a/attributes.go +++ b/attributes.go @@ -3,9 +3,9 @@ package bascule -// Attributes is an optional interface that a Token may implement +// AttributesAccessor is an optional interface that a Token may implement // that provides access to arbitrary key/value pairs. -type Attributes interface { +type AttributesAccessor interface { // Get returns the value of an attribute, if it exists. Get(key string) (any, bool) } @@ -19,7 +19,7 @@ type Attributes interface { // type map[string]any or Attributes, this function will return false. // // If no keys are supplied, this function returns the zero value for T and false. -func GetAttribute[T any](a Attributes, keys ...string) (v T, ok bool) { +func GetAttribute[T any](a AttributesAccessor, keys ...string) (v T, ok bool) { if len(keys) == 0 { return } @@ -33,8 +33,8 @@ func GetAttribute[T any](a Attributes, keys ...string) (v T, ok bool) { continue } - var a Attributes - if a, ok = raw.(Attributes); ok { + var a AttributesAccessor + if a, ok = raw.(AttributesAccessor); ok { raw, ok = a.Get(keys[i]) } } diff --git a/attributes_test.go b/attributes_test.go index 9dbfc57..99cc9ad 100644 --- a/attributes_test.go +++ b/attributes_test.go @@ -21,7 +21,7 @@ type AttributesTestSuite struct { suite.Suite } -func (suite *AttributesTestSuite) testAttributes() Attributes { +func (suite *AttributesTestSuite) testAttributesAccessor() AttributesAccessor { return testAttributes{ "value": 123, "untypedNil": nil, @@ -31,16 +31,16 @@ func (suite *AttributesTestSuite) testAttributes() Attributes { "nestedMap": map[string]any{ "value": 123, }, - "nestedAttributes": Attributes(testAttributes{ + "nestedAttributes": AttributesAccessor(testAttributes{ "value": 123, }), }, - "nestedAttributes": Attributes(testAttributes{ + "nestedAttributes": AttributesAccessor(testAttributes{ "value": 123, "nestedMap": map[string]any{ "value": 123, }, - "nestedAttributes": Attributes(testAttributes{ + "nestedAttributes": AttributesAccessor(testAttributes{ "value": 123, }), }), @@ -117,7 +117,7 @@ func (suite *AttributesTestSuite) TestGetAttribute() { for _, testCase := range testCases { suite.Run(fmt.Sprintf("%v", testCase.keys), func() { - actual, ok := GetAttribute[int](suite.testAttributes(), testCase.keys...) + actual, ok := GetAttribute[int](suite.testAttributesAccessor(), testCase.keys...) suite.Equal(testCase.expectedValue, actual) suite.Equal(testCase.expectedOK, ok) }) From a28783442350f84ca3f1ffa5920591f72c9c2deb Mon Sep 17 00:00:00 2001 From: johnabass Date: Mon, 12 Aug 2024 17:42:07 -0700 Subject: [PATCH 2/2] ensure Token implements the new AttributesAccessor interface --- basculejwt/token.go | 4 ++++ basculejwt/token_test.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/basculejwt/token.go b/basculejwt/token.go index dc3af18..82b6fae 100644 --- a/basculejwt/token.go +++ b/basculejwt/token.go @@ -86,6 +86,10 @@ func (t token) Capabilities() (caps []string) { return } +func (t token) Get(key string) (any, bool) { + return t.jwt.Get(key) +} + // tokenParser is the canonical parser for bascule that deals with JWTs. // This parser does not use the source. type tokenParser struct { diff --git a/basculejwt/token_test.go b/basculejwt/token_test.go index 7acbca1..f4dac8e 100644 --- a/basculejwt/token_test.go +++ b/basculejwt/token_test.go @@ -130,6 +130,11 @@ func (suite *TokenTestSuite) TestTokenParser() { suite.Equal(suite.capabilities, caps) suite.True(ok) + suite.Require().Implements((*bascule.AttributesAccessor)(nil), token) + v, ok := bascule.GetAttribute[string](token.(bascule.AttributesAccessor), "version") + suite.True(ok) + suite.Equal(suite.version, v) + suite.Require().Implements((*Claims)(nil), token) claims := token.(Claims) suite.Equal(suite.audience, claims.Audience())