-
Notifications
You must be signed in to change notification settings - Fork 469
/
UserAuthService.go
522 lines (473 loc) · 15.5 KB
/
UserAuthService.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright (c) 2020 Devtron Labs
*
* 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 user
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"strings"
"time"
"github.com/argoproj/argo-cd/util/session"
"github.com/caarlos0/env"
"github.com/casbin/casbin"
"github.com/coreos/go-oidc"
"github.com/devtron-labs/devtron/api/bean"
session2 "github.com/devtron-labs/devtron/client/argocdServer/session"
"github.com/devtron-labs/devtron/internal/constants"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/auth"
"github.com/golang-jwt/jwt/v4"
"github.com/gorilla/sessions"
"go.uber.org/zap"
"golang.org/x/oauth2"
)
type UserAuthService interface {
HandleLogin(username string, password string) (string, error)
HandleDexCallback(w http.ResponseWriter, r *http.Request)
HandleRefresh(w http.ResponseWriter, r *http.Request)
CreateRole(roleData *bean.RoleData) (bool, error)
AuthVerification(r *http.Request) (bool, error)
}
type UserAuthServiceImpl struct {
sessionManager *session.SessionManager
userAuthRepository repository.UserAuthRepository
sessionClient session2.ServiceClient
logger *zap.SugaredLogger
userRepository repository.UserRepository
}
var (
cStore *sessions.CookieStore
dexOauthConfig *oauth2.Config
//googleOauthConfig *oauth2.Config
oauthStateString = randToken()
idTokenVerifier *oidc.IDTokenVerifier
jwtKey = randKey()
CookieExpirationTime int
JwtExpirationTime int
)
type User struct {
email string
groups []string
}
var Claims struct {
Email string `json:"email"`
Verified bool `json:"email_verified"`
Groups []string `json:"groups"`
Token string `json:"token"`
Roles []string `json:"roles"`
jwt.StandardClaims
}
type DexConfig struct {
RedirectURL string `env:"DEX_RURL" envDefault:"http://127.0.0.1:8080/callback"`
ClientID string `env:"DEX_CID" envDefault:"example-app"`
ClientSecret string `env:"DEX_SECRET" `
DexURL string `env:"DEX_URL" `
DexJwtKey string `env:"DEX_JWTKEY" `
CStoreKey string `env:"DEX_CSTOREKEY"`
CookieExpirationTime int `env:"CExpirationTime" envDefault:"600"`
JwtExpirationTime int `env:"JwtExpirationTime" envDefault:"120"`
}
type WebhookToken struct {
WebhookToken string `env:"WEBHOOK_TOKEN" envDefault:""`
}
func NewUserAuthServiceImpl(userAuthRepository repository.UserAuthRepository, sessionManager *session.SessionManager,
client session2.ServiceClient, logger *zap.SugaredLogger, userRepository repository.UserRepository,
) *UserAuthServiceImpl {
serviceImpl := &UserAuthServiceImpl{
userAuthRepository: userAuthRepository,
sessionManager: sessionManager,
sessionClient: client,
logger: logger,
userRepository: userRepository,
}
cStore = sessions.NewCookieStore(randKey())
return serviceImpl
}
func GetConfig() (*DexConfig, error) {
cfg := &DexConfig{}
err := env.Parse(cfg)
return cfg, err
}
func GetWebhookToken() (*WebhookToken, error) {
cfg := &WebhookToken{}
err := env.Parse(cfg)
return cfg, err
}
/* #nosec */
func randToken() string {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
util.GetLogger().Error(err)
}
return base64.StdEncoding.EncodeToString(b)
}
/* #nosec */
func randKey() []byte {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
util.GetLogger().Error(err)
}
return b
}
// authorize verifies a bearer token and pulls user information form the claims.
func authorize(ctx context.Context, bearerToken string) (*User, error) {
idToken, err := idTokenVerifier.Verify(ctx, bearerToken)
if err != nil {
return nil, fmt.Errorf("could not verify bearer token: %v", err)
}
if err := idToken.Claims(&Claims); err != nil {
return nil, fmt.Errorf("failed to parse claims: %v", err)
}
if !Claims.Verified {
return nil, fmt.Errorf("email (%q) in returned claims was not verified", Claims.Email)
}
return &User{Claims.Email, Claims.Groups}, nil
}
func (impl UserAuthServiceImpl) HandleRefresh(w http.ResponseWriter, r *http.Request) {
session, _ := cStore.Get(r, "JWT_TOKEN")
// Check if user is authenticated
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Redirect(w, r, dexOauthConfig.AuthCodeURL(oauthStateString), http.StatusFound)
} else {
jwtToken := session.Values["jwtToken"].(string)
claims := &Claims
// Parse the JWT string and store the result in `claims`.
// Note that we are passing the key in this method as well. This method will return an error
// if the token is invalid (if it has expired according to the expiry time we set on sign in),
// or if the signature does not match
tkn, err := jwt.ParseWithClaims(jwtToken, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
if !tkn.Valid {
session.Options = &sessions.Options{
MaxAge: -1,
}
writeResponse(http.StatusUnauthorized, "TOKEN EXPIRED", w, errors.New("token expired"))
return
}
if err != nil {
if err == jwt.ErrSignatureInvalid {
writeResponse(http.StatusUnauthorized, "SignatureInvalid", w, errors.New("SignatureInvalid"))
return
}
writeResponse(http.StatusBadRequest, "StatusBadRequest", w, errors.New("StatusBadRequest"))
return
}
bearerToken := claims.Token
user, err := authorize(context.Background(), bearerToken)
if err != nil {
fmt.Print("Exception :", err)
}
fmt.Print(user)
// We ensure that a new token is not issued until enough time has elapsed
// In this case, a new token will only be issued if the old token is within
// 30 seconds of expiry. Otherwise, return a bad request status
/*if time.Unix(claims.ExpiresAt, 0).Sub(time.Now()) > 30*time.Second {
w.WriteHeader(http.StatusBadRequest)
return
}*/
dbUser, err := impl.userRepository.FetchUserDetailByEmail(Claims.Email)
if err != nil {
impl.logger.Errorw("Exception while fetching user from db", "err", err)
}
if dbUser.Id > 0 {
// Do nothing, User already exist in our db. (unique check by email id)
} else {
// TODO - need to handle case
}
// Now, create a new token for the current use, with a renewed expiration time
expirationTime := time.Now().Add(time.Duration(JwtExpirationTime) * time.Second)
// Create the JWT claims, which includes the username and expiry time
claims.ExpiresAt = expirationTime.Unix()
claims.Roles = dbUser.Roles
// Declare the token with the algorithm used for signing, and the claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Create the JWT string
tokenString, err := token.SignedString(jwtKey)
if err != nil {
// If there is an error in creating the JWT return an internal server error
writeResponse(http.StatusInternalServerError, "StatusInternalServerError", w, errors.New("unauthorized"))
return
}
// Set some session values.
session.Values["jwtToken"] = tokenString
session.Values["authenticated"] = true
session.Options = &sessions.Options{
MaxAge: CookieExpirationTime,
}
// Save it before we write to the response/return from the handler.
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
}
}
func (impl UserAuthServiceImpl) HandleLogin(username string, password string) (string, error) {
return impl.sessionClient.Create(context.Background(), username, password)
}
func (impl UserAuthServiceImpl) HandleDexCallback(w http.ResponseWriter, r *http.Request) {
state := r.URL.Query().Get("state")
session, _ := cStore.Get(r, "JWT_TOKEN")
fmt.Print(state)
// Verify state.
oauth2Token, err := dexOauthConfig.Exchange(context.Background(), r.URL.Query().Get("code"))
if err != nil {
// handle error
}
// Extract the ID Token from OAuth2 token.
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
// handle missing token
}
// Parse and verify ID Token payload.
idToken, err := idTokenVerifier.Verify(context.Background(), rawIDToken)
if err != nil {
// handle error
}
if err := idToken.Claims(&Claims); err != nil {
// handle error
}
dbConnection := impl.userRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return
}
// Rollback tx on error.
defer tx.Rollback()
dbUser, err := impl.userRepository.FetchUserDetailByEmail(Claims.Email)
if err != nil {
impl.logger.Errorw("Exception while fetching user from db", "err", err)
}
if dbUser.Id > 0 {
// Do nothing, User already exist in our db. (unique check by email id)
} else {
//create new user in our db on d basis of info got from google api or hex. assign a basic role
model := &repository.UserModel{
EmailId: Claims.Email,
AccessToken: rawIDToken,
}
_, err := impl.userRepository.CreateUser(model, tx)
if err != nil {
log.Println(err)
}
dbUser, err = impl.userRepository.FetchUserDetailByEmail(Claims.Email)
}
err = tx.Commit()
if err != nil {
return
}
// Declare the expiration time of the token
// here, we have kept it as 5 minutes
expirationTime := time.Now().Add(time.Duration(JwtExpirationTime) * time.Second)
// Create the JWT claims, which includes the username and expiry time
claims := &Claims
claims.Email = dbUser.EmailId
claims.Verified = dbUser.Exist
claims.ExpiresAt = expirationTime.Unix()
claims.Token = rawIDToken
claims.Roles = dbUser.Roles
// Declare the token with the algorithm used for signing, and the claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Create the JWT string
tokenString, err := token.SignedString(jwtKey)
if err != nil {
// If there is an error in creating the JWT return an internal server error
w.WriteHeader(http.StatusInternalServerError)
return
}
// Set some session values.
session.Values["jwtToken"] = tokenString
session.Values["authenticated"] = true
session.Options = &sessions.Options{
MaxAge: CookieExpirationTime,
}
// Save it before we write to the response/return from the handler.
session.Save(r, w)
fmt.Print()
http.Redirect(w, r, "/", http.StatusFound)
}
// Authorizer is a middleware for authorization
func Authorizer(e *casbin.Enforcer, sessionManager *session.SessionManager) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
//var users []string
cookie, _ := r.Cookie("argocd.token")
token := ""
if cookie != nil {
token = cookie.Value
r.Header.Set("token", token)
}
if token == "" && cookie == nil {
token = r.Header.Get("token")
//if cookie == nil && len(token) != 0 {
// http.SetCookie(w, &http.Cookie{Name: "argocd.token", Value: token, Path: "/"})
//}
}
//users = append(users, "anonymous")
authEnabled := true
pass := false
config := auth.GetConfig()
authEnabled = config.AuthEnabled
if token != "" && authEnabled && !contains(r.URL.Path) {
_, err := sessionManager.VerifyToken(token)
if err != nil {
log.Printf("Error verifying token: %+v\n", err)
http.SetCookie(w, &http.Cookie{Name: "argocd.token", Value: token, Path: "/", MaxAge: -1})
writeResponse(http.StatusUnauthorized, "Unauthorized", w, err)
return
}
pass = true
//TODO - we also can set user info in session (to avoid fetch it for all create n active)
}
if pass {
next.ServeHTTP(w, r)
} else if contains(r.URL.Path) {
if r.URL.Path == "/app/ci-pipeline/github-webhook/trigger" {
apiKey := r.Header.Get("api-key")
t, err := GetWebhookToken()
if err != nil || t.WebhookToken == "" || t.WebhookToken != apiKey {
writeResponse(http.StatusUnauthorized, "UN-AUTHENTICATED", w, errors.New("unauthenticated"))
return
}
}
next.ServeHTTP(w, r)
} else if token == "" {
writeResponse(http.StatusUnauthorized, "UN-AUTHENTICATED", w, errors.New("unauthenticated"))
return
} else {
writeResponse(http.StatusForbidden, "FORBIDDEN", w, errors.New("unauthorized"))
return
}
}
return http.HandlerFunc(fn)
}
}
func contains(url string) bool {
urls := []string{
"/health",
"/metrics",
"/orchestrator/webhook/ci/gocd/artifact",
"/orchestrator/webhook/ext-ci/",
"/orchestrator/auth/login",
"/orchestrator/auth/callback",
"/orchestrator/api/v1/session",
"/orchestrator/app/ci-pipeline/github-webhook/trigger",
"/orchestrator/webhook/msg/nats",
"/orchestrator/devtron/auth/verify",
"/orchestrator/security/policy/verify/webhook",
"/orchestrator/sso/list",
"/",
}
for _, a := range urls {
if a == url {
return true
}
}
prefixUrls := []string{
"/orchestrator/webhook/ext-ci/",
"/orchestrator/api/vi/pod/exec/ws",
"/orchestrator/api/dex",
"/orchestrator/auth/callback",
"/orchestrator/auth/login",
"/dashboard",
"/orchestrator/webhook/git",
}
for _, a := range prefixUrls {
if strings.Contains(url, a) {
return true
}
}
return false
}
func writeResponse(status int, message string, w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
type Response struct {
Code int `json:"code,omitempty"`
Status string `json:"status,omitempty"`
Result interface{} `json:"result,omitempty"`
Errors []*util.ApiError `json:"errors,omitempty"`
}
response := Response{}
response.Code = status
response.Result = message
b, err := json.Marshal(response)
if err != nil {
b = []byte("OK")
util.GetLogger().Errorw("Unexpected error in apiError", "err", err)
}
_, err = w.Write(b)
if err != nil {
util.GetLogger().Errorw("error", "err", err)
}
}
func (impl UserAuthServiceImpl) CreateRole(roleData *bean.RoleData) (bool, error) {
roleModel := &repository.RoleModel{
Role: roleData.Role,
Team: roleData.Team,
EntityName: roleData.EntityName,
Environment: roleData.Environment,
Action: roleData.Action,
}
dbConnection := impl.userRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return false, err
}
// Rollback tx on error.
defer tx.Rollback()
roleModel, err = impl.userAuthRepository.CreateRole(roleModel, tx)
if err != nil || roleModel == nil {
return false, err
}
err = tx.Commit()
if err != nil {
return false, err
}
return true, nil
}
func (impl UserAuthServiceImpl) AuthVerification(r *http.Request) (bool, error) {
token := r.Header.Get("token")
if token == "" {
impl.logger.Infow("no token provided", "token", token)
err := &util.ApiError{
HttpStatusCode: http.StatusUnauthorized,
Code: constants.UserNoTokenProvided,
InternalMessage: "no token provided",
}
return false, err
}
_, err := impl.sessionManager.VerifyToken(token)
if err != nil {
impl.logger.Errorw("failed to verify token", "error", err)
err := &util.ApiError{
HttpStatusCode: http.StatusUnauthorized,
Code: constants.UserNoTokenProvided,
InternalMessage: "failed to verify token",
UserMessage: fmt.Sprintf("token verification failed while getting logged in user: %s", token),
}
return false, err
}
//TODO - extends for other purpose
return true, nil
}