@@ -15,57 +15,125 @@ const (
1515 HeaderAuthorizationKey = "Authorization"
1616)
1717
18+ type jwtOptions struct {
19+ isSwitchHTTPCode bool
20+ verify VerifyFn // verify function, only use in Auth
21+ }
22+
23+ // JwtOption set the jwt options.
24+ type JwtOption func (* jwtOptions )
25+
26+ func (o * jwtOptions ) apply (opts ... JwtOption ) {
27+ for _ , opt := range opts {
28+ opt (o )
29+ }
30+ }
31+
32+ func defaultJwtOptions () * jwtOptions {
33+ return & jwtOptions {
34+ isSwitchHTTPCode : false ,
35+ verify : nil ,
36+ }
37+ }
38+
39+ // WithSwitchHTTPCode switch to http code
40+ func WithSwitchHTTPCode () JwtOption {
41+ return func (o * jwtOptions ) {
42+ o .isSwitchHTTPCode = true
43+ }
44+ }
45+
46+ // WithVerify set verify function
47+ func WithVerify (verify VerifyFn ) JwtOption {
48+ return func (o * jwtOptions ) {
49+ o .verify = verify
50+ }
51+ }
52+
53+ func responseUnauthorized (c * gin.Context , isSwitchHTTPCode bool ) {
54+ if isSwitchHTTPCode {
55+ response .Out (c , errcode .Unauthorized )
56+ } else {
57+ response .Error (c , errcode .Unauthorized )
58+ }
59+ }
60+
61+ // -------------------------------------------------------------------------------------------
62+
63+ // VerifyFn verify function
64+ type VerifyFn func (claims * jwt.Claims ) error
65+
1866// Auth authorization
19- func Auth () gin.HandlerFunc {
67+ func Auth (opts ... JwtOption ) gin.HandlerFunc {
68+ o := defaultJwtOptions ()
69+ o .apply (opts ... )
70+
2071 return func (c * gin.Context ) {
2172 authorization := c .GetHeader (HeaderAuthorizationKey )
22- if len (authorization ) < 20 {
23- logger .Warn ("authorization is illegal" , logger . String ( HeaderAuthorizationKey , authorization ) )
24- response . Error (c , errcode . Unauthorized )
73+ if len (authorization ) < 150 {
74+ logger .Warn ("authorization is illegal" )
75+ responseUnauthorized (c , o . isSwitchHTTPCode )
2576 c .Abort ()
2677 return
2778 }
28- token := authorization [ 7 :] // remove Bearer prefix
29- claims , err := jwt .VerifyToken ( token )
79+
80+ claims , err := jwt .ParseToken ( authorization [ 7 :]) // token=authorization[7:], remove Bearer prefix
3081 if err != nil {
31- logger .Warn ("VerifyToken error" , logger .Err (err ))
32- response . Error (c , errcode . Unauthorized )
82+ logger .Warn ("ParseToken error" , logger .Err (err ))
83+ responseUnauthorized (c , o . isSwitchHTTPCode )
3384 c .Abort ()
3485 return
3586 }
36- c .Set ("uid" , claims .UID )
87+
88+ if o .verify != nil {
89+ if err = o .verify (claims ); err != nil {
90+ logger .Warn ("verify error" , logger .Err (err ), logger .String ("uid" , claims .UID ), logger .String ("role" , claims .Role ))
91+ responseUnauthorized (c , o .isSwitchHTTPCode )
92+ c .Abort ()
93+ return
94+ }
95+ } else {
96+ c .Set ("uid" , claims .UID )
97+ c .Set ("role" , claims .Role )
98+ }
3799
38100 c .Next ()
39101 }
40102}
41103
42- // AuthAdmin admin authentication
43- func AuthAdmin () gin.HandlerFunc {
104+ // -------------------------------------------------------------------------------------------
105+
106+ // VerifyCustomFn verify custom function
107+ type VerifyCustomFn func (claims * jwt.CustomClaims ) error
108+
109+ // AuthCustom custom authentication
110+ func AuthCustom (verify VerifyCustomFn , opts ... JwtOption ) gin.HandlerFunc {
111+ o := defaultJwtOptions ()
112+ o .apply (opts ... )
113+
44114 return func (c * gin.Context ) {
45115 authorization := c .GetHeader (HeaderAuthorizationKey )
46- if len (authorization ) < 20 {
47- logger .Warn ("authorization is illegal" , logger . String ( HeaderAuthorizationKey , authorization ) )
48- response . Error (c , errcode . Unauthorized )
116+ if len (authorization ) < 150 {
117+ logger .Warn ("authorization is illegal" )
118+ responseUnauthorized (c , o . isSwitchHTTPCode )
49119 c .Abort ()
50120 return
51121 }
52- token := authorization [ 7 :] // remove Bearer prefix
53- claims , err := jwt .VerifyToken ( token )
122+
123+ claims , err := jwt .ParseCustomToken ( authorization [ 7 :]) // token=authorization[7:], remove Bearer prefix
54124 if err != nil {
55- logger .Warn ("VerifyToken error" , logger .Err (err ))
56- response . Error (c , errcode . Unauthorized )
125+ logger .Warn ("ParseToken error" , logger .Err (err ))
126+ responseUnauthorized (c , o . isSwitchHTTPCode )
57127 c .Abort ()
58128 return
59129 }
60130
61- // determine if it is an administrator
62- if claims .Role != "admin" {
63- logger .Warn ("prohibition of access" , logger .String ("uid" , claims .UID ), logger .String ("role" , claims .Role ))
64- response .Error (c , errcode .Forbidden )
131+ if err = verify (claims ); err != nil {
132+ logger .Warn ("verify error" , logger .Err (err ), logger .Any ("fields" , claims .Fields ))
133+ responseUnauthorized (c , o .isSwitchHTTPCode )
65134 c .Abort ()
66135 return
67136 }
68- c .Set ("uid" , claims .UID )
69137
70138 c .Next ()
71139 }
0 commit comments