-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
363 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu | ||
KUpRKfFLfRYC9AIKjbJTWit+CqvjWYzvQwECAwEAAQJAIJLixBy2qpFoS4DSmoEm | ||
o3qGy0t6z09AIJtH+5OeRV1be+N4cDYJKffGzDa88vQENZiRm0GRq6a+HPGQMd2k | ||
TQIhAKMSvzIBnni7ot/OSie2TmJLY4SwTQAevXysE2RbFDYdAiEBCUEaRQnMnbp7 | ||
9mxDXDf6AU0cN/RPBjb9qSHDcWZHGzUCIG2Es59z8ugGrDY+pxLQnwfotadxd+Uy | ||
v/Ow5T0q5gIJAiEAyS4RaI9YG8EWx/2w0T67ZUVAw8eOMB6BIUg0Xcu+3okCIBOs | ||
/5OiPgoTdSy7bcF9IGpSE8ZgGKzgYQVZeN97YE00 | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIID0DCCArigAwIBAgIBATANBgkqhkiG9w0BAQUFADB/MQswCQYDVQQGEwJGUjET | ||
MBEGA1UECAwKU29tZS1TdGF0ZTEOMAwGA1UEBwwFUGFyaXMxDTALBgNVBAoMBERp | ||
bWkxDTALBgNVBAsMBE5TQlUxEDAOBgNVBAMMB0RpbWkgQ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WSIyzQ16fyGve+ANf6vXvUizyvwDrPRv/kfvLNa3ZPnLMMxU98Mvh | ||
PXy3PkB8++6U4Y3vdk2Ni2WYYlIls8yqbM4327IKmkDc2TimS8u60CT47mKU7aDY | ||
cbTV5RDkrlaYwm5yqlTIglvCv7o= | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package key | ||
|
||
import ( | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/megablend/jwt-encryption/pkg/config" | ||
logger "github.com/sirupsen/logrus" | ||
"gopkg.in/square/go-jose.v2" | ||
) | ||
|
||
type Key struct { | ||
Config *config.Config | ||
} | ||
|
||
type SignerType int | ||
|
||
func (s SignerType) String() jose.ContentType { | ||
switch s { | ||
case JWT: | ||
return "JWT" | ||
case JWE: | ||
return "JWE" | ||
default: | ||
return "UNKNOWN" | ||
} | ||
} | ||
|
||
const ( | ||
JWT SignerType = iota | ||
JWE | ||
) | ||
|
||
// PrivateKey returns a private key object from the configured path | ||
func (k *Key) PrivateKey() (*rsa.PrivateKey, error) { | ||
bytes, err := k.getFileBytes(k.Config.Jwt.Directory, k.Config.Jwt.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
block, err := k.getBlockBytes(bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key, err := x509.ParsePKCS1PrivateKey(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return key, nil | ||
} | ||
|
||
func (k *Key) PublicKey() (*rsa.PublicKey, error) { | ||
bytes, err := k.getFileBytes(k.Config.Jwt.Directory, k.Config.Jwt.PublicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert, err := k.parseFromCert(bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key, ok := cert.PublicKey.(*rsa.PublicKey) | ||
if !ok { | ||
return nil, errors.New("unable to parse certificate into a private key") | ||
} | ||
return key, nil | ||
} | ||
|
||
// Signer returns a preferred method of signing token based on the algorithm and type provided | ||
func (k *Key) Signer(headers map[string]string, algorithm jose.SignatureAlgorithm, signerType SignerType) (jose.Signer, error) { | ||
privateKey, err := k.PrivateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signerKey := jose.SigningKey{Algorithm: algorithm, Key: privateKey} | ||
|
||
var signerOpts = jose.SignerOptions{} | ||
signerOpts.WithType(signerType.String()) | ||
|
||
// populate custom headers | ||
if len(headers) > 0 { | ||
for k, v := range headers { | ||
signerOpts.WithHeader(jose.HeaderKey(k), v) | ||
} | ||
} | ||
|
||
return jose.NewSigner(signerKey, &signerOpts) | ||
} | ||
|
||
func (k *Key) getBlockBytes(bytes []byte) ([]byte, error) { | ||
block, _ := pem.Decode(bytes) | ||
encrypted := x509.IsEncryptedPEMBlock(block) | ||
b := block.Bytes | ||
|
||
if encrypted { | ||
logger.Warn("PEM block is encrypted") | ||
var err error | ||
b, err = x509.DecryptPEMBlock(block, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return b, nil | ||
} | ||
|
||
func (k *Key) getFileBytes(dir, key string) ([]byte, error) { | ||
dir, err := k.getBasePath(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes, err := os.ReadFile(fmt.Sprintf("%s/%s", dir, key)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bytes, nil | ||
} | ||
|
||
func (k *Key) parseFromCert(bytes []byte) (*x509.Certificate, error) { | ||
b, err := k.getBlockBytes(bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return x509.ParseCertificate(b) | ||
} | ||
|
||
func (k *Key) getBasePath(keyPath string) (string, error) { | ||
if _, err := os.Stat(keyPath); os.IsNotExist(err) { | ||
logger.Warn("the configured keys directory does not exist") | ||
|
||
_, fileName, _, ok := runtime.Caller(0) | ||
if !ok { | ||
return "", errors.New("failed to retrieve caller information for key path configuration") | ||
} | ||
|
||
subDir := "pkg/key" | ||
dir := path.Dir(fileName) | ||
if !strings.Contains(dir, subDir) { | ||
return "", errors.New("invalid configuration folder path configured") | ||
} | ||
|
||
basePath := strings.ReplaceAll(dir, subDir, k.Config.Jwt.Directory) | ||
// ensure that the final base path is valid | ||
if _, err := os.Stat(basePath); os.IsNotExist(err) { | ||
return "", errors.New("invalid configuration base path") | ||
} | ||
|
||
return basePath, nil | ||
} | ||
return keyPath, nil | ||
} | ||
|
||
func New(config *config.Config) *Key { | ||
return &Key{ | ||
Config: config, | ||
} | ||
} |
Oops, something went wrong.