-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added impersonate header #120
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -51,33 +52,66 @@ func sessionCallbacks(request *http.Request, writer http.ResponseWriter) *http.R | |
} | ||
|
||
func reqisterUser(request *http.Request, writer http.ResponseWriter, db *gorm.DB) *http.Request { | ||
if token, err := getToken(request); err == nil { | ||
device := &model.Device{} | ||
if db.Where("token = ?", token).Find(device).RecordNotFound() { | ||
log.Info().Str("token", token).Msg("no device with token found") | ||
token, err := getToken(request) | ||
if err != nil { | ||
msg := "Issue fetching token" | ||
log.Info().Msg(msg) | ||
http.Error(writer, msg, 400) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is problematic @jmattheis. I'm seeing output like this (on http://localhost:3030/):
Do you see the same issue on your end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, fixed in the last commit. |
||
return request | ||
} | ||
|
||
device := &model.Device{} | ||
if db.Where("token = ?", token).Find(device).RecordNotFound() { | ||
msg := "No device with token found" | ||
log.Info().Str("token", token).Msg(msg) | ||
http.Error(writer, msg, 400) | ||
return request | ||
} | ||
|
||
user := &model.User{} | ||
if db.Find(user, device.UserID).RecordNotFound() { | ||
log.Panic().Int("userID", device.UserID).Int("deviceID", device.ID).Msg("User not found") | ||
} | ||
log.Info().Int("userid", device.UserID).Str("username", user.Name).Msg("User found") | ||
|
||
impersonate := request.Header.Get("X-Traggo-Impersonate") | ||
if impersonate != "" { | ||
if !user.Admin { | ||
msg := "Trying to impersonate without being admin" | ||
log.Info().Str("impersonate", impersonate).Msg(msg) | ||
http.Error(writer, msg, 403) | ||
return request | ||
} | ||
|
||
user := &model.User{} | ||
if db.Find(user, device.UserID).RecordNotFound() { | ||
log.Panic().Int("userID", device.UserID).Int("deviceID", device.ID).Msg("User not found") | ||
userID, err := strconv.Atoi(impersonate) | ||
if err != nil { | ||
msg := "Unable to parse impersonation header" | ||
log.Info().Str("impersonate", impersonate).Msg(msg) | ||
http.Error(writer, msg, 400) | ||
return request | ||
} | ||
impersonateUser := &model.User{} | ||
if db.Find(impersonateUser, userID).RecordNotFound() { | ||
msg := "Impersonation user not found" | ||
log.Info().Int("userid", userID).Msg(msg) | ||
http.Error(writer, msg, 400) | ||
return request | ||
} | ||
user = impersonateUser | ||
log.Info().Str("username", user.Name).Msg("Impersonation user") | ||
} | ||
|
||
if device.ActiveAt.Before(time.Now().Add(5 * -time.Minute)) { | ||
log.Debug().Int("deviceId", device.ID).Str("deviceName", device.Name).Msg("update device activeAt") | ||
device.ActiveAt = timeNow() | ||
db.Save(device) | ||
if device.ActiveAt.Before(time.Now().Add(5 * -time.Minute)) { | ||
log.Debug().Int("deviceId", device.ID).Str("deviceName", device.Name).Msg("update device activeAt") | ||
device.ActiveAt = timeNow() | ||
db.Save(device) | ||
|
||
if cookie, err := request.Cookie(traggoSession); err == nil && cookie != nil { | ||
cookie.MaxAge = device.Type.Seconds() | ||
http.SetCookie(writer, cookie) | ||
} | ||
if cookie, err := request.Cookie(traggoSession); err == nil && cookie != nil { | ||
cookie.MaxAge = device.Type.Seconds() | ||
http.SetCookie(writer, cookie) | ||
} | ||
|
||
return request.WithContext(WithUser(WithDevice(request.Context(), device), user)) | ||
} | ||
|
||
return request | ||
return request.WithContext(WithUser(WithDevice(request.Context(), device), user)) | ||
} | ||
|
||
func getToken(request *http.Request) (string, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, the code looks fine, could you add some tests in auth/middleware_test.go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some HTTP status-code returns and a few tests.