-
Notifications
You must be signed in to change notification settings - Fork 90
/
login.go
438 lines (370 loc) · 12.5 KB
/
login.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package handlers
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
oidc "github.com/coreos/go-oidc"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/handlers/types"
"github.com/replicatedhq/kots/pkg/identity"
identityclient "github.com/replicatedhq/kots/pkg/identity/client"
ingress "github.com/replicatedhq/kots/pkg/ingress"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/session"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/user"
usertypes "github.com/replicatedhq/kots/pkg/user/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/segmentio/ksuid"
"golang.org/x/oauth2"
)
type LoginRequest struct {
Password string `json:"password"`
}
type LoginResponse struct {
Error string `json:"error,omitempty"`
}
type LoginMethod string
const (
PasswordAuth LoginMethod = "shared-password"
IdentityService LoginMethod = "identity-service"
SessionTimeout = time.Hour * 12
)
func getRedirectOnErrorURL(redirectURL string, errorMsg string) string {
msg := map[string]string{
"error": errorMsg,
}
msgJson, _ := json.Marshal(msg)
v := url.Values{}
v.Set("message", base64.StdEncoding.EncodeToString(msgJson))
return fmt.Sprintf("%s/secure-console?%s", redirectURL, v.Encode())
}
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
identityConfig, err := identity.GetConfig(r.Context(), util.PodNamespace)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if identityConfig.Spec.Enabled && identityConfig.Spec.DisablePasswordAuth {
err := errors.New("password authentication disabled")
JSON(w, http.StatusForbidden, types.NewErrorResponse(err))
return
}
loginResponse := LoginResponse{}
loginRequest := LoginRequest{}
if err := json.NewDecoder(r.Body).Decode(&loginRequest); err != nil {
logger.Error(err)
JSON(w, http.StatusBadRequest, loginResponse)
return
}
foundUser, err := user.LogIn(loginRequest.Password)
if err == user.ErrInvalidPassword {
loginResponse.Error = "Invalid password. Please try again."
JSON(w, http.StatusUnauthorized, loginResponse)
return
} else if err == user.ErrTooManyAttempts {
resetPasswordCmd := "kubectl kots reset-password"
if util.PodNamespace != "" {
resetPasswordCmd = fmt.Sprintf("%s -n %s", resetPasswordCmd, util.PodNamespace)
}
loginResponse.Error = fmt.Sprintf("Admin Console has been locked. Please reset password using the \"%s\" command.", resetPasswordCmd)
JSON(w, http.StatusUnauthorized, loginResponse)
return
} else if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// TODO: super user permissions
roles := session.GetSessionRolesFromRBAC(nil, identity.DefaultGroups)
issuedAt, expiresAt := time.Now(), time.Now().Add(SessionTimeout)
createdSession, err := store.GetStore().CreateSession(foundUser, issuedAt, expiresAt, roles)
if err != nil {
logger.Error(err)
JSON(w, http.StatusInternalServerError, loginResponse)
return
}
signedJWT, err := session.SignJWT(createdSession)
if err != nil {
logger.Error(err)
JSON(w, http.StatusInternalServerError, loginResponse)
return
}
responseToken := fmt.Sprintf("Bearer %s", signedJWT)
expiration := time.Now().Add(SessionTimeout)
origin := r.Header.Get("Origin")
tokenCookie, err := session.GetSessionCookie(responseToken, expiration, origin)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get session cookie"))
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, tokenCookie)
JSON(w, http.StatusOK, loginResponse)
}
type OIDCLoginResponse struct {
AuthCodeURL string `json:"authCodeURL"`
Error string `json:"error,omitempty"`
}
func (h *Handler) OIDCLogin(w http.ResponseWriter, r *http.Request) {
namespace := util.PodNamespace
oidcLoginResponse := OIDCLoginResponse{}
clientset, err := k8sutil.GetClientset()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get k8s client"))
w.WriteHeader(http.StatusInternalServerError)
return
}
provider, err := identity.GetKotsadmOIDCProvider(r.Context(), clientset, namespace)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get kotsadm oidc provider"))
w.WriteHeader(http.StatusInternalServerError)
return
}
oauth2Config, err := identity.GetKotsadmOAuth2Config(r.Context(), clientset, namespace, *provider)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get kotsadm oauth2 config"))
w.WriteHeader(http.StatusInternalServerError)
return
}
// generate a random state
state := ksuid.New().String()
// save the generated state to compare on callback
if err := identityclient.SetOIDCState(r.Context(), namespace, state); err != nil {
logger.Error(errors.Wrap(err, "failed to set oidc state"))
w.WriteHeader(http.StatusInternalServerError)
return
}
authCodeURL := oauth2Config.AuthCodeURL(state)
oidcLoginResponse.AuthCodeURL = authCodeURL
// return a response instead of a redirect because Dex doesn't allow redirects from different origins (CORS)
JSON(w, http.StatusOK, oidcLoginResponse)
}
func (h *Handler) OIDCLoginCallback(w http.ResponseWriter, r *http.Request) {
namespace := util.PodNamespace
clientset, err := k8sutil.GetClientset()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get k8s client"))
w.WriteHeader(http.StatusInternalServerError)
return
}
provider, err := identity.GetKotsadmOIDCProvider(r.Context(), clientset, namespace)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get kotsadm oidc provider"))
w.WriteHeader(http.StatusInternalServerError)
return
}
oauth2Config, err := identity.GetKotsadmOAuth2Config(r.Context(), clientset, namespace, *provider)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get kotsadm oauth2 config"))
w.WriteHeader(http.StatusInternalServerError)
return
}
identityConfig, err := identity.GetConfig(r.Context(), namespace)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get identity config"))
w.WriteHeader(http.StatusInternalServerError)
return
}
ingressConfig, err := ingress.GetConfig(r.Context(), namespace)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get ingress config"))
w.WriteHeader(http.StatusInternalServerError)
return
}
redirectURL := identityConfig.Spec.AdminConsoleAddress
if redirectURL == "" && ingressConfig.Spec.Enabled {
redirectURL = ingress.GetAddress(ingressConfig.Spec)
}
var token *oauth2.Token
switch r.Method {
case http.MethodGet:
// Authorization redirect callback from OAuth2 auth flow.
if errMsg := r.FormValue("error"); errMsg != "" {
logger.Error(errors.Wrapf(err, "%s: %s", errMsg, r.FormValue("error_description")))
w.WriteHeader(http.StatusBadRequest)
return
}
code := r.FormValue("code")
if code == "" {
logger.Error(errors.New("no code in request"))
w.WriteHeader(http.StatusBadRequest)
return
}
state := r.FormValue("state")
foundState, err := identityclient.GetOIDCState(r.Context(), namespace, state)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get saved oidc state"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if foundState == "" {
logger.Error(errors.Errorf("invalid state %s", state))
w.WriteHeader(http.StatusBadRequest)
return
}
if err := identityclient.ResetOIDCState(r.Context(), namespace, state); err != nil {
logger.Error(errors.Wrap(err, "failed to reset oidc state"))
w.WriteHeader(http.StatusInternalServerError)
return
}
httpClient, err := identityclient.HTTPClient(r.Context(), namespace, *identityConfig)
if err != nil {
err = errors.Wrap(err, "failed to get identity http client")
logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
ctx := oidc.ClientContext(r.Context(), httpClient)
token, err = oauth2Config.Exchange(ctx, code)
if err != nil {
logger.Error(errors.Wrap(err, "failed to exchange token"))
w.WriteHeader(http.StatusBadRequest)
return
}
case http.MethodPost:
// Form request from frontend to refresh a token.
refresh := r.FormValue("refresh_token")
if refresh == "" {
logger.Error(errors.New("no refresh_token in request"))
w.WriteHeader(http.StatusBadRequest)
return
}
httpClient, err := identityclient.HTTPClient(r.Context(), namespace, *identityConfig)
if err != nil {
err = errors.Wrap(err, "failed to get identity http client")
logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
ctx := oidc.ClientContext(r.Context(), httpClient)
t := &oauth2.Token{
RefreshToken: refresh,
Expiry: time.Now().Add(-time.Hour),
}
token, err = oauth2Config.TokenSource(ctx, t).Token()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get token"))
w.WriteHeader(http.StatusBadRequest)
return
}
default:
logger.Error(errors.Errorf("method not implemented: %s", r.Method))
w.WriteHeader(http.StatusBadRequest)
return
}
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
logger.Error(errors.New("no id_token in token response"))
w.WriteHeader(http.StatusInternalServerError)
return
}
verifier := provider.Verifier(&oidc.Config{ClientID: oauth2Config.ClientID})
idToken, err := verifier.Verify(r.Context(), rawIDToken)
if err != nil {
logger.Error(errors.Wrap(err, "failed to verify ID token"))
w.WriteHeader(http.StatusInternalServerError)
return
}
var claims struct {
Email string `json:"email"`
Name string `json:"name"`
Verified bool `json:"email_verified"`
Groups []string `json:"groups"`
}
if err := idToken.Claims(&claims); err != nil {
logger.Error(errors.Wrap(err, "error decoding ID token claims"))
w.WriteHeader(http.StatusInternalServerError)
return
}
user := &usertypes.User{
ID: claims.Email,
}
groups := identity.DefaultGroups
if len(identityConfig.Spec.Groups) > 0 {
groups = identityConfig.Spec.Groups
}
roles := session.GetSessionRolesFromRBAC(claims.Groups, groups)
if len(roles) == 0 {
redirectURL = getRedirectOnErrorURL(redirectURL, "user must be a part of at least 1 group with roles")
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
return
}
issuedAt, expiresAt := time.Now(), time.Now().Add(SessionTimeout)
createdSession, err := store.GetStore().CreateSession(user, issuedAt, expiresAt, roles) // idToken.IssuedAt, idToken.Expiry
if err != nil {
logger.Error(errors.Wrap(err, "failed to create session"))
w.WriteHeader(http.StatusInternalServerError)
return
}
signedJWT, err := session.SignJWT(createdSession)
if err != nil {
logger.Error(errors.Wrap(err, "failed to sign jwt"))
w.WriteHeader(http.StatusInternalServerError)
return
}
responseToken := fmt.Sprintf("Bearer %s", signedJWT)
u, err := url.Parse(redirectURL)
if err != nil {
logger.Error(errors.Wrap(err, "failed to parse redirect url"))
w.WriteHeader(http.StatusInternalServerError)
return
}
expire := time.Now().Add(30 * time.Minute)
// cookie for identity service
identityServiceCookie := http.Cookie{
Name: "identity-service-login",
Value: "true",
Expires: expire,
Path: "/",
}
if u.Scheme == "https" {
identityServiceCookie.Secure = true
}
http.SetCookie(w, &identityServiceCookie)
signedTokenCookie, err := session.GetSessionCookie(responseToken, expire, redirectURL)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get session cookie"))
w.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(w, signedTokenCookie)
// session roles cookie
sessionRolesCookie := http.Cookie{
Name: "session_roles",
Value: strings.Join(createdSession.Roles, ","),
Expires: expire,
Path: "/",
}
if u.Scheme == "https" {
sessionRolesCookie.Secure = true
}
http.SetCookie(w, &sessionRolesCookie)
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
}
type GetLoginInfoResponse struct {
Method LoginMethod `json:"method"`
Error string `json:"error,omitempty"`
}
func (h *Handler) GetLoginInfo(w http.ResponseWriter, r *http.Request) {
getLoginInfoResponse := GetLoginInfoResponse{}
identityConfig, err := identity.GetConfig(r.Context(), util.PodNamespace)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get identity config"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if !identityConfig.Spec.Enabled || !identityConfig.Spec.DisablePasswordAuth {
getLoginInfoResponse.Method = PasswordAuth
JSON(w, http.StatusOK, getLoginInfoResponse)
return
}
getLoginInfoResponse.Method = IdentityService
JSON(w, http.StatusOK, getLoginInfoResponse)
}