Allow access to public and private keys

This commit is contained in:
Melon 2023-10-25 17:37:55 +01:00
parent 8769decef2
commit d29f481a3c
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 9 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package mjwt package mjwt
import ( import (
"crypto/rsa"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"time" "time"
) )
@ -12,9 +13,11 @@ type Signer interface {
GenerateJwt(sub, id string, aud jwt.ClaimStrings, dur time.Duration, claims Claims) (string, error) GenerateJwt(sub, id string, aud jwt.ClaimStrings, dur time.Duration, claims Claims) (string, error)
SignJwt(claims jwt.Claims) (string, error) SignJwt(claims jwt.Claims) (string, error)
Issuer() string Issuer() string
PrivateKey() *rsa.PrivateKey
} }
// Verifier is used to verify the validity MJWT tokens and extract the claim values. // Verifier is used to verify the validity MJWT tokens and extract the claim values.
type Verifier interface { type Verifier interface {
VerifyJwt(token string, claims baseTypeClaim) (*jwt.Token, error) VerifyJwt(token string, claims baseTypeClaim) (*jwt.Token, error)
PublicKey() *rsa.PublicKey
} }

View File

@ -18,6 +18,7 @@ type defaultMJwtSigner struct {
} }
var _ Signer = &defaultMJwtSigner{} var _ Signer = &defaultMJwtSigner{}
var _ Verifier = &defaultMJwtSigner{}
// NewMJwtSigner creates a new defaultMJwtSigner using the issuer name and rsa.PrivateKey // NewMJwtSigner creates a new defaultMJwtSigner using the issuer name and rsa.PrivateKey
func NewMJwtSigner(issuer string, key *rsa.PrivateKey) Signer { func NewMJwtSigner(issuer string, key *rsa.PrivateKey) Signer {
@ -73,6 +74,9 @@ func (d *defaultMJwtSigner) VerifyJwt(token string, claims baseTypeClaim) (*jwt.
return d.verify.VerifyJwt(token, claims) return d.verify.VerifyJwt(token, claims)
} }
func (d *defaultMJwtSigner) PrivateKey() *rsa.PrivateKey { return d.key }
func (d *defaultMJwtSigner) PublicKey() *rsa.PublicKey { return d.verify.pub }
// readOrCreatePrivateKey returns the private key it the file already exists, // readOrCreatePrivateKey returns the private key it the file already exists,
// generates a new private key and saves it to the file, or returns an error if // generates a new private key and saves it to the file, or returns an error if
// reading or generating failed. // reading or generating failed.

View File

@ -57,3 +57,5 @@ func (d *defaultMJwtVerifier) VerifyJwt(token string, claims baseTypeClaim) (*jw
} }
return withClaims, claims.Valid() return withClaims, claims.Valid()
} }
func (d *defaultMJwtVerifier) PublicKey() *rsa.PublicKey { return d.pub }