Skip to content

Commit

Permalink
Add context on Authenticate func
Browse files Browse the repository at this point in the history
  • Loading branch information
instabledesign committed Dec 21, 2020
1 parent 00ac90b commit 0fee888
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

vendor
12 changes: 8 additions & 4 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package auth

import (
"context"
)

type Authenticator interface {
Authenticate(Credential) (Credential, error)
Authenticate(context.Context, Credential) (Credential, error)
}

type AuthenticatorFunc func(Credential) (Credential, error)
type AuthenticatorFunc func(context.Context, Credential) (Credential, error)

func (a AuthenticatorFunc) Authenticate(credential Credential) (Credential, error) {
return a(credential)
func (a AuthenticatorFunc) Authenticate(ctx context.Context, credential Credential) (Credential, error) {
return a(ctx, credential)
}
9 changes: 5 additions & 4 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ func WithSuccessMiddleware(middleware httpware.Middleware) AuthOption {

// NewAuthenticateFunc is an AuthenticateFunc that find, authenticate and hydrate credentials on the request context
func NewAuthenticateFunc(authenticator auth.Authenticator, options ...AuthFuncOption) AuthenticateFunc {
config := newAuthFuncConfig(options...)
config := NewAuthFuncConfig(options...)
return func(request *http.Request) (*http.Request, error) {
ctx := request.Context()
credential := config.credentialFinder(request)
if authenticator != nil {
creds, err := authenticator.Authenticate(credential)
creds, err := authenticator.Authenticate(ctx, credential)
if err != nil {
return request, err
}
credential = creds
}
return request.WithContext(auth.CredentialToContext(request.Context(), credential)), nil
return request.WithContext(auth.CredentialToContext(ctx, credential)), nil
}
}

Expand All @@ -101,7 +102,7 @@ func (o *AuthFuncConfig) apply(options ...AuthFuncOption) {
}
}

func newAuthFuncConfig(options ...AuthFuncOption) *AuthFuncConfig {
func NewAuthFuncConfig(options ...AuthFuncOption) *AuthFuncConfig {
opts := &AuthFuncConfig{
credentialFinder: DefaultCredentialFinder,
}
Expand Down
2 changes: 1 addition & 1 deletion middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestNewAuthenticateFunc_WithCredentialFinder(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, "http://fake-addr", nil)

authenticator := &mocks.Authenticator{}
authenticator.On("Authenticate", "my_credential_finder_value").Return("my_authenticate_credential", nil)
authenticator.On("Authenticate", context.TODO(), "my_credential_finder_value").Return("my_authenticate_credential", nil)

authenticateFunc := middleware.NewAuthenticateFunc(
authenticator,
Expand Down
23 changes: 13 additions & 10 deletions mocks/Authenticator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fee888

Please sign in to comment.