fix jwt auth
This commit is contained in:
parent
9b27282ee3
commit
1b261b983c
5 changed files with 23 additions and 22 deletions
|
|
@ -3,7 +3,7 @@ package jwt
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/dgrijalva/jwt-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppClaims represent the claims parsed from JWT access token.
|
// AppClaims represent the claims parsed from JWT access token.
|
||||||
|
|
@ -14,20 +14,20 @@ type AppClaims struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseClaims parses JWT claims into AppClaims.
|
// ParseClaims parses JWT claims into AppClaims.
|
||||||
func (c *AppClaims) ParseClaims(claims jwtauth.Claims) error {
|
func (c *AppClaims) ParseClaims(claims jwt.MapClaims) error {
|
||||||
id, ok := claims.Get("id")
|
id, ok := claims["id"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("could not parse claim id")
|
return errors.New("could not parse claim id")
|
||||||
}
|
}
|
||||||
c.ID = int(id.(float64))
|
c.ID = int(id.(float64))
|
||||||
|
|
||||||
sub, ok := claims.Get("sub")
|
sub, ok := claims["sub"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("could not parse claim sub")
|
return errors.New("could not parse claim sub")
|
||||||
}
|
}
|
||||||
c.Sub = sub.(string)
|
c.Sub = sub.(string)
|
||||||
|
|
||||||
rl, ok := claims.Get("roles")
|
rl, ok := claims["roles"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("could not parse claims roles")
|
return errors.New("could not parse claims roles")
|
||||||
}
|
}
|
||||||
|
|
@ -49,8 +49,8 @@ type RefreshClaims struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseClaims parses the JWT claims into RefreshClaims.
|
// ParseClaims parses the JWT claims into RefreshClaims.
|
||||||
func (c *RefreshClaims) ParseClaims(claims jwtauth.Claims) error {
|
func (c *RefreshClaims) ParseClaims(claims jwt.MapClaims) error {
|
||||||
token, ok := claims.Get("token")
|
token, ok := claims["token"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("could not parse claim token")
|
return errors.New("could not parse claim token")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package jwt
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/go-pg/pg/orm"
|
"github.com/go-pg/pg/orm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -37,8 +37,8 @@ func (t *Token) BeforeUpdate(db orm.DB) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Claims returns the token claims to be signed
|
// Claims returns the token claims to be signed
|
||||||
func (t *Token) Claims() jwtauth.Claims {
|
func (t *Token) Claims() jwt.MapClaims {
|
||||||
return jwtauth.Claims{
|
return jwt.MapClaims{
|
||||||
"id": t.ID,
|
"id": t.ID,
|
||||||
"token": t.Token,
|
"token": t.Token,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -38,7 +39,7 @@ func (a *TokenAuth) Verifier() func(http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenTokenPair returns both an access token and a refresh token.
|
// GenTokenPair returns both an access token and a refresh token.
|
||||||
func (a *TokenAuth) GenTokenPair(ca jwtauth.Claims, cr jwtauth.Claims) (string, string, error) {
|
func (a *TokenAuth) GenTokenPair(ca jwt.MapClaims, cr jwt.MapClaims) (string, string, error) {
|
||||||
access, err := a.CreateJWT(ca)
|
access, err := a.CreateJWT(ca)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
|
|
@ -51,17 +52,17 @@ func (a *TokenAuth) GenTokenPair(ca jwtauth.Claims, cr jwtauth.Claims) (string,
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateJWT returns an access token for provided account claims.
|
// CreateJWT returns an access token for provided account claims.
|
||||||
func (a *TokenAuth) CreateJWT(c jwtauth.Claims) (string, error) {
|
func (a *TokenAuth) CreateJWT(c jwt.MapClaims) (string, error) {
|
||||||
c.SetIssuedNow()
|
jwtauth.SetIssuedNow(c)
|
||||||
c.SetExpiryIn(a.JwtExpiry)
|
jwtauth.SetExpiryIn(c, a.JwtExpiry)
|
||||||
_, tokenString, err := a.JwtAuth.Encode(c)
|
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||||
return tokenString, err
|
return tokenString, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateRefreshJWT returns a refresh token for provided token Claims.
|
// CreateRefreshJWT returns a refresh token for provided token Claims.
|
||||||
func (a *TokenAuth) CreateRefreshJWT(c jwtauth.Claims) (string, error) {
|
func (a *TokenAuth) CreateRefreshJWT(c jwt.MapClaims) (string, error) {
|
||||||
c.SetIssuedNow()
|
jwtauth.SetIssuedNow(c)
|
||||||
c.SetExpiryIn(a.JwtRefreshExpiry)
|
jwtauth.SetExpiryIn(c, a.JwtRefreshExpiry)
|
||||||
_, tokenString, err := a.JwtAuth.Encode(c)
|
_, tokenString, err := a.JwtAuth.Encode(c)
|
||||||
return tokenString, err
|
return tokenString, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dhax/go-base/auth/jwt"
|
"github.com/dhax/go-base/auth/jwt"
|
||||||
"github.com/go-chi/jwtauth"
|
jwtgo "github.com/dgrijalva/jwt-go"
|
||||||
validation "github.com/go-ozzo/ozzo-validation"
|
validation "github.com/go-ozzo/ozzo-validation"
|
||||||
"github.com/go-ozzo/ozzo-validation/is"
|
"github.com/go-ozzo/ozzo-validation/is"
|
||||||
"github.com/go-pg/pg/orm"
|
"github.com/go-pg/pg/orm"
|
||||||
|
|
@ -66,8 +66,8 @@ func (a *Account) CanLogin() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Claims returns the account's claims to be signed
|
// Claims returns the account's claims to be signed
|
||||||
func (a *Account) Claims() jwtauth.Claims {
|
func (a *Account) Claims() jwtgo.MapClaims {
|
||||||
return jwtauth.Claims{
|
return jwtgo.MapClaims{
|
||||||
"id": a.ID,
|
"id": a.ID,
|
||||||
"sub": a.Name,
|
"sub": a.Name,
|
||||||
"roles": a.Roles,
|
"roles": a.Roles,
|
||||||
|
|
|
||||||
|
|
@ -187,7 +187,7 @@ func (rs *Resource) token(w http.ResponseWriter, r *http.Request) {
|
||||||
browser, _ := ua.Browser()
|
browser, _ := ua.Browser()
|
||||||
|
|
||||||
token := &jwt.Token{
|
token := &jwt.Token{
|
||||||
Token: uuid.NewV4().String(),
|
Token: uuid.Must(uuid.NewV4()).String(),
|
||||||
Expiry: time.Now().Add(rs.TokenAuth.JwtRefreshExpiry),
|
Expiry: time.Now().Add(rs.TokenAuth.JwtRefreshExpiry),
|
||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
AccountID: acc.ID,
|
AccountID: acc.ID,
|
||||||
|
|
@ -247,7 +247,7 @@ func (rs *Resource) refresh(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token.Token = uuid.NewV4().String()
|
token.Token = uuid.Must(uuid.NewV4()).String()
|
||||||
token.Expiry = time.Now().Add(rs.TokenAuth.JwtRefreshExpiry)
|
token.Expiry = time.Now().Add(rs.TokenAuth.JwtRefreshExpiry)
|
||||||
token.UpdatedAt = time.Now()
|
token.UpdatedAt = time.Now()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue