forked from qvest-digital/loginsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_claims.go
39 lines (32 loc) · 829 Bytes
/
user_claims.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
package login
import (
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/kernle32dll/loginsrv/model"
"github.com/pkg/errors"
)
type customClaims map[string]interface{}
func (custom customClaims) Valid() error {
if exp, ok := custom["exp"]; ok {
if exp, ok := exp.(int64); ok {
if exp < time.Now().Unix() {
return errors.New("token expired")
}
}
}
return nil
}
func (custom customClaims) merge(values map[string]interface{}) {
for k, v := range values {
custom[k] = v
}
}
type UserClaims interface {
Claims(userInfo model.UserInfo) (jwt.Claims, error)
}
func NewUserClaims(config *Config) (UserClaims, error) {
if config.UserEndpoint != "" {
return newUserClaimsProvider(config.UserEndpoint, config.UserEndpointToken, config.UserEndpointTimeout)
}
return newUserClaimsFile(config.UserFile)
}