8000 Add private key read function to JWT config by rdner · Pull Request #18 · contiamo/go-base · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Add private key read function to JWT config #18

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/config/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type JWT struct {
// PublicKeyPath is a path to the public key for JWT signature verification
PublicKeyPath string `json:"publicKeyPath"`
// PrivateKeyPath is a path to the private key for signing JWT
PrivateKeyPath string `json:"privateKeyPath"`
}

// GetPublicKey gets the encryption key from a given path
Expand All @@ -27,3 +29,17 @@ func (j *JWT) GetPublicKey() (publicKey *rsa.PublicKey, err error) {

return jwt.ParseRSAPublicKeyFromPEM(keyBytes)
}

// GetPrivateKey gets the encryption key from a given path
func (j *JWT) GetPrivateKey() (privateKey *rsa.PrivateKey, err error) {
if j.PrivateKeyPath == "" {
return nil, errors.New("path to the private key file is empty")
}

keyBytes, err := ioutil.ReadFile(j.PrivateKeyPath)
if err != nil {
return nil, errors.Wrapf(err, "can not read the private key file `%s`", j.PrivateKeyPath)
}

return jwt.ParseRSAPrivateKeyFromPEM(keyBytes)
}
31 changes: 31 additions & 0 deletions pkg/config/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,34 @@ func TestJWTGetPublicKey(t *testing.T) {
require.NotNil(t, key)
})
}

func TestJWTGetPrivateKey(t *testing.T) {
t.Run("Returns error when the path is empty", func(t *testing.T) {
cfg := JWT{}
key, err := cfg.GetPrivateKey()
require.Nil(t, key)
require.Error(t, err)
require.Equal(t, "path to the private key file is empty", err.Error())
})
t.Run("Returns error when the path is not PEM certificate", func(t *testing.T) {
cfg := JWT{PrivateKeyPath: "./testdata/password"}
key, err := cfg.GetPrivateKey()
require.Nil(t, key)
require.Error(t, err)
require.Equal(t, "Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key", err.Error())
})
t.Run("Returns error when the path is not found", func(t *testing.T) {
cfg := JWT{PrivateKeyPath: "./testdata/invalid"}
key, err := cfg.GetPrivateKey()
require.Nil(t, key)
require.Error(t, err)
require.Equal(t, "can not read the private key file `./testdata/invalid`: open ./testdata/invalid: no such file or directory", err.Error())
})

t.Run("Returns key when the path is valid", func(t *testing.T) {
cfg := JWT{PrivateKeyPath: "./testdata/test.key"}
key, err := cfg.GetPrivateKey()
require.NoError(t, err)
require.NotNil(t, key)
})
}
27 changes: 27 additions & 0 deletions pkg/config/testdata/test.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAqAFYvQQlLXgwIV1GyHI7KZvEAfwmLIL03qSc+p3HZwkbhU9k
sXrHAJG+PiHBddYluzMvrk6rPQUqX2QtkOXYN62nItAYjxr5SJopQ2iMTxI1UVz2
vWN2ZXa3bZdKmCSSymc9GY+TEHT3JoxQ10T1N89xZZnWaC+4jK61wXsS9YIrpdNj
I9/axo20b9EtFmDk+5ZtFlEDPkQAd1uLPHOW7VajB7WVM7PGIUdoHp1Z7O4vkxGk
eaawnYzsV3CdOx1mXBexdb37oTpCFJePPzeN4QDcfc9Zw6nvcojwxlAzUVjAf4aV
QsRxjSUzi7r3Knmf6vrm1jKHCZm8mnPjYVNfqwIDAQABAoIBAFpILvmUCq5+kc68
I1eAG07mx9QRPipZTWF/sGP0+bzi5gkjewbs5ZfmugcFeMLsC2anwQ24dMjbx8Nv
QBVHjoONK+3HSgtNAX0EavwwaIXtUP2iKW3ozy0DONsJnCpaLQ/bnrfnsa38TPxI
FwE3q1cMgPGKU++1BUWEdWO90ZhgNlNxx7ugVi9Yyh0boLVmTvG3abvlON+M0kuQ
iUyel+8c3E4PmLOmM758q14hy9JZd5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