Skip to content

Commit dec064c

Browse files
committed
Migrate jwt-go library from v2 to v3
1 parent 79bfa11 commit dec064c

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

core/authentication/jwt_backend.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {
4040

4141
func (backend *JWTAuthenticationBackend) GenerateToken(userUUID string) (string, error) {
4242
token := jwt.New(jwt.SigningMethodRS512)
43-
token.Claims["exp"] = time.Now().Add(time.Hour * time.Duration(settings.Get().JWTExpirationDelta)).Unix()
44-
token.Claims["iat"] = time.Now().Unix()
45-
token.Claims["sub"] = userUUID
43+
token.Claims = jwt.MapClaims{
44+
"exp": time.Now().Add(time.Hour * time.Duration(settings.Get().JWTExpirationDelta)).Unix(),
45+
"iat": time.Now().Unix(),
46+
"sub": userUUID,
47+
}
4648
tokenString, err := token.SignedString(backend.privateKey)
4749
if err != nil {
4850
panic(err)
@@ -76,7 +78,7 @@ func (backend *JWTAuthenticationBackend) getTokenRemainingValidity(timestamp int
7678

7779
func (backend *JWTAuthenticationBackend) Logout(tokenString string, token *jwt.Token) error {
7880
redisConn := redis.Connect()
79-
return redisConn.SetValue(tokenString, tokenString, backend.getTokenRemainingValidity(token.Claims["exp"]))
81+
return redisConn.SetValue(tokenString, tokenString, backend.getTokenRemainingValidity(token.Claims.(jwt.MapClaims)["exp"]))
8082
}
8183

8284
func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {

core/authentication/middlewares.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package authentication
33
import (
44
"fmt"
55
jwt "github.com/dgrijalva/jwt-go"
6+
request "github.com/dgrijalva/jwt-go/request"
67
"net/http"
78
)
89

910
func RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
1011
authBackend := InitJWTAuthenticationBackend()
1112

12-
token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
13+
token, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
1314
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
1415
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
1516
} else {

services/auth_service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"api.jwt.auth/services/models"
77
"encoding/json"
88
jwt "github.com/dgrijalva/jwt-go"
9+
request "github.com/dgrijalva/jwt-go/request"
910
"net/http"
1011
)
1112

@@ -40,7 +41,7 @@ func RefreshToken(requestUser *models.User) []byte {
4041

4142
func Logout(req *http.Request) error {
4243
authBackend := authentication.InitJWTAuthenticationBackend()
43-
tokenRequest, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
44+
tokenRequest, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
4445
return authBackend.PublicKey, nil
4546
})
4647
if err != nil {

0 commit comments

Comments
 (0)