-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
58 lines (49 loc) · 1.54 KB
/
api.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
package openid
import (
"net/http"
"time"
"github.com/go-chassis/go-chassis/v2/security/token"
"github.com/go-chassis/openlog"
)
const (
// HeaderAuthorizationType = "Authorization-Type"
// AuthorizationTypeIDToken = "ID_TOKEN"
// AuthorizationTypeAccessToken = "ACCESS_TOKEN"
// TokenTypeBearer = "Bearer"
ContextHeaderAuthorizationType = "Authorization-TYPE"
AuthorizationTypeIDToken = "ID_TOKEN"
AuthorizationTypeAccessToken = "ACCESS_TOKEN"
TokenTypeBearer = "Bearer"
)
var auth *Auth
//Auth should implement auth logic
//it is singleton
type Auth struct {
SecretFunc token.SecretFunc //required
Expire time.Duration
Realm string //required
//optional. Authorize check whether this request could access some resource or API based on json claims.
//Typically, this method should communicate with a RBAC, ABAC system
Authorize func(payload map[string]interface{}, req *http.Request) error
//optional.
// this function control whether a request should be validate or not
// if this func is nil, validate all requests.
MustAuth func(req *http.Request) bool
}
//Use put a custom auth logic
//then register handler to chassis
func Use(middleware *Auth) {
auth = middleware
if auth.Expire == 0 {
openlog.Warn("token issued by service will not expire")
}
if auth.MustAuth == nil {
openlog.Info("auth all requests")
} else {
openlog.Warn("under some condition, no auth")
}
}
//SetExpire reset the expire time
func SetExpire(duration time.Duration) {
auth.Expire = duration
}