Split jwt into two files
This commit is contained in:
parent
d4287f8aac
commit
7fdd8bd935
@ -12,31 +12,26 @@ import (
|
||||
|
||||
// TokenVerifier verifies Tokens.
|
||||
type TokenVerifier struct {
|
||||
Expiration int
|
||||
secret string
|
||||
}
|
||||
|
||||
var ErrEmptySecret = fmt.Errorf("secret is required")
|
||||
|
||||
func NewTokenVerifier(secret string) (*TokenVerifier, error) {
|
||||
if secret == "" {
|
||||
return nil, ErrEmptySecret
|
||||
}
|
||||
|
||||
return &TokenVerifier{
|
||||
Expiration: 72,
|
||||
secret: secret,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Token contains everything to authenticate a user.
|
||||
type Token struct {
|
||||
username string
|
||||
name string
|
||||
expiry float64
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
const (
|
||||
expiration = 72
|
||||
var (
|
||||
ErrUnexpectedSigningMethod = fmt.Errorf("unexpected signing method")
|
||||
ErrInvalidToken = fmt.Errorf("token is invalid")
|
||||
ErrTokenExpired = fmt.Errorf("token has expired")
|
||||
ErrEmptySecret = fmt.Errorf("secret is required")
|
||||
)
|
||||
|
||||
// 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{
|
||||
"usr": user.Email,
|
||||
"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,
|
||||
})
|
||||
|
||||
@ -61,13 +56,7 @@ func (tv *TokenVerifier) CreateToken(user *postgres.User) (string, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
var (
|
||||
ErrUnexpectedSigningMethod = fmt.Errorf("unexpected signing method")
|
||||
ErrInvalidToken = fmt.Errorf("token is invalid")
|
||||
ErrTokenExpired = fmt.Errorf("token has expired")
|
||||
)
|
||||
|
||||
// VerifyToken verifys a given string-token.
|
||||
// VerifyToken verifies a given string-token.
|
||||
func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) { //nolint:ireturn
|
||||
if tv.secret == "" {
|
||||
return nil, ErrEmptySecret
|
||||
@ -96,36 +85,3 @@ func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error
|
||||
}
|
||||
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