41 lines
942 B
Go
41 lines
942 B
Go
package jwt
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type AccessTokenClaims struct {
|
|
UserId string `json:"id"`
|
|
FirstName string `json:"fn"`
|
|
LastName string `json:"ln"`
|
|
JWT
|
|
}
|
|
|
|
func TestJWT(t *testing.T) {
|
|
dir := "./data/"
|
|
|
|
accessClaims := AccessTokenClaims{
|
|
UserId: "123",
|
|
FirstName: "Igor",
|
|
LastName: "Sypachev",
|
|
}
|
|
accessClaims.
|
|
WithId("sadfswdf").
|
|
WithTtl(20 * time.Minute).
|
|
WithSessionId("wergwergw")
|
|
|
|
privateKey, _ := ReadPrivateKey(filepath.Join(filepath.Dir(dir), "private"))
|
|
token, err := Encode(accessClaims, privateKey)
|
|
assert.Equal(t, true, err == nil)
|
|
|
|
publicKey, _ := ReadPublicKey(filepath.Join(filepath.Dir(dir), "public"))
|
|
decodedClaims, _ := Decode(token, &AccessTokenClaims{}, publicKey)
|
|
f, _ := decodedClaims.(*AccessTokenClaims)
|
|
assert.Equal(t, f.UserId, "123")
|
|
assert.Equal(t, f.FirstName, "Igor")
|
|
assert.Equal(t, f.LastName, "Sypachev")
|
|
}
|