-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ba71de
commit 6af93d9
Showing
8 changed files
with
314 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package token | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
const minSecretKeySize = 32 | ||
|
||
type JWTMaker struct { | ||
secretKey string | ||
} | ||
|
||
func NewJWTMaker(secretKey string) (Maker, error) { | ||
if len(secretKey) < minSecretKeySize { | ||
return nil, fmt.Errorf("invalid key size: must be at least %d characters", minSecretKeySize) | ||
} | ||
return &JWTMaker{secretKey}, nil | ||
} | ||
|
||
func (maker *JWTMaker) CreateToken(username string, duration time.Duration) (string, error) { | ||
payload, err := NewPayload(username, duration) | ||
if err != nil { | ||
return "", err | ||
} | ||
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, payload) | ||
return jwtToken.SignedString([]byte(maker.secretKey)) | ||
} | ||
|
||
func (maker *JWTMaker) VerifyToken(token string) (*Payload, error) { | ||
keyFunc := func(token *jwt.Token) (interface{}, error) { | ||
_, ok := token.Method.(*jwt.SigningMethodHMAC) | ||
if !ok { | ||
return nil, ErrInvalidToken | ||
} | ||
return []byte(maker.secretKey), nil | ||
} | ||
|
||
jwtToken, err := jwt.ParseWithClaims(token, &Payload{}, keyFunc) | ||
if err != nil { | ||
verr, ok := err.(*jwt.ValidationError) | ||
if ok && errors.Is(verr.Inner, ErrExpiredToken) { | ||
return nil, ErrExpiredToken | ||
} | ||
return nil, ErrInvalidToken | ||
} | ||
|
||
payload, ok := jwtToken.Claims.(*Payload) | ||
if !ok { | ||
return nil, ErrInvalidToken | ||
} | ||
|
||
return payload, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package token | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ayushrakesh/go-bank/util" | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJWTMaker(t *testing.T) { | ||
maker, err := NewJWTMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
username := util.RandomOwner() | ||
duration := time.Minute | ||
|
||
issuedAt := time.Now() | ||
expiredAt := issuedAt.Add(duration) | ||
|
||
token, err := maker.CreateToken(username, duration) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
payload, err := maker.VerifyToken(token) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, payload) | ||
|
||
require.NotZero(t, payload.ID) | ||
require.Equal(t, username, payload.UserName) | ||
require.WithinDuration(t, issuedAt, payload.IssuedAt, time.Second) | ||
require.WithinDuration(t, expiredAt, payload.ExpiredAt, time.Second) | ||
} | ||
|
||
func TestExpiredJWTToken(t *testing.T) { | ||
maker, err := NewJWTMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
token, err := maker.CreateToken(util.RandomOwner(), -time.Minute) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
payload, err := maker.VerifyToken(token) | ||
require.Error(t, err) | ||
require.EqualError(t, err, ErrExpiredToken.Error()) | ||
require.Nil(t, payload) | ||
} | ||
|
||
func TestInvalidJWTTokenAlgNone(t *testing.T) { | ||
payload, err := NewPayload(util.RandomOwner(), time.Minute) | ||
require.NoError(t, err) | ||
|
||
jwtToken := jwt.NewWithClaims(jwt.SigningMethodNone, payload) | ||
token, err := jwtToken.SignedString(jwt.UnsafeAllowNoneSignatureType) | ||
require.NoError(t, err) | ||
|
||
maker, err := NewJWTMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
payload, err = maker.VerifyToken(token) | ||
require.Error(t, err) | ||
require.EqualError(t, err, ErrInvalidToken.Error()) | ||
require.Nil(t, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package token | ||
|
||
import "time" | ||
|
||
type Maker interface { | ||
CreateToken(string, time.Duration) (string, error) | ||
VerifyToken(token string) (*Payload, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package token | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ayushrakesh/go-bank/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPasetoMaker(t *testing.T) { | ||
maker, err := NewPasetoMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
username := util.RandomOwner() | ||
duration := time.Minute | ||
|
||
issuedAt := time.Now() | ||
expiredAt := issuedAt.Add(duration) | ||
|
||
token, err := maker.CreateToken(username, duration) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
payload, err := maker.VerifyToken(token) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, payload) | ||
|
||
require.NotZero(t, payload.ID) | ||
require.Equal(t, username, payload.UserName) | ||
require.WithinDuration(t, issuedAt, payload.IssuedAt, time.Second) | ||
require.WithinDuration(t, expiredAt, payload.ExpiredAt, time.Second) | ||
} | ||
|
||
func TestExpiredPasetoToken(t *testing.T) { | ||
maker, err := NewPasetoMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
token, err := maker.CreateToken(util.RandomOwner(), -time.Minute) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
payload, err := maker.VerifyToken(token) | ||
require.Error(t, err) | ||
require.EqualError(t, err, ErrExpiredToken.Error()) | ||
require.Nil(t, payload) | ||
} |
Oops, something went wrong.