jwt/test/helpers.go

70 lines
1.3 KiB
Go
Raw Permalink Normal View History

package test
import (
"crypto"
"crypto/rsa"
"os"
2022-12-12 17:07:02 +03:00
"git.internal/re/jwt/v4"
)
func LoadRSAPrivateKeyFromDisk(location string) *rsa.PrivateKey {
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseRSAPrivateKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}
func LoadRSAPublicKeyFromDisk(location string) *rsa.PublicKey {
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseRSAPublicKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}
// MakeSampleToken creates and returns a encoded JWT token that has been signed with the specified cryptographic key.
func MakeSampleToken(c jwt.Claims, method jwt.SigningMethod, key interface{}) string {
token := jwt.NewWithClaims(method, c)
s, e := token.SignedString(key)
if e != nil {
panic(e.Error())
}
return s
}
func LoadECPrivateKeyFromDisk(location string) crypto.PrivateKey {
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseECPrivateKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}
func LoadECPublicKeyFromDisk(location string) crypto.PublicKey {
keyData, e := os.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseECPublicKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}