-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
57 lines (43 loc) · 948 Bytes
/
rsa.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
package config
import (
"fmt"
"io"
"io/ioutil"
"gopkg.in/yaml.v3"
)
type RSAPubKeys struct {
RSAPubKeys []RSAPubKey `yaml:"rsaPubKeys"`
}
type RSAPubKey struct {
Key string `yaml:"key"`
Kid string `yaml:"kid"`
}
func NewRSAPubKeys(pubKey string) (*RSAPubKeys, error) {
key := RSAPubKey{Key: pubKey}
if err := key.validate(); err != nil {
return nil, err
}
return &RSAPubKeys{[]RSAPubKey{key}}, nil
}
func NewRSAPubKeysFromFile(reader io.Reader) (*RSAPubKeys, error) {
keysBytes, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
keys := RSAPubKeys{}
if err = yaml.Unmarshal(keysBytes, &keys); err != nil {
return nil, err
}
for _, key := range keys.RSAPubKeys {
if err = key.validate(); err != nil {
return nil, err
}
}
return &keys, nil
}
func (r RSAPubKey) validate() error {
if r.Key == "" {
return fmt.Errorf("invalid configuration, public key value is missing")
}
return nil
}