hodu/jwt_test.go

40 lines
991 B
Go
Raw Permalink Normal View History

2025-01-28 00:44:02 +09:00
package hodu_test
import "crypto/rand"
import "crypto/rsa"
2025-01-28 00:44:02 +09:00
import "hodu"
import "testing"
func TestJwt(t *testing.T) {
var tok string
var err error
type JWTClaim struct {
Abc string `json:"abc"`
Donkey string `json:"donkey"`
IssuedAt int `json:"iat"`
}
var jc JWTClaim
jc.Abc = "def"
jc.Donkey = "kong"
jc.IssuedAt = 111
var key *rsa.PrivateKey
key, err = rsa.GenerateKey(rand.Reader, 2048)
if err != nil { t.Fatalf("keygen failure - %s", err.Error()) }
var j *hodu.JWT[JWTClaim]
j = hodu.NewJWT(key, &jc)
tok, err = j.SignRS512()
2025-01-28 00:44:02 +09:00
if err != nil { t.Fatalf("signing failure - %s", err.Error()) }
jc = JWTClaim{}
err = j.VerifyRS512(tok)
2025-01-28 00:44:02 +09:00
if err != nil { t.Fatalf("verification failure - %s", err.Error()) }
if jc.Abc != "def" { t.Fatal("decoding failure of Abc field") }
if jc.Donkey != "kong" { t.Fatal("decoding failure of Donkey field") }
if jc.IssuedAt != 111 { t.Fatal("decoding failure of Issued field") }
2025-01-28 00:44:02 +09:00
}