-
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.
Merge pull request #24 from moslehazizi/ft/auth
Add paseto maker and test it
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 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
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,52 @@ | ||
package token | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aead/chacha20poly1305" | ||
"github.com/o1egl/paseto" | ||
) | ||
|
||
type PasetoMaker struct { | ||
paseto *paseto.V2 | ||
symmetricKey []byte | ||
} | ||
|
||
func NewPasetoMaker(symmetricKey string) (Maker, error) { | ||
if len(symmetricKey) != chacha20poly1305.KeySize { | ||
return nil, fmt.Errorf("invalid key size: must be exactly %d characters", chacha20poly1305.KeySize) | ||
} | ||
|
||
maker := &PasetoMaker{ | ||
paseto: paseto.NewV2(), | ||
symmetricKey: []byte(symmetricKey), | ||
} | ||
|
||
return maker, nil | ||
} | ||
|
||
func (maker *PasetoMaker) CreateToken(phonenumber string, duration time.Duration) (string, error) { | ||
payload, err := NewPayload(phonenumber, duration) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return maker.paseto.Encrypt(maker.symmetricKey, payload, nil) | ||
} | ||
|
||
func (maker *PasetoMaker) VerifyToken(token string) (*Payload, error) { | ||
payload := &Payload{} | ||
|
||
err := maker.paseto.Decrypt(token, maker.symmetricKey, payload, nil) | ||
if err != nil { | ||
return nil, ErrInvalidToken | ||
} | ||
|
||
err = payload.Valid() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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,48 @@ | ||
package token | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/moslehazizi/Elyasam_Restaurant/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPasetoMaker(t *testing.T) { | ||
maker, err := NewPasetoMaker(util.RandomString(32)) | ||
require.NoError(t, err) | ||
|
||
phonenumber := util.RandomName() | ||
duration := time.Minute | ||
|
||
issuedAt := time.Now() | ||
expiredAt := issuedAt.Add(duration) | ||
|
||
token, err := maker.CreateToken(phonenumber, duration) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
payload, err := maker.VerifyToken(token) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, token) | ||
|
||
require.NotZero(t, payload.ID) | ||
require.Equal(t, phonenumber, payload.Phonenumber) | ||
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.RandomName(), -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) | ||
} | ||
|