-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
login_test.go
455 lines (367 loc) · 19.4 KB
/
login_test.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
package password_test
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"
"github.com/ory/x/assertx"
"github.com/ory/x/errorsx"
"github.com/ory/x/sqlxx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/ory/x/pointerx"
"github.com/ory/viper"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/strategy/password"
"github.com/ory/kratos/text"
"github.com/ory/kratos/x"
)
func TestCompleteLogin(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
viper.Set(configuration.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword),
map[string]interface{}{"enabled": true})
publicTS, _ := testhelpers.NewKratosServer(t, reg)
errTS := testhelpers.NewErrorTestServer(t, reg)
uiTS := testhelpers.NewLoginUIFlowEchoServer(t, reg)
redirTS := newReturnTs(t, reg)
// Overwrite these two:
viper.Set(configuration.ViperKeySelfServiceErrorUI, errTS.URL+"/error-ts")
viper.Set(configuration.ViperKeySelfServiceLoginUI, uiTS.URL+"/login-ts")
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/login.schema.json")
viper.Set(configuration.ViperKeySecretsDefault, []string{"not-a-secure-session-key"})
ensureFieldsExist := func(t *testing.T, body []byte) {
checkFormContent(t, body, "identifier",
"password",
"csrf_token")
}
createIdentity := func(identifier, password string) {
p, _ := reg.Hasher().Generate([]byte(password))
require.NoError(t, reg.PrivilegedIdentityPool().CreateIdentity(context.Background(), &identity.Identity{
ID: x.NewUUID(),
Traits: identity.Traits(fmt.Sprintf(`{"subject":"%s"}`, identifier)),
Credentials: map[identity.CredentialsType]identity.Credentials{
identity.CredentialsTypePassword: {
Type: identity.CredentialsTypePassword,
Identifiers: []string{identifier},
Config: sqlxx.JSONRawMessage(`{"hashed_password":"` + string(p) + `"}`),
},
},
}))
}
apiClient := testhelpers.NewDebugClient(t)
t.Run("case=should show the error ui because the request payload is malformed", func(t *testing.T) {
t.Run("type=api", func(t *testing.T) {
f := testhelpers.InitializeLoginFlowViaAPI(t, apiClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
body, res := testhelpers.LoginMakeRequest(t, true, c, apiClient, "14=)=!(%)$/ZP()GHIÖ")
assert.Contains(t, res.Request.URL.String(), publicTS.URL+password.RouteLogin)
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, body, `cannot unmarshal number into`)
})
t.Run("type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
body, res := testhelpers.LoginMakeRequest(t, false, c, browserClient, "14=)=!(%)$/ZP()GHIÖ")
assert.Contains(t, res.Request.URL.String(), uiTS.URL+"/login-ts")
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.messages.0.text").String(), "invalid URL escape", "%s", body)
})
})
t.Run("should return an error because the request does not exist", func(t *testing.T) {
var check = func(t *testing.T, actual string) {
assert.Equal(t, int64(http.StatusNotFound), gjson.Get(actual, "code").Int(), "%s", actual)
assert.Equal(t, "Not Found", gjson.Get(actual, "status").String(), "%s", actual)
assert.Contains(t, gjson.Get(actual, "message").String(), "Unable to locate the resource", "%s", actual)
}
fakeFlow := &models.LoginFlowMethodConfig{
Action: pointerx.String(publicTS.URL + password.RouteLogin + "?flow=" + x.NewUUID().String())}
t.Run("type=api", func(t *testing.T) {
actual, res := testhelpers.LoginMakeRequest(t, true, fakeFlow, apiClient, "{}")
assert.Contains(t, res.Request.URL.String(), publicTS.URL+password.RouteLogin)
check(t, gjson.Get(actual, "error").Raw)
})
t.Run("type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
actual, res := testhelpers.LoginMakeRequest(t, false, fakeFlow, browserClient, "")
assert.Contains(t, res.Request.URL.String(), errTS.URL)
check(t, gjson.Get(actual, "0").Raw)
})
})
t.Run("case=should return an error because the request is expired", func(t *testing.T) {
viper.Set(configuration.ViperKeySelfServiceLoginRequestLifespan, "10ms")
defer viper.Set(configuration.ViperKeySelfServiceLoginRequestLifespan, "10m")
values := url.Values{
"csrf_token": {x.FakeCSRFToken},
"identifier": {"identifier"},
"password": {"password"},
}
t.Run("type=api", func(t *testing.T) {
f := testhelpers.InitializeLoginFlowViaAPI(t, apiClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
time.Sleep(time.Millisecond * 20)
actual, res := testhelpers.LoginMakeRequest(t, true, c, apiClient, testhelpers.EncodeFormAsJSON(t, true, values))
assert.Contains(t, res.Request.URL.String(), publicTS.URL+login.RouteGetFlow)
assert.NotEqual(t, f.Payload.ID, gjson.Get(actual, "id").String(), "%s", actual)
assert.Contains(t, gjson.Get(actual, "messages.0.text").String(), "expired", "%s", actual)
})
t.Run("type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
time.Sleep(time.Millisecond * 20)
actual, res := testhelpers.LoginMakeRequest(t, false, c, browserClient, values.Encode())
assert.Contains(t, res.Request.URL.String(), uiTS.URL+"/login-ts")
assert.NotEqual(t, f.Payload.ID, gjson.Get(actual, "id").String(), "%s", actual)
assert.Contains(t, gjson.Get(actual, "messages.0.text").String(), "expired", "%s", actual)
})
})
t.Run("case=should have correct CSRF behavior", func(t *testing.T) {
var values = url.Values{
"csrf_token": {"invalid_token"},
"identifier": {"login-identifier-csrf-browser"},
"password": {x.NewUUID().String()},
}
t.Run("case=should fail because of missing CSRF token/type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
actual, res := testhelpers.LoginMakeRequest(t, false, c, browserClient, values.Encode())
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assertx.EqualAsJSON(t, x.ErrInvalidCSRFToken,
json.RawMessage(gjson.Get(actual, "0").Raw), "%s", actual)
})
t.Run("case=should pass even without CSRF token/type=api", func(t *testing.T) {
f := testhelpers.InitializeLoginFlowViaAPI(t, apiClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
actual, res := testhelpers.LoginMakeRequest(t, true, c, apiClient, testhelpers.EncodeFormAsJSON(t, true, values))
assert.EqualValues(t, http.StatusBadRequest, res.StatusCode)
assert.Contains(t, actual, "provided credentials are invalid")
})
})
var expectValidationError = func(t *testing.T, isAPI, forced bool, values func(url.Values)) string {
return testhelpers.SubmitLoginForm(t, isAPI, nil, publicTS, values,
identity.CredentialsTypePassword, forced,
testhelpers.ExpectStatusCode(isAPI, http.StatusBadRequest, http.StatusOK),
testhelpers.ExpectURL(isAPI, publicTS.URL+password.RouteLogin, conf.SelfServiceFlowLoginUI().String()))
}
t.Run("should return an error because the credentials are invalid (user does not exist)", func(t *testing.T) {
var check = func(t *testing.T, body string) {
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.action").String(), publicTS.URL+password.RouteLogin, "%s", body)
assert.Equal(t, text.NewErrorValidationInvalidCredentials().Text, gjson.Get(body, "methods.password.config.messages.0.text").String())
}
var values = func(v url.Values) {
v.Set("identifier", "identifier")
v.Set("password", "password")
}
t.Run("type=browser", func(t *testing.T) {
check(t, expectValidationError(t, false, false, values))
})
t.Run("type=api", func(t *testing.T) {
check(t, expectValidationError(t, true, false, values))
})
})
t.Run("should return an error because no identifier is set", func(t *testing.T) {
var check = func(t *testing.T, body string) {
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.action").String(), publicTS.URL+password.RouteLogin, "%s", body)
ensureFieldsExist(t, []byte(body))
assert.Equal(t, "Property identifier is missing.", gjson.Get(body, "methods.password.config.fields.#(name==identifier).messages.0.text").String(), "%s", body)
// The password value should not be returned!
assert.Empty(t, gjson.Get(body, "methods.password.config.fields.#(name==password).value").String())
}
var values = func(v url.Values) {
v.Del("identifier")
v.Set("password", "password")
}
t.Run("type=browser", func(t *testing.T) {
check(t, expectValidationError(t, false, false, values))
})
t.Run("type=api", func(t *testing.T) {
check(t, expectValidationError(t, true, false, values))
})
})
t.Run("should return an error because no password is set", func(t *testing.T) {
var check = func(t *testing.T, body string) {
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.action").String(), publicTS.URL+password.RouteLogin, "%s", body)
ensureFieldsExist(t, []byte(body))
assert.Equal(t, "Property password is missing.", gjson.Get(body, "methods.password.config.fields.#(name==password).messages.0.text").String(), "%s", body)
assert.Equal(t, "identifier", gjson.Get(body, "methods.password.config.fields.#(name==identifier).value").String(), "%s", body)
// This must not include the password!
assert.Empty(t, gjson.Get(body, "methods.password.config.fields.#(name==password).value").String())
}
var values = func(v url.Values) {
v.Set("identifier", "identifier")
v.Del("password")
}
t.Run("type=browser", func(t *testing.T) {
check(t, expectValidationError(t, false, false, values))
})
t.Run("type=api", func(t *testing.T) {
check(t, expectValidationError(t, true, false, values))
})
})
t.Run("should return an error because the credentials are invalid (password not correct)", func(t *testing.T) {
var check = func(t *testing.T, body string) {
assert.NotEmpty(t, gjson.Get(body, "id").String(), "%s", body)
assert.Contains(t, gjson.Get(body, "methods.password.config.action").String(), publicTS.URL+password.RouteLogin, "%s", body)
ensureFieldsExist(t, []byte(body))
assert.Equal(t,
errorsx.Cause(schema.NewInvalidCredentialsError()).(*schema.ValidationError).Messages[0].Text,
gjson.Get(body, "methods.password.config.messages.0.text").String(),
"%s", body,
)
// This must not include the password!
assert.Empty(t, gjson.Get(body, "methods.password.config.fields.#(name==password).value").String())
}
identifier, pwd := x.NewUUID().String(), "password"
createIdentity(identifier, pwd)
var values = func(v url.Values) {
v.Set("identifier", identifier)
v.Set("password", "not-password")
}
t.Run("type=browser", func(t *testing.T) {
check(t, expectValidationError(t, false, false, values))
})
t.Run("type=api", func(t *testing.T) {
check(t, expectValidationError(t, true, false, values))
})
})
t.Run("should pass with real request", func(t *testing.T) {
identifier, pwd := x.NewUUID().String(), "password"
createIdentity(identifier, pwd)
var values = func(v url.Values) {
v.Set("identifier", identifier)
v.Set("password", pwd)
}
t.Run("type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
body := testhelpers.SubmitLoginForm(t, false, browserClient, publicTS, values,
identity.CredentialsTypePassword, false, http.StatusOK, redirTS.URL)
assert.Equal(t, identifier, gjson.Get(body, "identity.traits.subject").String(), "%s", body)
t.Run("retry with different refresh", func(t *testing.T) {
t.Run("redirect to returnTS if refresh is missing", func(t *testing.T) {
res, err := browserClient.Get(publicTS.URL + login.RouteInitBrowserFlow)
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, res.StatusCode)
})
t.Run("show UI and hint at username", func(t *testing.T) {
res, err := browserClient.Get(publicTS.URL + login.RouteInitBrowserFlow + "?refresh=true")
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, res.StatusCode)
rid := res.Request.URL.Query().Get("flow")
assert.NotEmpty(t, rid, "%s", res.Request.URL)
res, err = browserClient.Get(publicTS.URL + login.RouteGetFlow + "?id=" + rid)
require.NoError(t, err)
require.EqualValues(t, http.StatusOK, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
assert.True(t, gjson.GetBytes(body, "forced").Bool())
assert.Equal(t, identifier, gjson.GetBytes(body, "methods.password.config.fields.#(name==identifier).value").String(), "%s", body)
assert.Empty(t, gjson.GetBytes(body, "methods.password.config.fields.#(name==password).value").String(), "%s", body)
})
})
})
t.Run("type=api", func(t *testing.T) {
body := testhelpers.SubmitLoginForm(t, true, nil, publicTS, values,
identity.CredentialsTypePassword, false, http.StatusOK, publicTS.URL+password.RouteLogin)
assert.Equal(t, identifier, gjson.Get(body, "session.identity.traits.subject").String(), "%s", body)
st := gjson.Get(body, "session_token").String()
assert.NotEmpty(t, st, "%s", body)
t.Run("retry with different refresh", func(t *testing.T) {
c := &http.Client{Transport: x.NewTransportWithHeader(http.Header{"Authorization": {"Bearer " + st}})}
t.Run("redirect to returnTS if refresh is missing", func(t *testing.T) {
res, err := c.Do(testhelpers.NewHTTPGetJSONRequest(t, publicTS.URL+login.RouteInitAPIFlow))
require.NoError(t, err)
defer res.Body.Close()
body := x.MustReadAll(res.Body)
require.EqualValues(t, http.StatusBadRequest, res.StatusCode)
assertx.EqualAsJSON(t, login.ErrAlreadyLoggedIn, json.RawMessage(gjson.GetBytes(body, "error").Raw), "%s", body)
})
t.Run("show UI and hint at username", func(t *testing.T) {
res, err := c.Do(testhelpers.NewHTTPGetJSONRequest(t, publicTS.URL+login.RouteInitAPIFlow+"?refresh=true"))
require.NoError(t, err)
defer res.Body.Close()
body := x.MustReadAll(res.Body)
assert.True(t, gjson.GetBytes(body, "forced").Bool())
assert.Equal(t, identifier, gjson.GetBytes(body, "methods.password.config.fields.#(name==identifier).value").String(), "%s", body)
assert.Empty(t, gjson.GetBytes(body, "methods.password.config.fields.#(name==password).value").String(), "%s", body)
})
})
})
})
t.Run("case=should return an error because not passing validation and reset previous errors and values", func(t *testing.T) {
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stub/login.schema.json")
var check = func(t *testing.T, actual string) {
assert.NotEmpty(t, gjson.Get(actual, "id").String(), "%s", actual)
assert.Contains(t, gjson.Get(actual, "methods.password.config.action").String(), publicTS.URL+password.RouteLogin, "%s", actual)
}
var checkFirst = func(t *testing.T, actual string) {
check(t, actual)
assert.Contains(t, gjson.Get(actual, "methods.password.config.fields.#(name==identifier).messages.0").String(), "Property identifier is missing.", "%s", actual)
}
var checkSecond = func(t *testing.T, actual string) {
check(t, actual)
assert.Empty(t, gjson.Get(actual, "methods.password.config.fields.#(name==identifier).error"))
assert.EqualValues(t, "identifier", gjson.Get(actual, "methods.password.config.fields.#(name==identifier).value").String())
assert.Empty(t, gjson.Get(actual, "methods.password.config.error"))
assert.Contains(t, gjson.Get(actual, "methods.password.config.fields.#(name==password).messages.0").String(), "Property password is missing.", "%s", actual)
}
var valuesFirst = func(v url.Values) url.Values {
v.Del("identifier")
v.Set("password", x.NewUUID().String())
return v
}
var valuesSecond = func(v url.Values) url.Values {
v.Set("identifier", "identifier")
v.Del("password")
return v
}
t.Run("type=api", func(t *testing.T) {
f := testhelpers.InitializeLoginFlowViaAPI(t, apiClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
actual, _ := testhelpers.LoginMakeRequest(t, true, c, apiClient, testhelpers.EncodeFormAsJSON(t, true, valuesFirst(testhelpers.SDKFormFieldsToURLValues(c.Fields))))
checkFirst(t, actual)
actual, _ = testhelpers.LoginMakeRequest(t, true, c, apiClient, testhelpers.EncodeFormAsJSON(t, true, valuesSecond(testhelpers.SDKFormFieldsToURLValues(c.Fields))))
checkSecond(t, actual)
})
t.Run("type=browser", func(t *testing.T) {
browserClient := testhelpers.NewClientWithCookies(t)
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
actual, _ := testhelpers.LoginMakeRequest(t, false, c, browserClient, valuesFirst(testhelpers.SDKFormFieldsToURLValues(c.Fields)).Encode())
checkFirst(t, actual)
actual, _ = testhelpers.LoginMakeRequest(t, false, c, browserClient, valuesSecond(testhelpers.SDKFormFieldsToURLValues(c.Fields)).Encode())
checkSecond(t, actual)
})
})
t.Run("should be a new session with forced flag", func(t *testing.T) {
identifier, pwd := x.NewUUID().String(), "password"
createIdentity(identifier, pwd)
browserClient := testhelpers.NewClientWithCookies(t)
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, false)
c := testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
values := url.Values{"identifier": {identifier}, "password": {pwd}, "csrf_token": {x.FakeCSRFToken}}.Encode()
body1, res := testhelpers.LoginMakeRequest(t, false, c, browserClient, values)
assert.EqualValues(t, http.StatusOK, res.StatusCode)
f = testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, publicTS, true)
c = testhelpers.GetLoginFlowMethodConfig(t, f.Payload, identity.CredentialsTypePassword.String())
body2, res := testhelpers.LoginMakeRequest(t, false, c, browserClient, values)
require.Contains(t, res.Request.URL.Path, "return-ts", "%s", res.Request.URL.String())
assert.Equal(t, identifier, gjson.Get(body2, "identity.traits.subject").String(), "%s", body2)
assert.NotEqual(t, gjson.Get(body1, "id").String(), gjson.Get(body2, "id").String(), "%s\n\n%s\n", body1, body2)
})
}