/
authenticator.go
155 lines (141 loc) · 4.54 KB
/
authenticator.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package saltboot
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"fmt"
)
type SignatureMethod int
const (
SIGNED SignatureMethod = iota
OPEN
SIGNATURE = "signature"
SIGNED_CONTENT = "signed"
)
type Authenticator struct {
Username string
Password string
SignatureKey []byte
}
func (a *Authenticator) Wrap(handler func(w http.ResponseWriter, req *http.Request), signatureMethod SignatureMethod) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if a.Username == "" || a.Password == "" || len(a.SignatureKey) == 0 {
log.Printf("[Authenticator] missing Username, Password or SignatureKey we are going to load it")
securityConfig, err := DetermineSecurityDetails(os.Getenv, defaultSecurityConfigLoc)
if err != nil {
errorMsg := fmt.Sprintf("Failed to get security config: %s", err.Error())
log.Printf("[Authenticator] [ERROR] %s", errorMsg)
w.WriteHeader(http.StatusUnauthorized)
if _, err = w.Write([]byte("401 Unauthorized: " + errorMsg)); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't write response: %s", err.Error())
}
return
}
a.Username = securityConfig.Username
a.Password = securityConfig.Password
a.SignatureKey = []byte(securityConfig.SignVerifyKey)
}
valid := CheckAuth(a.Username, a.Password, r)
if !valid {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("401 Unauthorized")); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't write response: %s", err.Error())
}
return
}
if signatureMethod == SIGNED {
body := new(bytes.Buffer)
if strings.Index(r.Header.Get("Content-Type"), "multipart") == 0 {
file, _, _ := r.FormFile("file")
defer closeIt(file)
if _, err := ioutil.ReadAll(io.TeeReader(file, body)); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't read body: %s", err.Error())
}
} else {
defer closeIt(r.Body)
if _, err := ioutil.ReadAll(io.TeeReader(r.Body, body)); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't read body: %s", err.Error())
}
r.Body = ioutil.NopCloser(body)
r.Header.Set(SIGNED_CONTENT, string(body.Bytes()))
}
signature := strings.TrimSpace(r.Header.Get(SIGNATURE))
if !CheckSignature(signature, a.SignatureKey, body.Bytes()) {
w.WriteHeader(http.StatusNotAcceptable)
if _, err := w.Write([]byte("406 Not Acceptable")); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't write response: %s", err.Error())
}
return
}
}
w.Header().Set("Content-Type", "application/json")
http.HandlerFunc(handler).ServeHTTP(w, r)
})
}
func CheckAuth(user string, pass string, r *http.Request) bool {
hUser, hPassword := GetAuthUserPass(r)
result := user == hUser && pass == hPassword
if !result {
log.Printf("[Authenticator] invalid autorization header: %s from %s", r.Header.Get("Authorization"), r.Host)
}
return result
}
func CheckSignature(rawSign string, pubPem []byte, data []byte) bool {
var err error
var sign []byte
var pub interface{}
sign, err = base64.StdEncoding.DecodeString(rawSign)
if err == nil {
block, _ := pem.Decode(pubPem)
if block != nil {
pub, err = x509.ParsePKIXPublicKey(block.Bytes)
if err == nil {
newHash := crypto.SHA256.New()
if _, err = newHash.Write(data); err != nil {
log.Printf("[Authenticator] [ERROR] couldn't write new hash: %s", err.Error())
return false
}
opts := rsa.PSSOptions{SaltLength: 20}
err = rsa.VerifyPSS(pub.(*rsa.PublicKey), crypto.SHA256, newHash.Sum(nil), sign, &opts)
if err == nil {
return true
}
}
} else {
err = errors.New("unable to decode PEM")
}
}
log.Printf("[Authenticator] [ERROR] unable to check signature: %s", err.Error())
return false
}
func GetAuthUserPass(r *http.Request) (string, string) {
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
log.Printf("[Authenticator] Missing Basic authorization header")
return "", ""
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
log.Printf("[Authenticator] [ERROR] Authorization header is not MD5 encoded: %s", err.Error())
return "", ""
}
pair := strings.Split(string(b), ":")
if len(pair) != 2 {
log.Printf("[Authenticator] Missing username/password")
return "", ""
}
return pair[0], pair[1]
}
func GetSignatureAndSigned(r *http.Request) (string, string) {
return strings.TrimSpace(r.Header.Get(SIGNATURE)), r.Header.Get(SIGNED_CONTENT)
}