Skip to content
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

Merged
merged 1 commit into from
May 18, 2022
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
72 changes: 53 additions & 19 deletions auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Copy link
Member

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?

Copy link
Contributor Author

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.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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/):

Issue fetching token
<!doctype html><html lang="en"><head><meta charset="utf-8"/>...</head><body>...</body></html>

Do you see the same issue on your end?

Copy link
Member

Choose a reason for hiding this comment

The 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) {
Expand Down
124 changes: 124 additions & 0 deletions auth/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -196,6 +197,129 @@ func TestMiddleware_destroySession_destroysCookie(t *testing.T) {
assert.Equal(t, "traggo=; Max-Age=0", cookieHeader)
}

func TestMiddleware_impersonate_no_admin(t *testing.T) {
now := test.Time("2018-06-30T18:30:00Z")
timeDispose := fakeTime(now)
defer timeDispose()

db := test.InMemoryDB(t)
defer db.Close()
builder := db.User(1)
builder.NewDevice(2, "abc", "test")
spy := &requestSpy{}
recorder := httptest.NewRecorder()

request := httptest.NewRequest("GET", "/test?token=abc", nil)
request.Header.Set("X-Traggo-Impersonate", "empty")
Middleware(db.DB)(spy).ServeHTTP(recorder, request)

response := recorder.Result()
assert.Equal(t, 403, response.StatusCode)

bodyBytes, _ := io.ReadAll(response.Body)
bodyString := string(bodyBytes)
assert.Equal(t, "Trying to impersonate without being admin\n", bodyString)

ctx := spy.req.Context()
assert.Nil(t, GetUser(ctx))
assert.Nil(t, GetDevice(ctx))
}

func TestMiddleware_impersonate_invalid_personate_header(t *testing.T) {
now := test.Time("2018-06-30T18:30:00Z")
timeDispose := fakeTime(now)
defer timeDispose()

db := test.InMemoryDB(t)
defer db.Close()
builder := db.User(1)
user := builder.User
user.Admin = true
db.Save(user)
builder.NewDevice(2, "abc", "test")
spy := &requestSpy{}
recorder := httptest.NewRecorder()

request := httptest.NewRequest("GET", "/test?token=abc", nil)
request.Header.Set("X-Traggo-Impersonate", "invalid")
Middleware(db.DB)(spy).ServeHTTP(recorder, request)

response := recorder.Result()
assert.Equal(t, 400, response.StatusCode)

bodyBytes, _ := io.ReadAll(response.Body)
bodyString := string(bodyBytes)
assert.Equal(t, "Unable to parse impersonation header\n", bodyString)

ctx := spy.req.Context()
assert.Nil(t, GetUser(ctx))
assert.Nil(t, GetDevice(ctx))
}

func TestMiddleware_impersonate_non_existing_user(t *testing.T) {
now := test.Time("2018-06-30T18:30:00Z")
timeDispose := fakeTime(now)
defer timeDispose()

db := test.InMemoryDB(t)
defer db.Close()
builder := db.User(1)
user := builder.User
user.Admin = true
db.Save(user)
builder.NewDevice(2, "abc", "test")
spy := &requestSpy{}
recorder := httptest.NewRecorder()

request := httptest.NewRequest("GET", "/test?token=abc", nil)
request.Header.Set("X-Traggo-Impersonate", "42")
Middleware(db.DB)(spy).ServeHTTP(recorder, request)

response := recorder.Result()
assert.Equal(t, 400, response.StatusCode)

bodyBytes, _ := io.ReadAll(response.Body)
bodyString := string(bodyBytes)
assert.Equal(t, "Impersonation user not found\n", bodyString)

ctx := spy.req.Context()
assert.Nil(t, GetUser(ctx))
assert.Nil(t, GetDevice(ctx))
}

func TestMiddleware_impersonate_happy(t *testing.T) {
now := test.Time("2018-06-30T18:30:00Z")
timeDispose := fakeTime(now)
defer timeDispose()

db := test.InMemoryDB(t)
defer db.Close()
admin_builder := db.User(1)
admin_user := admin_builder.User
admin_user.Admin = true
db.Save(admin_user)
admin_device := admin_builder.NewDevice(2, "abc", "test")

builder := db.User(2)
user := builder.User

spy := &requestSpy{}
recorder := httptest.NewRecorder()

request := httptest.NewRequest("GET", "/test?token=abc", nil)
request.Header.Set("X-Traggo-Impersonate", "2")
Middleware(db.DB)(spy).ServeHTTP(recorder, request)

response := recorder.Result()
assert.Equal(t, 200, response.StatusCode)

ctx := spy.req.Context()
assert.Equal(t, &user, GetUser(ctx))

admin_device.ActiveAt = now
assert.Equal(t, &admin_device, GetDevice(ctx))
}

func TestGetCreateSession_panicsWhenMiddlewareWasNotExecuted(t *testing.T) {
assert.Panics(t, func() {
GetCreateSession(context.Background())
Expand Down