Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/user/UserAuthHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (handler UserAuthHandlerImpl) LoginHandler(w http.ResponseWriter, r *http.R
}
//token, err := handler.loginService.CreateLoginSession(up.Username, up.Password)
clientIp := util.GetClientIP(r)
token, err := handler.userAuthService.HandleLoginWithClientIp(up.Username, up.Password, clientIp)
token, err := handler.userAuthService.HandleLoginWithClientIp(r.Context(),up.Username, up.Password, clientIp)
if err != nil {
common.WriteJsonResp(w, fmt.Errorf("invalid username or password"), nil, http.StatusForbidden)
return
Expand Down
6 changes: 3 additions & 3 deletions pkg/user/UserAuthService.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
)

type UserAuthService interface {
HandleLoginWithClientIp(username, password, clientIp string) (string, error)
HandleLoginWithClientIp(ctx context.Context, username, password, clientIp string) (string, error)
HandleLogin(username string, password string) (string, error)
HandleDexCallback(w http.ResponseWriter, r *http.Request)
HandleRefresh(w http.ResponseWriter, r *http.Request)
Expand Down Expand Up @@ -255,10 +255,10 @@ func (impl UserAuthServiceImpl) HandleRefresh(w http.ResponseWriter, r *http.Req
}
}

func (impl UserAuthServiceImpl) HandleLoginWithClientIp(username, password, clientIp string) (string, error) {
func (impl UserAuthServiceImpl) HandleLoginWithClientIp(ctx context.Context, username, password, clientIp string) (string, error) {
token, err := impl.HandleLogin(username, password)
if err == nil {
id, _, err := impl.userService.GetUserByToken(token)
id, _, err := impl.userService.GetUserByToken(ctx, token)
if err != nil {
impl.logger.Infow("error occured while getting user by token", "err", err)
} else {
Expand Down
13 changes: 9 additions & 4 deletions pkg/user/UserService.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package user

import (
"context"
"fmt"
"github.com/devtron-labs/authenticator/jwt"
"github.com/devtron-labs/authenticator/middleware"
Expand All @@ -30,6 +31,7 @@ import (
util2 "github.com/devtron-labs/devtron/util"
"github.com/go-pg/pg"
"github.com/gorilla/sessions"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"net/http"
"strings"
Expand All @@ -49,7 +51,7 @@ type UserService interface {
DeleteUser(userInfo *bean.UserInfo) (bool, error)
CheckUserRoles(id int32) ([]string, error)
SyncOrchestratorToCasbin() (bool, error)
GetUserByToken(token string) (int32, string, error)
GetUserByToken(context context.Context, token string) (int32, string, error)
IsSuperAdmin(userId int) (bool, error)
GetByIdIncludeDeleted(id int32) (*bean.UserInfo, error)
UserExists(emailId string) bool
Expand Down Expand Up @@ -1160,26 +1162,29 @@ func (impl UserServiceImpl) GetUserByEmail(emailId string) (*bean.UserInfo, erro
return response, nil
}
func (impl UserServiceImpl) GetLoggedInUser(r *http.Request) (int32, error) {
_, span := otel.Tracer("userService").Start(r.Context(), "GetLoggedInUser")
defer span.End()
token := ""
if strings.Contains(r.URL.Path, "/orchestrator/webhook/ext-ci/") {
token = r.Header.Get("api-token")
} else {
token = r.Header.Get("token")
}
userId, userType, err := impl.GetUserByToken(token)
userId, userType, err := impl.GetUserByToken(r.Context(), token)
// if user is of api-token type, then update lastUsedBy and lastUsedAt
if err == nil && userType == bean.USER_TYPE_API_TOKEN {
go impl.saveUserAudit(r, userId)
}
return userId, err
}

func (impl UserServiceImpl) GetUserByToken(token string) (int32, string, error) {
func (impl UserServiceImpl) GetUserByToken(context context.Context, token string) (int32, string, error) {
_, span := otel.Tracer("userService").Start(context, "GetUserByToken")
email, err := impl.GetEmailFromToken(token)
span.End()
if err != nil {
return http.StatusUnauthorized, "", err
}

userInfo, err := impl.GetUserByEmail(email)
if err != nil {
impl.logger.Errorw("unable to fetch user from db", "error", err)
Expand Down
5 changes: 2 additions & 3 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.