build: change jwt package to the new one
This commit is contained in:
parent
c3809c7cab
commit
c4dc8a2c21
7 changed files with 114 additions and 31 deletions
|
|
@ -2,6 +2,7 @@ package jwt
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/lestrrat-go/jwx/jwt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/jwtauth"
|
||||
|
|
@ -40,7 +41,7 @@ func Authenticator(next http.Handler) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
if err := jwt.Validate(token); err != nil {
|
||||
render.Render(w, r, ErrUnauthorized(ErrTokenExpired))
|
||||
return
|
||||
}
|
||||
|
|
@ -69,7 +70,8 @@ func AuthenticateRefreshJWT(next http.Handler) http.Handler {
|
|||
render.Render(w, r, ErrUnauthorized(ErrTokenUnauthorized))
|
||||
return
|
||||
}
|
||||
if !token.Valid {
|
||||
|
||||
if err := jwt.Validate(token); err != nil {
|
||||
render.Render(w, r, ErrUnauthorized(ErrTokenExpired))
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,20 +2,22 @@ package jwt
|
|||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
type CommonClaims struct {
|
||||
ExpiresAt int64 `json:"exp,omitempty"`
|
||||
IssuedAt int64 `json:"iat,omitempty"`
|
||||
}
|
||||
|
||||
// AppClaims represent the claims parsed from JWT access token.
|
||||
type AppClaims struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
Sub string `json:"sub,omitempty"`
|
||||
Roles []string `json:"roles,omitempty"`
|
||||
jwt.StandardClaims
|
||||
CommonClaims
|
||||
}
|
||||
|
||||
// ParseClaims parses JWT claims into AppClaims.
|
||||
func (c *AppClaims) ParseClaims(claims jwt.MapClaims) error {
|
||||
func (c *AppClaims) ParseClaims(claims map[string]interface{}) error {
|
||||
id, ok := claims["id"]
|
||||
if !ok {
|
||||
return errors.New("could not parse claim id")
|
||||
|
|
@ -48,11 +50,11 @@ func (c *AppClaims) ParseClaims(claims jwt.MapClaims) error {
|
|||
type RefreshClaims struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
jwt.StandardClaims
|
||||
CommonClaims
|
||||
}
|
||||
|
||||
// ParseClaims parses the JWT claims into RefreshClaims.
|
||||
func (c *RefreshClaims) ParseClaims(claims jwt.MapClaims) error {
|
||||
func (c *RefreshClaims) ParseClaims(claims map[string]interface{}) error {
|
||||
token, ok := claims["token"]
|
||||
if !ok {
|
||||
return errors.New("could not parse claim token")
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package jwt
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
|
@ -54,15 +55,38 @@ func (a *TokenAuth) GenTokenPair(accessClaims AppClaims, refreshClaims RefreshCl
|
|||
func (a *TokenAuth) CreateJWT(c AppClaims) (string, error) {
|
||||
c.IssuedAt = time.Now().Unix()
|
||||
c.ExpiresAt = time.Now().Add(a.JwtExpiry).Unix()
|
||||
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||
|
||||
claims, err := ParseStructToMap(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, tokenString, err := a.JwtAuth.Encode(claims)
|
||||
return tokenString, err
|
||||
}
|
||||
|
||||
func ParseStructToMap(c interface{}) (map[string]interface{}, error){
|
||||
var claims map[string]interface{}
|
||||
inrec, _ := json.Marshal(c)
|
||||
err := json.Unmarshal(inrec, &claims)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return claims, err
|
||||
}
|
||||
|
||||
// CreateRefreshJWT returns a refresh token for provided token Claims.
|
||||
func (a *TokenAuth) CreateRefreshJWT(c RefreshClaims) (string, error) {
|
||||
c.IssuedAt = time.Now().Unix()
|
||||
c.ExpiresAt = time.Now().Add(a.JwtRefreshExpiry).Unix()
|
||||
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||
|
||||
claims, err := ParseStructToMap(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, tokenString, err := a.JwtAuth.Encode(claims)
|
||||
return tokenString, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
jwt_go "github.com/dgrijalva/jwt-go"
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
|
|
@ -246,7 +245,7 @@ func TestAuthResource_refresh(t *testing.T) {
|
|||
// }
|
||||
refreshJWT := genRefreshJWT(jwt.RefreshClaims{
|
||||
Token: tc.token,
|
||||
StandardClaims: jwt_go.StandardClaims{
|
||||
CommonClaims: jwt.CommonClaims{
|
||||
ExpiresAt: time.Now().Add(time.Minute * tc.exp).UnixNano(),
|
||||
},
|
||||
})
|
||||
|
|
@ -312,7 +311,7 @@ func TestAuthResource_logout(t *testing.T) {
|
|||
t.Run(tc.name, func(t *testing.T) {
|
||||
refreshJWT := genRefreshJWT(jwt.RefreshClaims{
|
||||
Token: tc.token,
|
||||
StandardClaims: jwt_go.StandardClaims{
|
||||
CommonClaims: jwt.CommonClaims{
|
||||
ExpiresAt: time.Now().Add(time.Minute * tc.exp).UnixNano(),
|
||||
},
|
||||
})
|
||||
|
|
@ -360,11 +359,15 @@ func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io
|
|||
}
|
||||
|
||||
func genJWT(c jwt.AppClaims) string {
|
||||
_, tokenString, _ := auth.TokenAuth.JwtAuth.Encode(c)
|
||||
claims, _ := jwt.ParseStructToMap(c)
|
||||
|
||||
_, tokenString, _ := auth.TokenAuth.JwtAuth.Encode(claims)
|
||||
return tokenString
|
||||
}
|
||||
func genRefreshJWT(c jwt.RefreshClaims) string {
|
||||
_, tokenString, _ := auth.TokenAuth.JwtAuth.Encode(c)
|
||||
claims, _ := jwt.ParseStructToMap(c)
|
||||
|
||||
_, tokenString, _ := auth.TokenAuth.JwtAuth.Encode(claims)
|
||||
return tokenString
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue