forked from zhufuyi/sponge
/
auth.go
140 lines (116 loc) · 3.34 KB
/
auth.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
// Package middleware is gin middleware plugin.
package middleware
import (
"github.com/hankyu66/sponge/pkg/errcode"
"github.com/hankyu66/sponge/pkg/gin/response"
"github.com/hankyu66/sponge/pkg/jwt"
"github.com/hankyu66/sponge/pkg/logger"
"github.com/gin-gonic/gin"
)
const (
// HeaderAuthorizationKey http header authorization key
HeaderAuthorizationKey = "Authorization"
)
type jwtOptions struct {
isSwitchHTTPCode bool
verify VerifyFn // verify function, only use in Auth
}
// JwtOption set the jwt options.
type JwtOption func(*jwtOptions)
func (o *jwtOptions) apply(opts ...JwtOption) {
for _, opt := range opts {
opt(o)
}
}
func defaultJwtOptions() *jwtOptions {
return &jwtOptions{
isSwitchHTTPCode: false,
verify: nil,
}
}
// WithSwitchHTTPCode switch to http code
func WithSwitchHTTPCode() JwtOption {
return func(o *jwtOptions) {
o.isSwitchHTTPCode = true
}
}
// WithVerify set verify function
func WithVerify(verify VerifyFn) JwtOption {
return func(o *jwtOptions) {
o.verify = verify
}
}
func responseUnauthorized(c *gin.Context, isSwitchHTTPCode bool) {
if isSwitchHTTPCode {
response.Out(c, errcode.Unauthorized)
} else {
response.Error(c, errcode.Unauthorized)
}
}
// -------------------------------------------------------------------------------------------
// VerifyFn verify function
type VerifyFn func(claims *jwt.Claims) error
// Auth authorization
func Auth(opts ...JwtOption) gin.HandlerFunc {
o := defaultJwtOptions()
o.apply(opts...)
return func(c *gin.Context) {
authorization := c.GetHeader(HeaderAuthorizationKey)
if len(authorization) < 150 {
logger.Warn("authorization is illegal")
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
claims, err := jwt.ParseToken(authorization[7:]) // token=authorization[7:], remove Bearer prefix
if err != nil {
logger.Warn("ParseToken error", logger.Err(err))
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
if o.verify != nil {
if err = o.verify(claims); err != nil {
logger.Warn("verify error", logger.Err(err), logger.String("uid", claims.UID), logger.String("role", claims.Role))
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
} else {
c.Set("uid", claims.UID)
c.Set("role", claims.Role)
}
c.Next()
}
}
// -------------------------------------------------------------------------------------------
// VerifyCustomFn verify custom function
type VerifyCustomFn func(claims *jwt.CustomClaims) error
// AuthCustom custom authentication
func AuthCustom(verify VerifyCustomFn, opts ...JwtOption) gin.HandlerFunc {
o := defaultJwtOptions()
o.apply(opts...)
return func(c *gin.Context) {
authorization := c.GetHeader(HeaderAuthorizationKey)
if len(authorization) < 150 {
logger.Warn("authorization is illegal")
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
claims, err := jwt.ParseCustomToken(authorization[7:]) // token=authorization[7:], remove Bearer prefix
if err != nil {
logger.Warn("ParseToken error", logger.Err(err))
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
if err = verify(claims); err != nil {
logger.Warn("verify error", logger.Err(err), logger.Any("fields", claims.Fields))
responseUnauthorized(c, o.isSwitchHTTPCode)
c.Abort()
return
}
c.Next()
}
}