-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
120 lines (102 loc) · 2.97 KB
/
util.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package auth
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/base32"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"strings"
"github.com/golang-jwt/jwt"
)
func containsAction(actionsList []string, action string) bool {
for _, a := range actionsList {
if a == action {
return true
}
}
return false
}
func generateBasicAuthHeader(username string, password string) string {
base := username + ":" + password
basicAuthHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
return basicAuthHeader
}
func generatePublicKey(publicKeyPath string, publicKey []byte) (*rsa.PublicKey, error) {
pem, err := getPemFromPathOrKey(publicKeyPath, publicKey)
if err != nil {
return nil, err
}
// https://github.com/golang-jwt/jwt/blob/main/rsa_test.go
return jwt.ParseRSAPublicKeyFromPEM(pem)
}
func generatePrivateKey(privateKeyPath string, privateKey []byte) (*rsa.PrivateKey, error) {
pem, err := getPemFromPathOrKey(privateKeyPath, privateKey)
if err != nil {
return nil, err
}
return jwt.ParseRSAPrivateKeyFromPEM(pem)
}
func getPemFromPathOrKey(keyPath string, key []byte) ([]byte, error) {
var pem []byte
var err error
if keyPath != "" {
pem, err = ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
} else if key != nil {
pem = key
} else {
return nil, errors.New("must supply either cert path or cert")
}
return pem, nil
}
func getTokenCustomClaims(token *jwt.Token) (*Claims, error) {
c := token.Claims.(jwt.MapClaims)
byteData, err := json.Marshal(c)
if err != nil {
return nil, err
}
claims := Claims{}
json.Unmarshal(byteData, &claims)
return &claims, nil
}
// adapted from "" method here: https://github.com/docker/libtrust/blob/aabc10ec26b754e797f9028f4589c5b7bd90dc20/util.go#L194
func generateKIDFromPublicKey(pubKey *rsa.PublicKey) (string, error) {
derBytes, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return "", err
}
hasher := crypto.SHA256.New()
hasher.Write(derBytes)
return keyIDEncode(hasher.Sum(nil)[:30]), nil
}
// copied from here: https://github.com/docker/libtrust/blob/aabc10ec26b754e797f9028f4589c5b7bd90dc20/util.go#L181
func keyIDEncode(b []byte) string {
s := strings.TrimRight(base32.StdEncoding.EncodeToString(b), "=")
var buf bytes.Buffer
var i int
for i = 0; i < len(s)/4-1; i++ {
start := i * 4
end := start + 4
buf.WriteString(s[start:end] + ":")
}
buf.WriteString(s[i*4:])
return buf.String()
}