forked from shaj13/go-guardian
/
strategy.go
67 lines (55 loc) · 1.89 KB
/
strategy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package auth
import (
"context"
"errors"
"net/http"
)
// ErrInvalidStrategy is returned by Append/Revoke functions,
// when passed strategy does not implement Append/Revoke.
var ErrInvalidStrategy = errors.New("auth: Invalid strategy")
// Strategy represents an authentication mechanism or method to authenticate users requests.
type Strategy interface {
// Authenticate users requests and return user information or error.
Authenticate(ctx context.Context, r *http.Request) (Info, error)
}
// Option configures Strategy using the functional options paradigm popularized by Rob Pike and Dave Cheney.
// If you're unfamiliar with this style,
// see https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and
// https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.
type Option interface {
Apply(v interface{})
}
// OptionFunc implements Option interface.
type OptionFunc func(v interface{})
// Apply the configuration to the provided strategy.
func (fn OptionFunc) Apply(v interface{}) {
fn(v)
}
// Append new Info to a strategy store.
// if passed strategy does not implement Append type ErrInvalidStrategy returned,
// Otherwise, nil.
//
// WARNING: Append function does not guarantee safe concurrency, It's natively depends on strategy store.
func Append(s Strategy, key interface{}, info Info) error {
u, ok := s.(interface {
Append(interface{}, Info) error
})
if ok {
return u.Append(key, info)
}
return ErrInvalidStrategy
}
// Revoke delete Info from strategy store.
// if passed strategy does not implement Revoke type ErrInvalidStrategy returned,
// Otherwise, nil.
//
// WARNING: Revoke function does not guarantee safe concurrency, It's natively depends on strategy store.
func Revoke(s Strategy, key interface{}) error {
u, ok := s.(interface {
Revoke(key interface{}) error
})
if ok {
return u.Revoke(key)
}
return ErrInvalidStrategy
}