mjwt/verifier.go

119 lines
3.2 KiB
Go
Raw Normal View History

2022-12-04 13:42:35 +00:00
package mjwt
import (
"crypto/rsa"
"errors"
"github.com/1f349/rsa-helper/rsapublic"
2022-12-04 13:42:35 +00:00
"github.com/golang-jwt/jwt/v4"
)
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 {
pub *rsa.PublicKey
kStore KeyStore
2022-12-04 13:42:35 +00: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
func NewMJwtVerifier(key *rsa.PublicKey) Verifier {
return NewMjwtVerifierWithKeyStore(key, NewMJwtKeyStore())
2022-12-04 13:42:35 +00:00
}
// NewMjwtVerifierWithKeyStore creates a new defaultMJwtVerifier using a rsa.PublicKey as the non kID key
// and a KeyStore for kID based keys
func NewMjwtVerifierWithKeyStore(defaultKey *rsa.PublicKey, kStore KeyStore) Verifier {
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
func NewMJwtVerifierFromFile(file string) (Verifier, error) {
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
// read key
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
}
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) {
if d == nil {
return nil, errors.New("verifier nil")
}
2022-12-04 13:42:35 +00:00
withClaims, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
kIDI, exs := token.Header["kid"]
if exs {
kID, ok := kIDI.(string)
if ok {
key := d.kStore.GetKeyPublic(kID)
if key == nil {
return nil, errors.New("no public key found")
} else {
return key, nil
}
} else {
return nil, errors.New("kid invalid")
}
}
if d.pub == nil {
return nil, errors.New("no public key found")
}
2022-12-04 13:42:35 +00:00
return d.pub, nil
})
if err != nil {
return nil, err
}
return withClaims, claims.Valid()
}
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
}