|
1 | 1 | // Package bearer provides authentication strategy, |
2 | 2 | // to authenticate HTTP requests based on the bearer token. |
| 3 | +// |
| 4 | +// Deprecated: Use token Strategy instead. |
3 | 5 | package bearer |
4 | 6 |
|
5 | 7 | import ( |
6 | 8 | "context" |
7 | | - "errors" |
8 | | - "fmt" |
9 | 9 | "net/http" |
10 | | - "strings" |
11 | 10 |
|
12 | 11 | "github.com/shaj13/go-guardian/auth" |
| 12 | + "github.com/shaj13/go-guardian/auth/strategies/token" |
| 13 | + "github.com/shaj13/go-guardian/store" |
13 | 14 | ) |
14 | 15 |
|
15 | 16 | var ( |
16 | 17 | // ErrInvalidToken indicate a hit of an invalid bearer token format. |
17 | 18 | // And it's returned by Token function. |
18 | | - ErrInvalidToken = errors.New("bearer: Invalid bearer token") |
| 19 | + ErrInvalidToken = token.ErrInvalidToken |
19 | 20 | // ErrTokenNotFound is returned by authenticating functions for bearer strategies, |
20 | 21 | // when token not found in their store. |
21 | | - ErrTokenNotFound = errors.New("barer: Token does not exists") |
| 22 | + ErrTokenNotFound = token.ErrTokenNotFound |
22 | 23 | ) |
23 | 24 |
|
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 | +) |
25 | 33 |
|
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 |
33 | 42 |
|
34 | 43 | // Token return bearer token from Authorization header, or ErrInvalidToken, |
35 | 44 | // The returned token will not contain "Bearer" keyword |
36 | 45 | 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 | +} |
43 | 48 |
|
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 | +} |
48 | 53 |
|
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 | +} |
52 | 58 |
|
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) |
54 | 64 | } |
55 | 65 |
|
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 |
58 | 71 | } |
0 commit comments