/
rsaKeyStore.go
93 lines (81 loc) · 2.11 KB
/
rsaKeyStore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package store
import (
"github.com/dgrijalva/jwt-go"
"github.com/emilhauk/identity-api/model"
"github.com/sirupsen/logrus"
"io/ioutil"
"regexp"
"strings"
)
type KeyMap map[string]model.RSAKeyPair
type RSAKeyStore struct {
keyMap KeyMap
defaultKeyPair model.RSAKeyPair
DefaultKeyId string
}
func NewRSAKeyStore(path string, defaultKeyId string) (store RSAKeyStore) {
keyMap := loadKeyMap(path)
validateKeyMap(keyMap)
defaultKeyPair, ok := keyMap[defaultKeyId]
if !ok {
logrus.Fatalln("Invalid default key id %s", defaultKeyId)
return
}
return RSAKeyStore{
keyMap,
defaultKeyPair,
defaultKeyId,
}
}
func loadKeyMap(path string) KeyMap {
dir, err := ioutil.ReadDir(path)
if err != nil {
logrus.Fatalln("Reading key store path", err)
}
reg, _ := regexp.Compile("\\.pub$")
keyMap := KeyMap{}
for _, file := range dir {
keyId := strings.NewReplacer(".pub", "").Replace(file.Name())
if _, ok := keyMap[keyId]; !ok {
keyMap[keyId] = model.RSAKeyPair{}
}
currentKey := keyMap[keyId]
data, err := ioutil.ReadFile(path + "/" + file.Name())
if err != nil {
logrus.Fatalln("Error reading key store file", err)
}
isPub := reg.Find([]byte(file.Name())) != nil
if isPub {
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(data)
if err != nil {
logrus.Fatalln("Invalid public key for id (%s)", keyId)
}
currentKey.Public = publicKey
} else {
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(data)
if err != nil {
logrus.Fatalln("Invalid private key for id (%s)", keyId)
}
currentKey.Private = privateKey
}
keyMap[keyId] = currentKey
}
return keyMap
}
func validateKeyMap(keyMap KeyMap) {
for keyName, keyPair := range keyMap {
if keyPair.Private == nil || keyPair.Public == nil {
logrus.Fatalln("Unbalanced public/private key for (%s)", keyName)
}
}
}
func (c *RSAKeyStore) GetAllKeyPairs() (keyMap KeyMap) {
return c.keyMap
}
func (c *RSAKeyStore) GetKeyPairById(id string) (keyPair model.RSAKeyPair, ok bool) {
keyPair, ok = c.keyMap[id]
return
}
func (c *RSAKeyStore) GetDefaultKeyPair() (keyPair model.RSAKeyPair) {
return c.defaultKeyPair
}