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

Commit da9a39c

Browse files
committed
update to Middleware
1 parent 4b65478 commit da9a39c

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

auth_jwt.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package jwt
22

33
import (
4-
"crypto/rsa"
5-
"errors"
6-
"github.com/gogf/gf/g"
7-
"github.com/gogf/gf/g/net/ghttp"
8-
"io/ioutil"
9-
"net/http"
10-
"strings"
11-
"time"
12-
4+
"crypto/rsa"
5+
"errors"
136
"github.com/dgrijalva/jwt-go"
7+
"github.com/gogf/gf/frame/g"
8+
"github.com/gogf/gf/net/ghttp"
9+
"io/ioutil"
10+
"net/http"
11+
"strings"
12+
"time"
1413
)
1514

1615
// MapClaims type that uses the map[string]interface{} for JSON decoding
@@ -392,7 +391,7 @@ func (mw *GfJWTMiddleware) GetClaimsFromJWT(r *ghttp.Request) (MapClaims, error)
392391
}
393392

394393
if mw.SendAuthorization {
395-
token := r.GetParam("JWT_TOKEN").String()
394+
token := r.GetString("JWT_TOKEN")
396395
if len(token) > 0 {
397396
r.Header.Set("Authorization", mw.TokenHeadName+" "+token)
398397
}
@@ -444,8 +443,8 @@ func (mw *GfJWTMiddleware) LoginHandler(r *ghttp.Request) {
444443

445444
// set cookie
446445
if mw.SendCookie {
447-
maxage := int(expire.Unix() - time.Now().Unix())
448-
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", maxage)
446+
maxage := int64(expire.Unix() - time.Now().Unix())
447+
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxage)*time.Millisecond)
449448
}
450449

451450
mw.LoginResponse(r, http.StatusOK, tokenString, expire)
@@ -501,8 +500,8 @@ func (mw *GfJWTMiddleware) RefreshToken(r *ghttp.Request) (string, time.Time, er
501500

502501
// set cookie
503502
if mw.SendCookie {
504-
maxage := int(expire.Unix() - time.Now().Unix())
505-
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", maxage)
503+
maxage := int64(expire.Unix() - time.Now().Unix())
504+
r.Cookie.SetCookie(mw.CookieName, tokenString, mw.CookieDomain, "/", time.Duration(maxage)*time.Millisecond)
506505
}
507506

508507
return tokenString, expire, nil
@@ -656,13 +655,13 @@ func (mw *GfJWTMiddleware) unauthorized(r *ghttp.Request, code int, message stri
656655

657656
// ExtractClaims help to extract the JWT claims
658657
func ExtractClaims(r *ghttp.Request) MapClaims {
659-
claims := r.GetParam("JWT_PAYLOAD").Val()
658+
claims := r.GetParam("JWT_PAYLOAD")
660659
return claims.(MapClaims)
661660
}
662661

663662
// GetToken help to get the JWT token string
664663
func GetToken(r *ghttp.Request) string {
665-
token := r.GetParam("JWT_TOKEN").String()
664+
token := r.GetString("JWT_TOKEN")
666665
if len(token) == 0 {
667666
return ""
668667
}

example/auth/auth.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package auth
22

33
import (
4-
"github.com/gogf/gf-jwt"
5-
"github.com/gogf/gf/g"
6-
"github.com/gogf/gf/g/net/ghttp"
7-
"github.com/gogf/gf/g/os/glog"
8-
"github.com/gogf/gf/g/util/gvalid"
4+
jwt "github.com/gogf/gf-jwt"
5+
"github.com/gogf/gf/frame/g"
6+
"github.com/gogf/gf/net/ghttp"
7+
"github.com/gogf/gf/os/glog"
8+
"github.com/gogf/gf/util/gvalid"
99
"net/http"
1010
"time"
1111
)

example/server/server.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
44
"github.com/gogf/gf-jwt/example/auth"
5-
"github.com/gogf/gf/g"
6-
"github.com/gogf/gf/g/net/ghttp"
5+
"github.com/gogf/gf/frame/g"
6+
"github.com/gogf/gf/net/ghttp"
7+
"time"
78
)
89

910
// hello should be authenticated to view.
@@ -17,21 +18,25 @@ func works(r *ghttp.Request) {
1718
}
1819

1920
// authHook is the HOOK function implements JWT logistics.
20-
func authHook(r *ghttp.Request) {
21-
r.Response.CORSDefault()
21+
func MiddlewareAuth(r *ghttp.Request) {
2222
auth.GfJWTMiddleware.MiddlewareFunc()(r)
23+
r.Middleware.Next()
24+
}
25+
26+
func MiddlewareCORS(r *ghttp.Request) {
27+
r.Response.CORSDefault()
28+
r.Middleware.Next()
2329
}
2430

2531
func main() {
32+
println(time.Now().Unix())
2633
s := g.Server()
27-
s.Group().Bind("/", []ghttp.GroupItem{
28-
{"ALL", "/", works},
29-
{"POST", "/login", auth.GfJWTMiddleware.LoginHandler},
30-
})
31-
s.Group("/user").Bind("/user", []ghttp.GroupItem{
32-
{"ALL", "*", authHook, ghttp.HOOK_BEFORE_SERVE},
33-
{"GET", "/refresh_token", auth.GfJWTMiddleware.RefreshHandler},
34-
{"GET", "/hello", hello},
34+
s.BindHandler("/", works)
35+
s.BindHandler("POST:/login", auth.GfJWTMiddleware.LoginHandler)
36+
s.Group("/user", func(g *ghttp.RouterGroup) {
37+
g.Middleware(MiddlewareCORS, MiddlewareAuth)
38+
g.ALL("/refresh_token", auth.GfJWTMiddleware.RefreshHandler)
39+
g.ALL("/hello", hello)
3540
})
3641
s.SetPort(8000)
3742
s.Run()

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ 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.6.2
5+
github.com/gogf/gf v1.9.7
66
)
7+
8+
go 1.13

0 commit comments

Comments
 (0)