Skip to content

Commit ffa0a71

Browse files
committed
fix: deprecate bearer strategy
1 parent 4a114db commit ffa0a71

File tree

12 files changed

+42
-739
lines changed

12 files changed

+42
-739
lines changed

.golangci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,3 @@ issues:
4848
- errcheck
4949
- gosec
5050

51-
# FIXME temporarily suppress this.
52-
- path: "auth/strategies/(token|bearer)/"
53-
linters:
54-
- dupl

auth/strategies/bearer/bearer.go

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,71 @@
11
// Package bearer provides authentication strategy,
22
// to authenticate HTTP requests based on the bearer token.
3+
//
4+
// Deprecated: Use token Strategy instead.
35
package bearer
46

57
import (
68
"context"
7-
"errors"
8-
"fmt"
99
"net/http"
10-
"strings"
1110

1211
"github.com/shaj13/go-guardian/auth"
12+
"github.com/shaj13/go-guardian/auth/strategies/token"
13+
"github.com/shaj13/go-guardian/store"
1314
)
1415

1516
var (
1617
// ErrInvalidToken indicate a hit of an invalid bearer token format.
1718
// And it's returned by Token function.
18-
ErrInvalidToken = errors.New("bearer: Invalid bearer token")
19+
ErrInvalidToken = token.ErrInvalidToken
1920
// ErrTokenNotFound is returned by authenticating functions for bearer strategies,
2021
// when token not found in their store.
21-
ErrTokenNotFound = errors.New("barer: Token does not exists")
22+
ErrTokenNotFound = token.ErrTokenNotFound
2223
)
2324

24-
type authenticateFunc func(ctx context.Context, r *http.Request, token string) (auth.Info, error)
25+
const (
26+
// CachedStrategyKey export identifier for the cached bearer strategy,
27+
// commonly used when enable/add strategy to go-guardian authenticator.
28+
CachedStrategyKey = token.CachedStrategyKey
29+
// StatitcStrategyKey export identifier for the static bearer strategy,
30+
// commonly used when enable/add strategy to go-guardian authenticator.
31+
StatitcStrategyKey = token.StatitcStrategyKey
32+
)
2533

26-
func (auth authenticateFunc) authenticate(ctx context.Context, r *http.Request) (auth.Info, error) {
27-
token, err := Token(r)
28-
if err != nil {
29-
return nil, err
30-
}
31-
return auth(ctx, r, token)
32-
}
34+
// AuthenticateFunc declare custom function to authenticate request using token.
35+
// The authenticate function invoked by Authenticate Strategy method when
36+
// The token does not exist in the cahce and the invocation result will be cached, unless an error returned.
37+
// Use NoOpAuthenticate instead to refresh/mangae token directly using cache or Append function.
38+
type AuthenticateFunc = token.AuthenticateFunc
39+
40+
// Static implements auth.Strategy and define a synchronized map honor all predefined bearer tokens.
41+
type Static = token.Static
3342

3443
// Token return bearer token from Authorization header, or ErrInvalidToken,
3544
// The returned token will not contain "Bearer" keyword
3645
func Token(r *http.Request) (string, error) {
37-
header := r.Header.Get("Authorization")
38-
header = strings.TrimSpace(header)
39-
40-
if header == "" {
41-
return "", ErrInvalidToken
42-
}
46+
return token.AuthorizationParser("Bearer").Token(r)
47+
}
4348

44-
token := strings.Split(header, " ")
45-
if len(token) < 2 || strings.ToLower(token[0]) != "bearer" {
46-
return "", ErrInvalidToken
47-
}
49+
// NewStaticFromFile returns static auth.Strategy, populated from a CSV file.
50+
func NewStaticFromFile(path string) (auth.Strategy, error) {
51+
return token.NewStaticFromFile(path)
52+
}
4853

49-
if len(token[1]) == 0 {
50-
return "", ErrInvalidToken
51-
}
54+
// NewStatic returns static auth.Strategy, populated from a map.
55+
func NewStatic(tokens map[string]auth.Info) auth.Strategy {
56+
return token.NewStatic(tokens)
57+
}
5258

53-
return token[1], nil
59+
// New return new auth.Strategy.
60+
// The returned strategy, caches the invocation result of authenticate function, See AuthenticateFunc.
61+
// Use NoOpAuthenticate to refresh/mangae token directly using cache or Append function, See NoOpAuthenticate.
62+
func New(auth AuthenticateFunc, c store.Cache) auth.Strategy {
63+
return token.New(auth, c)
5464
}
5565

56-
func challenge(realm string) string {
57-
return fmt.Sprintf(`Bearer realm="%s", title="Bearer Token Based Authentication Scheme"`, realm)
66+
// NoOpAuthenticate implements Authenticate function, it return nil, auth.ErrNOOP,
67+
// commonly used when token refreshed/mangaed directly using cache or Append function,
68+
// and there is no need to parse token and authenticate request.
69+
func NoOpAuthenticate(ctx context.Context, r *http.Request, token string) (auth.Info, error) {
70+
return nil, auth.ErrNOOP
5871
}

auth/strategies/bearer/bearer_test.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

auth/strategies/bearer/cached.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)