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