2016-04-08 23:01:55 +03:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2021-09-24 22:32:29 +03:00
|
|
|
"crypto"
|
2016-04-08 23:01:55 +03:00
|
|
|
"crypto/rsa"
|
2022-05-28 02:11:16 +03:00
|
|
|
"os"
|
2021-05-29 04:26:41 +03:00
|
|
|
|
2022-08-27 12:36:37 +03:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2016-04-08 23:01:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadRSAPrivateKeyFromDisk(location string) *rsa.PrivateKey {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, e := os.ReadFile(location)
|
2016-04-08 23:01:55 +03:00
|
|
|
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 {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, e := os.ReadFile(location)
|
2016-04-08 23:01:55 +03:00
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
key, e := jwt.ParseRSAPublicKeyFromPEM(keyData)
|
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:32:29 +03:00
|
|
|
// 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)
|
2016-04-08 23:01:55 +03:00
|
|
|
s, e := token.SignedString(key)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2021-09-24 22:32:29 +03:00
|
|
|
|
|
|
|
func LoadECPrivateKeyFromDisk(location string) crypto.PrivateKey {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, e := os.ReadFile(location)
|
2021-09-24 22:32:29 +03:00
|
|
|
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 {
|
2022-05-28 02:11:16 +03:00
|
|
|
keyData, e := os.ReadFile(location)
|
2021-09-24 22:32:29 +03:00
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
key, e := jwt.ParseECPublicKeyFromPEM(keyData)
|
|
|
|
if e != nil {
|
|
|
|
panic(e.Error())
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|