2022-12-04 13:42:35 +00:00
|
|
|
package mjwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
2024-06-09 16:49:57 +01:00
|
|
|
"errors"
|
2024-06-08 15:02:49 +01:00
|
|
|
"github.com/1f349/rsa-helper/rsapublic"
|
2022-12-04 13:42:35 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
)
|
|
|
|
|
2024-06-09 19:31:12 +01:00
|
|
|
var ErrNoPublicKeyFound = errors.New("no public key found")
|
|
|
|
var ErrKIDInvalid = errors.New("kid invalid")
|
|
|
|
var ErrVerifierNil = errors.New("verifier nil")
|
|
|
|
|
2023-06-19 11:40:28 +01:00
|
|
|
// defaultMJwtVerifier implements Verifier and uses a rsa.PublicKey to validate
|
2023-06-18 18:09:49 +01:00
|
|
|
// MJWT tokens
|
2022-12-04 13:42:35 +00:00
|
|
|
type defaultMJwtVerifier struct {
|
2024-06-09 16:49:57 +01:00
|
|
|
pub *rsa.PublicKey
|
|
|
|
kStore KeyStore
|
2022-12-04 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 13:03:41 +01:00
|
|
|
var _ Verifier = &defaultMJwtVerifier{}
|
2022-12-04 13:42:35 +00:00
|
|
|
|
2023-06-18 18:09:49 +01:00
|
|
|
// NewMJwtVerifier creates a new defaultMJwtVerifier using the rsa.PublicKey
|
2023-06-18 13:03:41 +01:00
|
|
|
func NewMJwtVerifier(key *rsa.PublicKey) Verifier {
|
2024-06-09 19:31:12 +01:00
|
|
|
return NewMJwtVerifierWithKeyStore(key, NewMJwtKeyStore())
|
2022-12-04 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:31:12 +01:00
|
|
|
// NewMJwtVerifierWithKeyStore creates a new defaultMJwtVerifier using a rsa.PublicKey as the non kID key
|
2024-06-09 16:49:57 +01:00
|
|
|
// and a KeyStore for kID based keys
|
2024-06-09 19:31:12 +01:00
|
|
|
func NewMJwtVerifierWithKeyStore(defaultKey *rsa.PublicKey, kStore KeyStore) Verifier {
|
2024-06-09 16:49:57 +01:00
|
|
|
return &defaultMJwtVerifier{pub: defaultKey, kStore: kStore}
|
2022-12-04 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 18:09:49 +01:00
|
|
|
// NewMJwtVerifierFromFile creates a new defaultMJwtVerifier using the path of a
|
|
|
|
// rsa.PublicKey file
|
2023-06-18 13:03:41 +01:00
|
|
|
func NewMJwtVerifierFromFile(file string) (Verifier, error) {
|
2024-06-09 16:49:57 +01:00
|
|
|
return NewMJwtVerifierFromFileAndDirectory(file, "", "", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMJwtVerifierFromDirectory creates a new defaultMJwtVerifier using the path of a directory to
|
|
|
|
// load the keys into a KeyStore; there is no default rsa.PublicKey
|
|
|
|
func NewMJwtVerifierFromDirectory(directory, prvExt, pubExt string) (Verifier, error) {
|
|
|
|
return NewMJwtVerifierFromFileAndDirectory("", directory, prvExt, pubExt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMJwtVerifierFromFileAndDirectory creates a new defaultMJwtVerifier using the path of a rsa.PublicKey
|
|
|
|
// file as the non kID key and the path of a directory to load the keys into a KeyStore
|
|
|
|
func NewMJwtVerifierFromFileAndDirectory(file, directory, prvExt, pubExt string) (Verifier, error) {
|
|
|
|
var err error
|
|
|
|
|
2024-06-08 15:02:49 +01:00
|
|
|
// read key
|
2024-06-09 16:49:57 +01:00
|
|
|
var pub *rsa.PublicKey = nil
|
|
|
|
if file != "" {
|
|
|
|
pub, err = rsapublic.Read(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read KeyStore
|
|
|
|
var kStore KeyStore = nil
|
|
|
|
if directory != "" {
|
|
|
|
kStore, err = NewMJwtKeyStoreFromDirectory(directory, prvExt, pubExt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-04 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:31:12 +01:00
|
|
|
return NewMJwtVerifierWithKeyStore(pub, kStore), nil
|
2022-12-09 13:22:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 18:09:49 +01:00
|
|
|
// VerifyJwt validates and parses MJWT tokens and returns the claims
|
2022-12-04 13:42:35 +00:00
|
|
|
func (d *defaultMJwtVerifier) VerifyJwt(token string, claims baseTypeClaim) (*jwt.Token, error) {
|
2024-06-09 16:49:57 +01:00
|
|
|
if d == nil {
|
2024-06-09 19:31:12 +01:00
|
|
|
return nil, ErrVerifierNil
|
2024-06-09 16:49:57 +01:00
|
|
|
}
|
2022-12-04 13:42:35 +00:00
|
|
|
withClaims, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
|
2024-06-09 16:49:57 +01:00
|
|
|
kIDI, exs := token.Header["kid"]
|
|
|
|
if exs {
|
|
|
|
kID, ok := kIDI.(string)
|
|
|
|
if ok {
|
|
|
|
key := d.kStore.GetKeyPublic(kID)
|
|
|
|
if key == nil {
|
2024-06-09 19:31:12 +01:00
|
|
|
return nil, ErrNoPublicKeyFound
|
2024-06-09 16:49:57 +01:00
|
|
|
} else {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-09 19:31:12 +01:00
|
|
|
return nil, ErrKIDInvalid
|
2024-06-09 16:49:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.pub == nil {
|
2024-06-09 19:31:12 +01:00
|
|
|
return nil, ErrNoPublicKeyFound
|
2024-06-09 16:49:57 +01:00
|
|
|
}
|
2022-12-04 13:42:35 +00:00
|
|
|
return d.pub, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return withClaims, claims.Valid()
|
|
|
|
}
|
2023-10-25 17:37:55 +01:00
|
|
|
|
2024-06-09 16:49:57 +01:00
|
|
|
func (d *defaultMJwtVerifier) PublicKey() *rsa.PublicKey {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.pub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *defaultMJwtVerifier) PublicKeyOf(kID string) *rsa.PublicKey {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.kStore.GetKeyPublic(kID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *defaultMJwtVerifier) GetKeyStore() KeyStore {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.kStore
|
|
|
|
}
|