-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclaims.go
153 lines (136 loc) · 4.2 KB
/
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package multi
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/golang-jwt/jwt"
"github.com/snowlyg/helper/arr"
)
const (
ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
ValidationErrorUnverifiable // Token could not be verified because of signing problems
ValidationErrorSignatureInvalid // Signature validation failed
ValidationErrorExpired // EXP validation failed
ValidationErrorId
ValidationErrorUsername
ValidationErrorAuthorityId
ValidationErrorAuthorityType
ValidationErrorLoginType
ValidationErrorAuthType
)
// Custom struct
// Id use, id
// Username
// TenancyId
// TenancyName
// AuthorityId
// AuthorityType
// LoginType web,app,wechat
// AuthType
// CreationDate
// ExpiresIn
type MultiClaims struct {
Id string `json:"id,omitempty" redis:"id"`
Username string `json:"username,omitempty" redis:"username"`
TenancyId uint `json:"tenancyId,omitempty" redis:"tenancy_id"`
TenancyName string `json:"tenancyName,omitempty" redis:"tenancy_name"`
AuthorityId string `json:"authorityId,omitempty" redis:"authority_id"`
AuthorityType int `json:"authorityType,omitempty" redis:"authority_type"`
LoginType int `json:"loginType,omitempty" redis:"login_type"`
AuthType int `json:"authType,omitempty" redis:"auth_type"`
CreationDate int64 `json:"creationData,omitempty" redis:"creation_data"`
ExpiresAt int64 `json:"expiresAt,omitempty" redis:"expires_at"`
}
func New(m *Multi) *MultiClaims {
claims := &MultiClaims{
Id: strconv.FormatUint(uint64(m.Id), 10),
Username: m.Username,
TenancyId: m.TenancyId,
TenancyName: m.TenancyName,
AuthorityId: strings.Join(m.AuthorityIds, "-"),
AuthorityType: m.AuthorityType,
LoginType: m.LoginType,
AuthType: m.AuthType,
CreationDate: time.Now().Local().Unix(),
ExpiresAt: m.ExpiresAt,
}
return claims
}
func (c *MultiClaims) Valid() error {
vErr := new(jwt.ValidationError)
now := time.Now().Unix()
// The claims below are optional, by default, so if they are set to the
// default value in Go, let's not fail the verification for them.
if !c.VerifyExpiresAt(now, false) {
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
vErr.Inner = fmt.Errorf("token is expired by %v", delta)
vErr.Errors |= ValidationErrorExpired
}
if !c.VerifyId() {
vErr.Inner = errors.New("id is empty")
vErr.Errors |= ValidationErrorId
}
if !c.VerifyUsername() {
vErr.Inner = errors.New("username is empty")
vErr.Errors |= ValidationErrorUsername
}
if !c.VerifyAuthorityId() {
vErr.Inner = errors.New("authority id is empty")
vErr.Errors |= ValidationErrorAuthorityId
}
if !c.VerifyAuthorityType() {
vErr.Inner = errors.New("authority type is invalid")
vErr.Errors |= ValidationErrorAuthorityType
}
if !c.VerifyLoginType() {
vErr.Inner = errors.New("login type is invalid")
vErr.Errors |= ValidationErrorLoginType
}
if !c.VerifyAuthType() {
vErr.Inner = errors.New("auth type is invalid")
vErr.Errors |= ValidationErrorAuthType
}
if valid(vErr) {
return nil
}
return vErr
}
// No errors
func valid(e *jwt.ValidationError) bool {
return e.Errors == 0
}
// Compares the exp claim against cmp.
// If required is false, this method will return true if the value matches or is unset
func (c *MultiClaims) VerifyExpiresAt(cmp int64, req bool) bool {
return verifyExp(c.ExpiresAt, cmp, req)
}
func verifyExp(exp int64, now int64, required bool) bool {
if exp == 0 {
return !required
}
return now <= exp
}
func (c *MultiClaims) VerifyId() bool {
return c.Id != ""
}
func (c *MultiClaims) VerifyUsername() bool {
return c.Username != ""
}
func (c *MultiClaims) VerifyAuthorityId() bool {
return c.AuthorityId != ""
}
func (c *MultiClaims) VerifyAuthorityType() bool {
return c.AuthorityType > 0
}
func (c *MultiClaims) VerifyLoginType() bool {
loginType := arr.NewCheckArrayType(4)
loginType.AddMutil(LoginTypeWeb, LoginTypeApp, LoginTypeWx, LoginTypeDevice)
return loginType.Check(c.LoginType)
}
func (c *MultiClaims) VerifyAuthType() bool {
authType := arr.NewCheckArrayType(4)
authType.AddMutil(NoAuth, AuthPwd, AuthCode, AuthThirdParty)
return authType.Check(c.AuthType)
}