2025-01-28 00:44:02 +09:00
|
|
|
package hodu_test
|
|
|
|
|
2025-01-31 04:06:03 +09:00
|
|
|
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
|
2025-01-31 04:06:03 +09:00
|
|
|
|
|
|
|
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()) }
|
|
|
|
|
2025-01-31 04:06:03 +09:00
|
|
|
jc = JWTClaim{}
|
|
|
|
err = j.VerifyRS512(tok)
|
2025-01-28 00:44:02 +09:00
|
|
|
if err != nil { t.Fatalf("verification failure - %s", err.Error()) }
|
2025-01-31 04:06:03 +09:00
|
|
|
|
|
|
|
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
|
|
|
}
|