Skip to content

Commit

Permalink
Migrate jwt-go library from v2 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
dghilardi committed Aug 22, 2016
1 parent 79bfa11 commit dec064c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 6 additions & 4 deletions core/authentication/jwt_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {

func (backend *JWTAuthenticationBackend) GenerateToken(userUUID string) (string, error) {
token := jwt.New(jwt.SigningMethodRS512)
token.Claims["exp"] = time.Now().Add(time.Hour * time.Duration(settings.Get().JWTExpirationDelta)).Unix()
token.Claims["iat"] = time.Now().Unix()
token.Claims["sub"] = userUUID
token.Claims = jwt.MapClaims{
"exp": time.Now().Add(time.Hour * time.Duration(settings.Get().JWTExpirationDelta)).Unix(),
"iat": time.Now().Unix(),
"sub": userUUID,
}
tokenString, err := token.SignedString(backend.privateKey)
if err != nil {
panic(err)
Expand Down Expand Up @@ -76,7 +78,7 @@ func (backend *JWTAuthenticationBackend) getTokenRemainingValidity(timestamp int

func (backend *JWTAuthenticationBackend) Logout(tokenString string, token *jwt.Token) error {
redisConn := redis.Connect()
return redisConn.SetValue(tokenString, tokenString, backend.getTokenRemainingValidity(token.Claims["exp"]))
return redisConn.SetValue(tokenString, tokenString, backend.getTokenRemainingValidity(token.Claims.(jwt.MapClaims)["exp"]))
}

func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
Expand Down
3 changes: 2 additions & 1 deletion core/authentication/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package authentication
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
request "github.com/dgrijalva/jwt-go/request"
"net/http"
)

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

token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
token, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
} else {
Expand Down
3 changes: 2 additions & 1 deletion services/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"api.jwt.auth/services/models"
"encoding/json"
jwt "github.com/dgrijalva/jwt-go"
request "github.com/dgrijalva/jwt-go/request"
"net/http"
)

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

func Logout(req *http.Request) error {
authBackend := authentication.InitJWTAuthenticationBackend()
tokenRequest, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
tokenRequest, err := request.ParseFromRequest(req, request.OAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
return authBackend.PublicKey, nil
})
if err != nil {
Expand Down

0 comments on commit dec064c

Please sign in to comment.