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

Commit

Permalink
Merge pull request #21 from arieslee/master
Browse files Browse the repository at this point in the history
update goframe version to v2
  • Loading branch information
mingzaily authored Feb 20, 2022
2 parents 001be56 + 4f6ff31 commit 2a05176
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# gf-jwt
GF jwt plugin

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.
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.


[英文](README.md) [中文](README_zh.md)
Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# gf-jwt
gf 的 jwt 插件。

这个插件是 fork 了 [https://github.com/appleboy/gin-jwt](https://github.com/appleboy/gin-jwt) 插件,修改为 [https://github.com/gogf/gf](https://github.com/gogf/gf) 插件.
这个插件是 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) 插件.


[英文](README.md) [中文](README_zh.md)
Expand Down
3 changes: 3 additions & 0 deletions auth_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package jwt
import "errors"

var (
// ErrMissingContext indicates Context is required
ErrMissingContext = errors.New("context is required")

// ErrMissingSecretKey indicates Secret key is required
ErrMissingSecretKey = errors.New("secret key is required")

Expand Down
39 changes: 23 additions & 16 deletions auth_jwt.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package jwt

import (
"context"
"crypto/rsa"
"github.com/gogf/gf/v2/crypto/gmd5"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcache"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/gogf/gf/crypto/gmd5"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/gcache"
)

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

// CacheAdapter
CacheAdapter gcache.Adapter
// context
Ctx context.Context
}

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

return m, nil
}

func (mw *GfJWTMiddleware) SetCtx(ctx context.Context) *GfJWTMiddleware {
mw.Ctx = ctx
return mw
}
func (mw *GfJWTMiddleware) readKeys() error {
err := mw.privateKey()
if err != nil {
Expand Down Expand Up @@ -203,7 +209,9 @@ func (mw *GfJWTMiddleware) usingPublicKeyAlgo() bool {

// MiddlewareInit initialize jwt configs.
func (mw *GfJWTMiddleware) MiddlewareInit() error {

if mw.Ctx == nil {
return ErrMissingContext
}
if mw.TokenLookup == "" {
mw.TokenLookup = "header:Authorization"
}
Expand Down Expand Up @@ -373,7 +381,7 @@ func (mw *GfJWTMiddleware) GetClaimsFromJWT(r *ghttp.Request) (MapClaims, string
}

if mw.SendAuthorization {
token := r.GetString(TokenKey)
token := r.Get(TokenKey).String()
if len(token) > 0 {
r.Header.Set("Authorization", mw.TokenHeadName+" "+token)
}
Expand Down Expand Up @@ -594,7 +602,7 @@ func (mw *GfJWTMiddleware) jwtFromHeader(r *ghttp.Request, key string) (string,
}

func (mw *GfJWTMiddleware) jwtFromQuery(r *ghttp.Request, key string) (string, error) {
token := r.GetString(key)
token := r.Get(key).String()

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

func (mw *GfJWTMiddleware) jwtFromCookie(r *ghttp.Request, key string) (string, error) {
cookie := r.Cookie.Get(key)
cookie := r.Cookie.Get(key).String()

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

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

// global gcache
err = blacklist.Set(token, true, duration)
err = blacklist.Set(mw.Ctx, token, true, duration)

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

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

// ExtractClaims help to extract the JWT claims
func ExtractClaims(r *ghttp.Request) MapClaims {
claims := r.GetParam(PayloadKey)
claims := r.GetParam(PayloadKey).Interface()
return claims.(MapClaims)
}

// GetToken help to get the JWT token string
func GetToken(r *ghttp.Request) string {
token := r.GetString(TokenKey)
token := r.Get(TokenKey).String()
if len(token) == 0 {
return ""
}

return token
}
14 changes: 8 additions & 6 deletions example/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
jwt "github.com/gogf/gf-jwt"
"github.com/gogf/gf-jwt/example/model"
"github.com/gogf/gf-jwt/example/service"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv"
"net/http"
"time"
)

var (
// The underlying JWT middleware.
// Auth The underlying JWT middleware.
Auth *jwt.GfJWTMiddleware
)

Expand All @@ -36,9 +37,10 @@ func init() {
Unauthorized: Unauthorized,
PayloadFunc: PayloadFunc,
IdentityHandler: IdentityHandler,
Ctx: gctx.New(),
})
if err != nil {
glog.Fatal("JWT Error:" + err.Error())
glog.Fatal(authMiddleware.Ctx, "JWT Error:"+err.Error())
}
Auth = authMiddleware
}
Expand Down
6 changes: 3 additions & 3 deletions example/api/work.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package api

import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

var Work = new(workApi)

type workApi struct{}

// works is the default router handler for web server.
// Works works is the default router handler for web server.
func (a *workApi) Works(r *ghttp.Request) {
data := g.Map{
"message": "It works!",
Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"github.com/gogf/gf-jwt/example/api"
"github.com/gogf/gf-jwt/example/service"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

// authHook is the HOOK function implements JWT logistics.
Expand Down
6 changes: 2 additions & 4 deletions example/service/middleware.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package service

import (
"github.com/gogf/gf/net/ghttp"
)
import "github.com/gogf/gf/v2/net/ghttp"

var Middleware = new(middlewareService)

type middlewareService struct {}
type middlewareService struct{}

func (s *middlewareService) CORS(r *ghttp.Request) {
r.Response.CORSDefault()
Expand Down
2 changes: 1 addition & 1 deletion example/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package service

import (
"github.com/gogf/gf-jwt/example/model"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)

var User = new(userService)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module github.com/gogf/gf-jwt

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

go 1.10
go 1.15
Binary file modified screenshot/401.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot/hello.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot/refresh_token.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot/server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2a05176

Please sign in to comment.