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

View File

@ -18,6 +18,7 @@ type defaultMJwtSigner struct {
}
var _ Signer = &defaultMJwtSigner{}
var _ Verifier = &defaultMJwtSigner{}
// NewMJwtSigner creates a new defaultMJwtSigner using the issuer name and rsa.PrivateKey
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)
}
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,
// generates a new private key and saves it to the file, or returns an error if
// reading or generating failed.

View File

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