summaryrefslogtreecommitdiff
path: root/kauth/backend_test.go
blob: fb18374691d0bc40d29334ea2cd1f5db55e436f0 (plain) (blame)
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

package kauth

import (
	"context"
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/martinlehoux/kagamigo/kcore"
	"github.com/stretchr/testify/assert"
)

// Test structures
type testUser struct {
	id       kcore.ID
	password string
}

func (u testUser) ID() kcore.ID {
	return u.id
}

type store struct {
	users map[string]testUser
}

func (s *store) Authenticate(ctx context.Context, username, password string) (testUser, bool) {
	if u, ok := s.users[username]; ok {
		if password == u.password {
			return u, true
		}
	}
	return testUser{}, false
}

func (s *store) LoadUser(ctx context.Context, id kcore.ID) (testUser, error) {
	for _, u := range s.users {
		if u.id == id {
			return u, nil
		}
	}
	return testUser{}, errors.New("user not found")
}

func newUser(password string) testUser {
	return testUser{id: kcore.NewID(), password: password}
}

func newBackend() (*Backend[testUser], *store) {
	s := store{users: map[string]testUser{}}
	return &Backend[testUser]{
		UserStore:    &s,
		Domain:       "localhost",
		CookieSecret: GenerateCookieSecret(),
		Now:          time.Now,
	}, &s
}

// Helper to extract authentication cookie value from a response recorder
func getAuthCookie(rr *httptest.ResponseRecorder) *http.Cookie {
	for _, c := range rr.Result().Cookies() {
		if c.Name == "authentication" {
			return c
		}
	}
	return nil
}

// Test Backend.Login sets cookie on success
func TestBackend_Login_Success(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user

	rr := httptest.NewRecorder()
	ctx := context.Background()
	_, err := backend.Login(rr, ctx, "user", "pass")
	assert.NoError(t, err)

	c := getAuthCookie(rr)
	assert.NotNil(t, c, "authentication cookie should be set")
	assert.Equal(t, backend.Domain, c.Domain)
	assert.Equal(t, "/", c.Path)
	assert.True(t, time.Until(c.Expires) >= 23*time.Hour && time.Until(c.Expires) <= 25*time.Hour, "expected expires about 24h from now")
}

// Test Backend.Login returns error on invalid credentials
func TestBackend_Login_InvalidCredentials(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user

	rr := httptest.NewRecorder()
	ctx := context.Background()
	_, err := backend.Login(rr, ctx, "baduser", "badpass")
	assert.Equal(t, ErrInvalidCredentials, err)

	c := getAuthCookie(rr)
	assert.Nil(t, c, "did not expect authentication cookie to be set")
}

// Test Backend.Logout deletes the authentication cookie
func TestBackend_Logout(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user

	rr := httptest.NewRecorder()
	backend.Logout(rr)

	c := getAuthCookie(rr)
	assert.NotNil(t, c, "authentication cookie should be set for deletion")
	assert.Equal(t, "", c.Value)
	assert.Equal(t, "/", c.Path)
	assert.Equal(t, backend.Domain, c.Domain)
	assert.Equal(t, int(-1), c.MaxAge)
	assert.True(t, c.Expires.Equal(time.Unix(0, 0)), "Expires should be Unix epoch")
	assert.True(t, c.HttpOnly)
	assert.True(t, c.Secure)
}

func TestCookieAuthMiddleware_ValidCookie_SetsUserInContext(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user

	rrLogin := httptest.NewRecorder()
	ctx := context.Background()
	_, _ = backend.Login(rrLogin, ctx, "user", "pass")
	cookie := getAuthCookie(rrLogin)
	req := httptest.NewRequest("GET", "/", nil)
	req.AddCookie(cookie)

	var userInHandler testUser
	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		u, ok := backend.User(r.Context())
		assert.True(t, ok, "user should be in context")
		userInHandler = u
		w.WriteHeader(http.StatusOK)
	}))

	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.Equal(t, user.id, userInHandler.id)
	assert.Equal(t, http.StatusOK, rr.Code)
}

func TestCookieAuthMiddleware_NoCookie_PassesThrough(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user
	req := httptest.NewRequest("GET", "/", nil)

	called := false
	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		called = true
		w.WriteHeader(http.StatusOK)
	}))
	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.True(t, called, "handler should be called")
	assert.Equal(t, http.StatusOK, rr.Code)
}

func TestCookieAuthMiddleware_InvalidCookie_DecryptionFails(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user
	req := httptest.NewRequest("GET", "/", nil)
	req.AddCookie(&http.Cookie{Name: "authentication", Value: "not-encrypted"})

	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t.Error("handler should not be called on decryption failure")
	}))
	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.Equal(t, http.StatusBadRequest, rr.Code)
}

func TestCookieAuthMiddleware_BadFormatCookie(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user
	// Encrypt a string that doesn't have the expected "id:expires" format
	badValue := encrypt(backend.CookieSecret, "badformat")
	req := httptest.NewRequest("GET", "/", nil)
	req.AddCookie(&http.Cookie{Name: "authentication", Value: badValue})

	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t.Error("handler should not be called on bad format")
	}))
	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.Equal(t, http.StatusBadRequest, rr.Code)
}

func Must[T any](val T, err error) T {
	if err != nil {
		panic(err)
	}
	return val
}

func TestCookieAuthMiddleware_ExpiredCookie(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user

	backend.Now = func() time.Time { return Must(time.Parse(time.RFC3339, "2025-12-23T12:00:00Z")) }
	rrLogin := httptest.NewRecorder()
	ctx := context.Background()
	_, _ = backend.Login(rrLogin, ctx, "user", "pass")
	cookie := getAuthCookie(rrLogin)
	assert.NotNil(t, cookie, "authentication cookie should be set")
	req := httptest.NewRequest("GET", "/", nil)
	req.AddCookie(cookie)

	var userSet bool
	backend.Now = func() time.Time { return Must(time.Parse(time.RFC3339, "2025-12-24T13:00:00Z")) } // after expiry
	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, ok := backend.User(r.Context())
		userSet = ok
		w.WriteHeader(http.StatusUnauthorized)
	}))
	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.False(t, userSet, "user should not be set in context for expired cookie")
	assert.Equal(t, http.StatusUnauthorized, rr.Code)
}

func TestCookieAuthMiddleware_InvalidUserID(t *testing.T) {
	user := newUser("pass")
	backend, store := newBackend()
	store.users["user"] = user
	rrLogin := httptest.NewRecorder()
	ctx := context.Background()
	_, _ = backend.Login(rrLogin, ctx, "user", "pass")
	cookie := getAuthCookie(rrLogin)

	req := httptest.NewRequest("GET", "/", nil)
	req.AddCookie(cookie)
	delete(store.users, "user") // User is no longer valid

	handler := backend.CookieAuthMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		t.Error("handler should not be called on invalid user id")
	}))
	rr := httptest.NewRecorder()
	handler.ServeHTTP(rr, req)
	assert.Equal(t, http.StatusInternalServerError, rr.Code)
}