mirror of
https://github.com/1f349/mjwt.git
synced 2024-11-12 14:31:38 +00:00
Allow access to public and private keys
This commit is contained in:
parent
8769decef2
commit
d29f481a3c
@ -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
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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 }
|
||||
|
Loading…
Reference in New Issue
Block a user