Split jwt into two files
This commit is contained in:
parent
d87716977a
commit
f72e7b8cec
@ -12,31 +12,26 @@ import (
|
|||||||
|
|
||||||
// TokenVerifier verifies Tokens.
|
// TokenVerifier verifies Tokens.
|
||||||
type TokenVerifier struct {
|
type TokenVerifier struct {
|
||||||
secret string
|
Expiration int
|
||||||
|
secret string
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrEmptySecret = fmt.Errorf("secret is required")
|
|
||||||
|
|
||||||
func NewTokenVerifier(secret string) (*TokenVerifier, error) {
|
func NewTokenVerifier(secret string) (*TokenVerifier, error) {
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
return nil, ErrEmptySecret
|
return nil, ErrEmptySecret
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TokenVerifier{
|
return &TokenVerifier{
|
||||||
secret: secret,
|
Expiration: 72,
|
||||||
|
secret: secret,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token contains everything to authenticate a user.
|
var (
|
||||||
type Token struct {
|
ErrUnexpectedSigningMethod = fmt.Errorf("unexpected signing method")
|
||||||
username string
|
ErrInvalidToken = fmt.Errorf("token is invalid")
|
||||||
name string
|
ErrTokenExpired = fmt.Errorf("token has expired")
|
||||||
expiry float64
|
ErrEmptySecret = fmt.Errorf("secret is required")
|
||||||
id uuid.UUID
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
expiration = 72
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateToken creates a new token from username and name.
|
// CreateToken creates a new token from username and name.
|
||||||
@ -48,7 +43,7 @@ func (tv *TokenVerifier) CreateToken(user *postgres.User) (string, error) {
|
|||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"usr": user.Email,
|
"usr": user.Email,
|
||||||
"name": user.Name,
|
"name": user.Name,
|
||||||
"exp": time.Now().Add(time.Hour * expiration).Unix(),
|
"exp": time.Now().Add(time.Hour * time.Duration(tv.Expiration)).Unix(),
|
||||||
"id": user.ID,
|
"id": user.ID,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -61,13 +56,7 @@ func (tv *TokenVerifier) CreateToken(user *postgres.User) (string, error) {
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// VerifyToken verifies a given string-token.
|
||||||
ErrUnexpectedSigningMethod = fmt.Errorf("unexpected signing method")
|
|
||||||
ErrInvalidToken = fmt.Errorf("token is invalid")
|
|
||||||
ErrTokenExpired = fmt.Errorf("token has expired")
|
|
||||||
)
|
|
||||||
|
|
||||||
// VerifyToken verifys a given string-token.
|
|
||||||
func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) { //nolint:ireturn
|
func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) { //nolint:ireturn
|
||||||
if tv.secret == "" {
|
if tv.secret == "" {
|
||||||
return nil, ErrEmptySecret
|
return nil, ErrEmptySecret
|
||||||
@ -96,36 +85,3 @@ func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error
|
|||||||
}
|
}
|
||||||
return tkn, nil
|
return tkn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyToken(token *jwt.Token) (jwt.MapClaims, error) {
|
|
||||||
if !token.Valid {
|
|
||||||
return nil, ErrInvalidToken
|
|
||||||
}
|
|
||||||
|
|
||||||
claims, ok := token.Claims.(jwt.MapClaims)
|
|
||||||
if !ok {
|
|
||||||
return nil, ErrInvalidToken
|
|
||||||
}
|
|
||||||
|
|
||||||
if !claims.VerifyExpiresAt(time.Now().Unix(), true) {
|
|
||||||
return nil, ErrTokenExpired
|
|
||||||
}
|
|
||||||
|
|
||||||
return claims, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Token) GetName() string {
|
|
||||||
return t.name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Token) GetUsername() string {
|
|
||||||
return t.username
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Token) GetExpiry() float64 {
|
|
||||||
return t.expiry
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Token) GetID() uuid.UUID {
|
|
||||||
return t.id
|
|
||||||
}
|
|
49
jwt/token.go
Normal file
49
jwt/token.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package jwt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Token contains everything to authenticate a user.
|
||||||
|
type Token struct {
|
||||||
|
username string
|
||||||
|
name string
|
||||||
|
expiry float64
|
||||||
|
id uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyToken(token *jwt.Token) (jwt.MapClaims, error) {
|
||||||
|
if !token.Valid {
|
||||||
|
return nil, ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
|
if !ok {
|
||||||
|
return nil, ErrInvalidToken
|
||||||
|
}
|
||||||
|
|
||||||
|
if !claims.VerifyExpiresAt(time.Now().Unix(), true) {
|
||||||
|
return nil, ErrTokenExpired
|
||||||
|
}
|
||||||
|
|
||||||
|
return claims, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) GetName() string {
|
||||||
|
return t.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) GetUsername() string {
|
||||||
|
return t.username
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) GetExpiry() float64 {
|
||||||
|
return t.expiry
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Token) GetID() uuid.UUID {
|
||||||
|
return t.id
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user