forked from keel-hq/keel
/
auth.go
148 lines (115 loc) · 3.8 KB
/
auth.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
package http
import (
"encoding/json"
"fmt"
"net/http"
"github.com/alwinius/bow/pkg/auth"
request "github.com/dgrijalva/jwt-go/request"
log "github.com/sirupsen/logrus"
)
func authHeadersMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
rw.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
rw.Header().Set("Access-Control-Expose-Headers", "Authorization")
rw.Header().Set("Access-Control-Request-Headers", "Authorization")
next(rw, r)
}
func (s *TriggerServer) requireAdminAuthorization(next http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
// rw.Header().Set("Access-Control-Expose-Headers", "Authorization")
// rw.Header().Set("Access-Control-Request-Headers", "Authorization")
//
if r.Method == "OPTIONS" {
rw.WriteHeader(200)
return
}
username, password, ok := r.BasicAuth()
if ok {
resp, err := s.authenticator.Authenticate(&auth.AuthRequest{
Username: username,
Password: password,
AuthType: auth.AuthTypeBasic,
})
if err != nil {
log.WithFields(log.Fields{
"error": err,
"user": username,
"pas": password,
}).Error("failed uath")
// rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
r = auth.SetAuthenticationDetails(r, &resp.User)
next(rw, r)
return
}
// authenticating via token
resp, err := s.authenticator.Authenticate(&auth.AuthRequest{
Token: extractToken(r),
AuthType: auth.AuthTypeToken,
})
if err != nil {
// rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
log.Warnf("authentication by token failed, token: %s, err: %s", extractToken(r), err)
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
r = auth.SetAuthenticationDetails(r, &resp.User)
next(rw, r)
}
}
func extractToken(req *http.Request) string {
ex := request.AuthorizationHeaderExtractor
token, err := ex.ExtractToken(req)
if err != nil {
return ""
}
return token
}
func (s *TriggerServer) logoutHandler(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(200)
resp.Write([]byte(`{}`))
}
type loginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
func (s *TriggerServer) loginHandler(resp http.ResponseWriter, req *http.Request) {
var lr loginRequest
dec := json.NewDecoder(req.Body)
defer req.Body.Close()
err := dec.Decode(&lr)
if err != nil {
resp.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(resp, "%s", err)
return
}
authResp, err := s.authenticator.Authenticate(&auth.AuthRequest{
Username: lr.Username,
Password: lr.Password,
AuthType: auth.AuthTypeBasic,
})
if err != nil {
log.Warnf("auth failed for user '%s', error: %s", lr.Username, err)
http.Error(resp, "username or password incorrect", 401)
return
}
log.Infof("auth successful for user %s", lr.Username)
resp.Header().Add("Access-Control-Expose-Headers", "Authorization")
resp.Header().Add("Authorization", fmt.Sprintf("Bearer %s", authResp.Token))
response(authResp, 200, nil, resp, req)
}
func (s *TriggerServer) refreshHandler(resp http.ResponseWriter, req *http.Request) {
user := auth.GetAccountFromCtx(req.Context())
authResp, err := s.authenticator.GenerateToken(*user)
if err != nil {
response(nil, http.StatusOK, err, resp, req)
return
}
// adding token to header
resp.Header().Add("Access-Control-Expose-Headers", "Authorization")
resp.Header().Add("Authorization", fmt.Sprintf("Bearer %s", authResp.Token))
response(authResp, http.StatusOK, err, resp, req)
}