...

Source file src/github.com/golang-jwt/jwt/v5/test/helpers.go

Documentation: github.com/golang-jwt/jwt/v5/test

     1  package test
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/rsa"
     6  	"os"
     7  
     8  	"github.com/golang-jwt/jwt/v5"
     9  )
    10  
    11  func LoadRSAPrivateKeyFromDisk(location string) *rsa.PrivateKey {
    12  	keyData, e := os.ReadFile(location)
    13  	if e != nil {
    14  		panic(e.Error())
    15  	}
    16  	key, e := jwt.ParseRSAPrivateKeyFromPEM(keyData)
    17  	if e != nil {
    18  		panic(e.Error())
    19  	}
    20  	return key
    21  }
    22  
    23  func LoadRSAPublicKeyFromDisk(location string) *rsa.PublicKey {
    24  	keyData, e := os.ReadFile(location)
    25  	if e != nil {
    26  		panic(e.Error())
    27  	}
    28  	key, e := jwt.ParseRSAPublicKeyFromPEM(keyData)
    29  	if e != nil {
    30  		panic(e.Error())
    31  	}
    32  	return key
    33  }
    34  
    35  // MakeSampleToken creates and returns a encoded JWT token that has been signed with the specified cryptographic key.
    36  func MakeSampleToken(c jwt.Claims, method jwt.SigningMethod, key interface{}) string {
    37  	token := jwt.NewWithClaims(method, c)
    38  	s, e := token.SignedString(key)
    39  
    40  	if e != nil {
    41  		panic(e.Error())
    42  	}
    43  
    44  	return s
    45  }
    46  
    47  func LoadECPrivateKeyFromDisk(location string) crypto.PrivateKey {
    48  	keyData, e := os.ReadFile(location)
    49  	if e != nil {
    50  		panic(e.Error())
    51  	}
    52  	key, e := jwt.ParseECPrivateKeyFromPEM(keyData)
    53  	if e != nil {
    54  		panic(e.Error())
    55  	}
    56  	return key
    57  }
    58  
    59  func LoadECPublicKeyFromDisk(location string) crypto.PublicKey {
    60  	keyData, e := os.ReadFile(location)
    61  	if e != nil {
    62  		panic(e.Error())
    63  	}
    64  	key, e := jwt.ParseECPublicKeyFromPEM(keyData)
    65  	if e != nil {
    66  		panic(e.Error())
    67  	}
    68  	return key
    69  }
    70  

View as plain text