Skip to content

Commit

Permalink
refactored token parsing to remove credentials and simplify the API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Aug 1, 2024
1 parent 5a5aa08 commit 271ba61
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 475 deletions.
4 changes: 2 additions & 2 deletions authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type Authorizers[R any] []Authorizer[R]
// Append tacks on one or more authorizers to this collection. The possibly
// new Authorizers instance is returned. The semantics of this method are
// the same as the built-in append.
func (as Authorizers[R]) Append(a ...Authorizer[R]) Authorizers[R] {
return append(as, a...)
func (as Authorizers[R]) Append(more ...Authorizer[R]) Authorizers[R] {
return append(as, more...)
}

// Authorize requires all authorizers in this sequence to allow access. This
Expand Down
7 changes: 0 additions & 7 deletions basculehttp/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ func (err *MissingHeaderError) StatusCode() int {
return http.StatusUnauthorized
}

// fastIsSpace tests an ASCII byte to see if it's whitespace.
// HTTP headers are restricted to US-ASCII, so we don't need
// the full unicode stack.
func fastIsSpace(b byte) bool {
return b == ' ' || b == '\t' || b == '\n' || b == '\r' || b == '\v' || b == '\f'
}

// DefaultCredentialsParser is the default algorithm used to produce HTTP credentials
// from a source request.
type DefaultCredentialsParser struct {
Expand Down
25 changes: 0 additions & 25 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@ type Contexter interface {
Context() context.Context
}

type credentialsContextKey struct{}

// GetCredentials examines the context and returns the credentials used to
// build the Token. If no credentials are in the context, this function
// returns false.
func GetCredentials(ctx context.Context) (c Credentials, found bool) {
c, found = ctx.Value(credentialsContextKey{}).(Credentials)
return
}

// GetCredentialsFrom uses the context held by src to obtain credentials.
// As with GetCredentials, if no credentials are found this function returns false.
func GetCredentialsFrom(src Contexter) (Credentials, bool) {
return GetCredentials(src.Context())
}

// WithCredentials constructs a new context with the supplied credentials.
func WithCredentials(ctx context.Context, c Credentials) context.Context {
return context.WithValue(
ctx,
credentialsContextKey{},
c,
)
}

type tokenContextKey struct{}

// GetToken retrieves a Token from a context. If not token is in the context,
Expand Down
84 changes: 0 additions & 84 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,90 +14,6 @@ type ContextTestSuite struct {
TestSuite
}

func (suite *ContextTestSuite) testGetCredentialsSuccess() {
ctx := context.WithValue(
context.Background(),
credentialsContextKey{},
suite.testCredentials(),
)

creds, ok := GetCredentials(ctx)
suite.Require().True(ok)
suite.Equal(
suite.testCredentials(),
creds,
)
}

func (suite *ContextTestSuite) testGetCredentialsMissing() {
creds, ok := GetCredentials(context.Background())
suite.Equal(Credentials{}, creds)
suite.False(ok)
}

func (suite *ContextTestSuite) testGetCredentialsWrongType() {
ctx := context.WithValue(context.Background(), credentialsContextKey{}, 123)
creds, ok := GetCredentials(ctx)
suite.Equal(Credentials{}, creds)
suite.False(ok)
}

func (suite *ContextTestSuite) TestGetCredentials() {
suite.Run("Success", suite.testGetCredentialsSuccess)
suite.Run("Missing", suite.testGetCredentialsMissing)
suite.Run("WrongType", suite.testGetCredentialsWrongType)
}

func (suite *ContextTestSuite) testGetCredentialsFromSuccess() {
c := suite.contexter(
context.WithValue(
context.Background(),
credentialsContextKey{},
suite.testCredentials(),
),
)

creds, ok := GetCredentialsFrom(c)
suite.Require().True(ok)
suite.Equal(
suite.testCredentials(),
creds,
)
}

func (suite *ContextTestSuite) testGetCredentialsFromMissing() {
creds, ok := GetCredentialsFrom(
suite.contexter(context.Background()),
)

suite.Equal(Credentials{}, creds)
suite.False(ok)
}

func (suite *ContextTestSuite) testGetCredentialsFromWrongType() {
c := suite.contexter(
context.WithValue(context.Background(), credentialsContextKey{}, 123),
)

creds, ok := GetCredentialsFrom(c)
suite.Equal(Credentials{}, creds)
suite.False(ok)
}

func (suite *ContextTestSuite) TestGetCredentialsFrom() {
suite.Run("Success", suite.testGetCredentialsFromSuccess)
suite.Run("Missing", suite.testGetCredentialsFromMissing)
suite.Run("WrongType", suite.testGetCredentialsFromWrongType)
}

func (suite *ContextTestSuite) TestWithCredentials() {
ctx := WithCredentials(context.Background(), suite.testCredentials())

creds, ok := ctx.Value(credentialsContextKey{}).(Credentials)
suite.Require().True(ok)
suite.Equal(suite.testCredentials(), creds)
}

func (suite *ContextTestSuite) testGetTokenSuccess() {
ctx := context.WithValue(
context.Background(),
Expand Down
32 changes: 0 additions & 32 deletions credentials.go

This file was deleted.

43 changes: 0 additions & 43 deletions credentials_test.go

This file was deleted.

109 changes: 0 additions & 109 deletions error.go

This file was deleted.

Loading

0 comments on commit 271ba61

Please sign in to comment.