Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 2a05176

Browse files
authored
Merge pull request #21 from arieslee/master
update goframe version to v2
2 parents 001be56 + 4f6ff31 commit 2a05176

15 files changed

+46
-36
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# gf-jwt
22
GF jwt plugin
33

4-
This plugin is forked [https://github.com/appleboy/gin-jwt](https://github.com/appleboy/gin-jwt) plugin, modified to [https://github.com/gogf/gf](https://github.com/gogf/gf) plugin.
4+
This plugin is forked [https://github.com/gogf/gf-jwt](https://github.com/gogf/gf-jwt) plugin, modified to [https://github.com/gogf/gf/v2](https://github.com/gogf/gf) plugin.
55

66

77
[英文](README.md) [中文](README_zh.md)

README_zh.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# gf-jwt
22
gf 的 jwt 插件。
33

4-
这个插件是 fork 了 [https://github.com/appleboy/gin-jwt](https://github.com/appleboy/gin-jwt) 插件,修改为 [https://github.com/gogf/gf](https://github.com/gogf/gf) 插件.
4+
这个插件是 fork 了 [https://github.com/gogf/gf-jwt](https://github.com/gogf/gf-jwt) 插件,修改为 [https://github.com/gogf/gf/v2](https://github.com/gogf/gf/v2) 插件.
55

66

77
[英文](README.md) [中文](README_zh.md)

auth_error.go

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package jwt
33
import "errors"
44

55
var (
6+
// ErrMissingContext indicates Context is required
7+
ErrMissingContext = errors.New("context is required")
8+
69
// ErrMissingSecretKey indicates Secret key is required
710
ErrMissingSecretKey = errors.New("secret key is required")
811

auth_jwt.go

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package jwt
22

33
import (
4+
"context"
45
"crypto/rsa"
6+
"github.com/gogf/gf/v2/crypto/gmd5"
7+
"github.com/gogf/gf/v2/frame/g"
8+
"github.com/gogf/gf/v2/net/ghttp"
9+
"github.com/gogf/gf/v2/os/gcache"
510
"io/ioutil"
611
"net/http"
712
"strings"
813
"time"
9-
14+
1015
"github.com/dgrijalva/jwt-go"
11-
"github.com/gogf/gf/crypto/gmd5"
12-
"github.com/gogf/gf/frame/g"
13-
"github.com/gogf/gf/net/ghttp"
14-
"github.com/gogf/gf/os/gcache"
1516
)
1617

1718
// MapClaims type that uses the map[string]interface{} for JSON decoding
@@ -133,6 +134,8 @@ type GfJWTMiddleware struct {
133134

134135
// CacheAdapter
135136
CacheAdapter gcache.Adapter
137+
// context
138+
Ctx context.Context
136139
}
137140

138141
var (
@@ -154,7 +157,10 @@ func New(m *GfJWTMiddleware) (*GfJWTMiddleware, error) {
154157

155158
return m, nil
156159
}
157-
160+
func (mw *GfJWTMiddleware) SetCtx(ctx context.Context) *GfJWTMiddleware {
161+
mw.Ctx = ctx
162+
return mw
163+
}
158164
func (mw *GfJWTMiddleware) readKeys() error {
159165
err := mw.privateKey()
160166
if err != nil {
@@ -203,7 +209,9 @@ func (mw *GfJWTMiddleware) usingPublicKeyAlgo() bool {
203209

204210
// MiddlewareInit initialize jwt configs.
205211
func (mw *GfJWTMiddleware) MiddlewareInit() error {
206-
212+
if mw.Ctx == nil {
213+
return ErrMissingContext
214+
}
207215
if mw.TokenLookup == "" {
208216
mw.TokenLookup = "header:Authorization"
209217
}
@@ -373,7 +381,7 @@ func (mw *GfJWTMiddleware) GetClaimsFromJWT(r *ghttp.Request) (MapClaims, string
373381
}
374382

375383
if mw.SendAuthorization {
376-
token := r.GetString(TokenKey)
384+
token := r.Get(TokenKey).String()
377385
if len(token) > 0 {
378386
r.Header.Set("Authorization", mw.TokenHeadName+" "+token)
379387
}
@@ -594,7 +602,7 @@ func (mw *GfJWTMiddleware) jwtFromHeader(r *ghttp.Request, key string) (string,
594602
}
595603

596604
func (mw *GfJWTMiddleware) jwtFromQuery(r *ghttp.Request, key string) (string, error) {
597-
token := r.GetString(key)
605+
token := r.Get(key).String()
598606

599607
if token == "" {
600608
return "", ErrEmptyQueryToken
@@ -604,7 +612,7 @@ func (mw *GfJWTMiddleware) jwtFromQuery(r *ghttp.Request, key string) (string, e
604612
}
605613

606614
func (mw *GfJWTMiddleware) jwtFromCookie(r *ghttp.Request, key string) (string, error) {
607-
cookie := r.Cookie.Get(key)
615+
cookie := r.Cookie.Get(key).String()
608616

609617
if cookie == "" {
610618
return "", ErrEmptyCookieToken
@@ -614,7 +622,7 @@ func (mw *GfJWTMiddleware) jwtFromCookie(r *ghttp.Request, key string) (string,
614622
}
615623

616624
func (mw *GfJWTMiddleware) jwtFromParam(r *ghttp.Request, key string) (string, error) {
617-
token := r.GetString(key)
625+
token := r.Get(key).String()
618626
if token == "" {
619627
return "", ErrEmptyParamToken
620628
}
@@ -689,7 +697,7 @@ func (mw *GfJWTMiddleware) setBlacklist(token string, claims jwt.MapClaims) erro
689697
duration := time.Unix(exp, 0).Add(mw.MaxRefresh).Sub(mw.TimeFunc()).Truncate(time.Second)
690698

691699
// global gcache
692-
err = blacklist.Set(token, true, duration)
700+
err = blacklist.Set(mw.Ctx, token, true, duration)
693701

694702
if err != nil {
695703
return err
@@ -707,7 +715,7 @@ func (mw *GfJWTMiddleware) inBlacklist(token string) (bool, error) {
707715
}
708716

709717
// Global gcache
710-
if in, err := blacklist.Contains(tokenRaw); err != nil {
718+
if in, err := blacklist.Contains(mw.Ctx, tokenRaw); err != nil {
711719
return false, nil
712720
} else {
713721
return in, nil
@@ -716,16 +724,15 @@ func (mw *GfJWTMiddleware) inBlacklist(token string) (bool, error) {
716724

717725
// ExtractClaims help to extract the JWT claims
718726
func ExtractClaims(r *ghttp.Request) MapClaims {
719-
claims := r.GetParam(PayloadKey)
727+
claims := r.GetParam(PayloadKey).Interface()
720728
return claims.(MapClaims)
721729
}
722730

723731
// GetToken help to get the JWT token string
724732
func GetToken(r *ghttp.Request) string {
725-
token := r.GetString(TokenKey)
733+
token := r.Get(TokenKey).String()
726734
if len(token) == 0 {
727735
return ""
728736
}
729-
730737
return token
731738
}

example/api/auth.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
jwt "github.com/gogf/gf-jwt"
55
"github.com/gogf/gf-jwt/example/model"
66
"github.com/gogf/gf-jwt/example/service"
7-
"github.com/gogf/gf/frame/g"
8-
"github.com/gogf/gf/net/ghttp"
9-
"github.com/gogf/gf/os/glog"
10-
"github.com/gogf/gf/util/gconv"
7+
"github.com/gogf/gf/v2/frame/g"
8+
"github.com/gogf/gf/v2/net/ghttp"
9+
"github.com/gogf/gf/v2/os/gctx"
10+
"github.com/gogf/gf/v2/os/glog"
11+
"github.com/gogf/gf/v2/util/gconv"
1112
"net/http"
1213
"time"
1314
)
1415

1516
var (
16-
// The underlying JWT middleware.
17+
// Auth The underlying JWT middleware.
1718
Auth *jwt.GfJWTMiddleware
1819
)
1920

@@ -36,9 +37,10 @@ func init() {
3637
Unauthorized: Unauthorized,
3738
PayloadFunc: PayloadFunc,
3839
IdentityHandler: IdentityHandler,
40+
Ctx: gctx.New(),
3941
})
4042
if err != nil {
41-
glog.Fatal("JWT Error:" + err.Error())
43+
glog.Fatal(authMiddleware.Ctx, "JWT Error:"+err.Error())
4244
}
4345
Auth = authMiddleware
4446
}

example/api/work.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package api
22

33
import (
4-
"github.com/gogf/gf/frame/g"
5-
"github.com/gogf/gf/net/ghttp"
4+
"github.com/gogf/gf/v2/frame/g"
5+
"github.com/gogf/gf/v2/net/ghttp"
66
)
77

88
var Work = new(workApi)
99

1010
type workApi struct{}
1111

12-
// works is the default router handler for web server.
12+
// Works works is the default router handler for web server.
1313
func (a *workApi) Works(r *ghttp.Request) {
1414
data := g.Map{
1515
"message": "It works!",

example/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import (
44
"github.com/gogf/gf-jwt/example/api"
55
"github.com/gogf/gf-jwt/example/service"
6-
"github.com/gogf/gf/frame/g"
7-
"github.com/gogf/gf/net/ghttp"
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/gogf/gf/v2/net/ghttp"
88
)
99

1010
// authHook is the HOOK function implements JWT logistics.

example/service/middleware.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package service
22

3-
import (
4-
"github.com/gogf/gf/net/ghttp"
5-
)
3+
import "github.com/gogf/gf/v2/net/ghttp"
64

75
var Middleware = new(middlewareService)
86

9-
type middlewareService struct {}
7+
type middlewareService struct{}
108

119
func (s *middlewareService) CORS(r *ghttp.Request) {
1210
r.Response.CORSDefault()

example/service/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package service
22

33
import (
44
"github.com/gogf/gf-jwt/example/model"
5-
"github.com/gogf/gf/frame/g"
5+
"github.com/gogf/gf/v2/frame/g"
66
)
77

88
var User = new(userService)

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module github.com/gogf/gf-jwt
22

33
require (
44
github.com/dgrijalva/jwt-go v3.2.0+incompatible
5-
github.com/gogf/gf v1.15.3
5+
github.com/gogf/gf/v2 v2.0.0-rc2.0.20220217150450-7812f41b4395
66
github.com/mattn/go-runewidth v0.0.12 // indirect
77
)
88

9-
go 1.10
9+
go 1.15

screenshot/401.png

-255 KB
Loading

screenshot/hello.png

-342 KB
Loading

screenshot/login.png

-536 KB
Loading

screenshot/refresh_token.png

-690 KB
Loading

screenshot/server.png

-333 KB
Loading

0 commit comments

Comments
 (0)