-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_handler.go
336 lines (306 loc) · 9.96 KB
/
auth_handler.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package aggregadantur
import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt/v4"
"github.com/gorilla/sessions"
"github.com/orange-cloudfoundry/aggregadantur/contexes"
"github.com/orange-cloudfoundry/aggregadantur/jwtclaim"
"github.com/orange-cloudfoundry/aggregadantur/models"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"io"
"net/http"
"net/url"
"strings"
)
type AuthHandler struct {
next http.Handler
aggrRoute *models.AggregateRoute
httpClient *http.Client
store sessions.Store
}
func NewAuthHandler(next http.Handler, aggrRoute *models.AggregateRoute, httpClient *http.Client, store sessions.Store) *AuthHandler {
return &AuthHandler{
next: next,
aggrRoute: aggrRoute,
httpClient: httpClient,
store: store,
}
}
func (a AuthHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if a.aggrRoute.OptionsPassthrough && req.Method == http.MethodOptions {
a.next.ServeHTTP(w, req)
return
}
if !a.aggrRoute.MatchAuth(req) {
a.next.ServeHTTP(w, req)
return
}
_, _, hasBasicAuth := req.BasicAuth()
if a.aggrRoute.Auth.BasicAuth != nil || hasBasicAuth {
a.basicAuth(w, req)
return
}
jwtToken := a.retrieveJwt(req)
if jwtToken == "" {
a.loginPage(w, req)
return
}
a.checkJwt(jwtToken, w, req)
}
func (a AuthHandler) checkJwt(jwtTokenRaw string, w http.ResponseWriter, req *http.Request) {
parser := jwt.Parser{}
parsedToken, _, err := parser.ParseUnverified(jwtTokenRaw, &jwtclaim.ScopeClaims{})
if err != nil {
panic(err)
}
claim := parsedToken.Claims.(*jwtclaim.ScopeClaims)
jwtCheck, err := a.aggrRoute.Auth.JWTCheck.FindJWTCheckByIssuer(claim.Issuer)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
signingMethod := jwt.GetSigningMethod(jwtCheck.Alg)
if signingMethod == nil {
http.Error(w, fmt.Sprintf("algorithm '%s' doesn't exists.", jwtCheck.Alg), http.StatusUnauthorized)
return
}
whichScope := ""
for _, scopeByPriorities := range a.aggrRoute.Auth.Oauth2Auth.Scopes {
for _, scope := range claim.Scope {
if scope == scopeByPriorities {
whichScope = scope
break
}
}
}
if whichScope == "" {
http.Error(w, "you have no valid scopes.", http.StatusUnauthorized)
return
}
_, err = jwt.Parse(jwtTokenRaw, func(token *jwt.Token) (interface{}, error) {
return checkTokenfunc(token, jwtCheck, signingMethod)
})
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
req.Header.Set(XAggregatorScopesHeader, strings.Join(claim.Scope, ","))
req.Header.Set(XAggregatorUsernameHeader, claim.Username)
req.Header.Set("Authorization", "Bearer "+jwtTokenRaw)
contexes.SetUsername(req, claim.Username)
contexes.SetScopes(req, claim.Scope)
contexes.SetJwtClaim(req, claim)
a.next.ServeHTTP(w, req)
}
func (a AuthHandler) retrieveJwt(req *http.Request) string {
session := a.getSession(req)
if j, ok := session.Values["jwt_token"]; ok {
return j.(string)
}
authorization := req.Header.Get("Authorization")
authSplit := strings.SplitN(authorization, " ", 2)
if len(authSplit) >= 2 && strings.EqualFold(authSplit[0], "bearer") {
return authSplit[1]
}
return ""
}
func (a AuthHandler) loginPage(w http.ResponseWriter, req *http.Request) {
redirectUrl := req.URL.Path
if redirectUrl == "" {
redirectUrl = "/"
}
if req.URL.RawQuery != "" {
redirectUrl += "?" + req.URL.RawQuery
}
loginPageTemplate, err := a.aggrRoute.Auth.MakeLoginPageTemplate(DefaultLoginTemplate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if req.Method == http.MethodGet {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(makeLoginPageHtml(
loginPageTemplate,
cases.Title(language.AmericanEnglish).String(a.aggrRoute.Name),
redirectUrl,
)))
return
}
err = req.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
authToken, err := a.oauth2Auth(req)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(fmt.Sprintf(
`<html><head><meta http-equiv="refresh" content="3;url=%s" /></head><body><h1>You are not authorized: %s.</h1></body></html>`,
req.URL.Path, err.Error())))
return
}
session := a.getSession(req)
session.Values["jwt_token"] = authToken.AccessToken
session.Options.MaxAge = authToken.ExpiresIn
err = session.Save(req, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req.Header.Del("Authorization")
http.Redirect(w, req, redirectUrl, http.StatusTemporaryRedirect)
}
func (a AuthHandler) getSession(req *http.Request) *sessions.Session {
session, _ := a.store.Get(req, "auth-"+a.aggrRoute.Name)
return session
}
func (a AuthHandler) basicAuth(w http.ResponseWriter, req *http.Request) {
user, password, hasBasicAuth := req.BasicAuth()
if !hasBasicAuth || user == "" {
http.Error(w, "no user provided", http.StatusUnauthorized)
return
}
if a.aggrRoute.Auth.BasicAuth != nil {
givenUser := sha256.Sum256([]byte(user))
givenPass := sha256.Sum256([]byte(password))
requiredUser := sha256.Sum256([]byte(a.aggrRoute.Auth.BasicAuth.Username))
requiredPass := sha256.Sum256([]byte(a.aggrRoute.Auth.BasicAuth.Password))
match := subtle.ConstantTimeCompare(givenUser[:], requiredUser[:]) == 1 &&
subtle.ConstantTimeCompare(givenPass[:], requiredPass[:]) == 1
if !match {
http.Error(w, "Wrong user/password", http.StatusUnauthorized)
return
}
req.Header.Set(XAggregatorScopesHeader, strings.Join(a.aggrRoute.Auth.BasicAuth.Scopes, ","))
req.Header.Set(XAggregatorUsernameHeader, user)
contexes.SetUsername(req, user)
contexes.SetScopes(req, a.aggrRoute.Auth.BasicAuth.Scopes)
a.next.ServeHTTP(w, req)
return
}
accessResp, err := a.oauth2Auth(req)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
a.checkJwt(accessResp.AccessToken, w, req)
}
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
}
func (a AuthHandler) oauth2Auth(origReq *http.Request) (AccessTokenResponse, error) {
var body io.Reader
var contentType string
authOption := a.aggrRoute.Auth.Oauth2Auth
user := origReq.Form.Get("username")
password := origReq.Form.Get("password")
userBasicAuth, passwordBasicAuth, hasBasicAuth := origReq.BasicAuth()
if user == "" && hasBasicAuth && userBasicAuth != "" {
user = userBasicAuth
password = passwordBasicAuth
}
if user == "" {
return AccessTokenResponse{}, fmt.Errorf("no user provided")
}
if authOption.ParamsAsJson {
body, contentType = a.generateJsonBody(user, password)
} else {
body, contentType = a.generateFormBody(user, password)
}
req, _ := http.NewRequest("POST", authOption.TokenURL, body)
req.SetBasicAuth(authOption.ClientID, authOption.ClientSecret)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", contentType)
resp, err := a.httpClient.Do(req)
if err != nil {
return AccessTokenResponse{}, fmt.Errorf("when getting token for %s: %s", user, err.Error())
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
if resp.StatusCode == 401 || resp.StatusCode == 403 {
return AccessTokenResponse{}, fmt.Errorf("%d: Unauthorized on uaa", resp.StatusCode)
}
b, _ := io.ReadAll(resp.Body)
return AccessTokenResponse{}, fmt.Errorf("from oauth server %d: %s", resp.StatusCode, string(b))
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return AccessTokenResponse{}, fmt.Errorf("when getting token for %s: %s", user, err.Error())
}
var accessResp AccessTokenResponse
err = json.Unmarshal(b, &accessResp)
if err != nil {
return AccessTokenResponse{}, fmt.Errorf("when getting token for %s: %s", user, err.Error())
}
scopes := strings.Split(accessResp.Scope, " ")
for _, scopeByPriorities := range authOption.Scopes {
for _, scope := range scopes {
if scope == scopeByPriorities {
origReq.Header.Set(XAggregatorScopesHeader, scopeByPriorities)
return accessResp, nil
}
}
}
return AccessTokenResponse{}, fmt.Errorf("you have no valid scopes.")
}
func (a AuthHandler) generateFormBody(user, password string) (io.Reader, string) {
formValues := make(url.Values)
formValues.Add("grant_type", "password")
formValues.Add("username", user)
formValues.Add("password", password)
tokenFormat := a.aggrRoute.Auth.Oauth2Auth.TokenFormat
if tokenFormat != "" {
formValues.Add("token_format", tokenFormat)
}
return strings.NewReader(formValues.Encode()), "application/x-www-form-urlencoded"
}
func (a AuthHandler) generateJsonBody(user, password string) (io.Reader, string) {
params := struct {
GrantType string `json:"grant_type"`
Username string `json:"username"`
Password string `json:"password"`
TokenFormat string `json:"token_format,omitempty"`
}{"password", user, password, a.aggrRoute.Auth.Oauth2Auth.TokenFormat}
b, _ := json.Marshal(params)
return bytes.NewReader(b), "application/json"
}
func checkTokenfunc(token *jwt.Token, jwtCheck models.JWTCheck, signingMethod jwt.SigningMethod) (interface{}, error) {
mapClaims := token.Claims.(jwt.MapClaims)
if jwtCheck.NotVerifyIssuedAt {
mapClaims["iat"] = ""
}
err := mapClaims.Valid()
if err != nil {
return nil, err
}
if jwtCheck.Issuer != "" && !mapClaims.VerifyIssuer(jwtCheck.Issuer, true) {
return nil, fmt.Errorf("Token doesn't contains the requested issuer.")
}
return getSecretEncoded(jwtCheck.Secret, signingMethod)
}
func getSecretEncoded(secret string, signingMethod jwt.SigningMethod) (interface{}, error) {
bSecret := []byte(secret)
if strings.HasPrefix(signingMethod.Alg(), "HS") {
return bSecret, nil
}
if strings.HasPrefix(signingMethod.Alg(), "ES") {
encSecret, err := jwt.ParseECPublicKeyFromPEM(bSecret)
if err == nil {
return encSecret, nil
}
return jwt.ParseECPrivateKeyFromPEM(bSecret)
}
// if no return token use RSA
encSecret, err := jwt.ParseRSAPublicKeyFromPEM(bSecret)
if err == nil {
return encSecret, nil
}
return jwt.ParseRSAPrivateKeyFromPEM(bSecret)
}