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
9 changes: 9 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ The login screen is **identifier-first** by default (username → password
prompt, or a 302 to the IdP when the identifier's email domain matches an OIDC
realm's `login_domains`); set the `login_mode` setting to `combined` (Admin →
Settings) for a classic combined form with per-realm OIDC buttons.
`/auth/identify` routes by realm configuration alone, so a local account
whose domain is a login domain is sent to the IdP like any other name. While
a password realm is enabled (`password` in `/auth/login-options`), the
identifier-first form therefore offers **Sign in with a password instead**:
the password step for the entered username, without asking `/auth/identify`.
The notice of a refused or failed OIDC sign-in (`/login?error=not_permitted`
or `oidc_failed`) offers the same, as a username and password form. Both are
shown for every username, so neither reveals whether an account exists, and
`/auth/identify` stays as it is (`web/src/routes/Login.tsx`).

### Endpoints

Expand Down
8 changes: 8 additions & 0 deletions docs/changes/2.2.0/531.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
With the identifier-first login screen, an account with a password whose
username is in an OIDC realm's login domain could not sign in from the
browser: the screen sent it to the identity provider and never asked for
the password. That locked out an administrator who added a realm with their
own domain. While a password realm is enabled, the screen now offers **Sign
in with a password instead**, for every username alike, and so does the
notice of a sign-in the identity provider refused or failed. The routing by
login domain and the combined screen are unchanged.
2 changes: 1 addition & 1 deletion docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ wrong.
| Setting | Default | Meaning |
| --- | --- | --- |
| `anonymous_mode` | `off` | `off` or `all`: who is served without credentials. A repository is read anonymously only when it also allows it (**Allow anonymous read**, or a grant of the `anonymous` role). Under `all`, signed-in users also read what anonymous callers may. To let a network read without credentials, have the load balancer name a service account through a [trusted-header realm](#trusted-header-realms). |
| `login_mode` | `identifier_first` | The login screen: the username first (then a password prompt or the identity provider whose login domains match), or `combined` (username and password, plus the OIDC button). |
| `login_mode` | `identifier_first` | The login screen: the username first (then a password prompt or the identity provider whose login domains match), or `combined` (username and password, plus the OIDC button). While a password realm is enabled, the username-first screen also offers **Sign in with a password instead**, which asks for the password of any username, also one in a login domain; the notice of a sign-in the identity provider refused or failed offers the same. It is how an account with a password signs in when its domain goes to an identity provider that is down or not linked to it. It does not enforce anything either way: `POST /auth/login` takes a password realm's passwords whatever the screen shows. |
| `remember_accounts` | `true` | The login screen offers the accounts signed in on the device before, so signing in again is one click (see below). `false` for kiosks and shared computers. |
| `session_idle_ttl_seconds` | `28800` (8 h) | A browser session ends this long after its last use. |
| `session_absolute_ttl_seconds` | `604800` (7 d) | A browser session ends this long after sign-in. New lifetimes apply to sessions signed in afterwards; an open session's idle window moves to the new idle timeout on its next use. |
Expand Down
226 changes: 225 additions & 1 deletion web/src/routes/Login.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { cleanup, render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { QueryClientProvider } from '@tanstack/react-query'
import {
Expand Down Expand Up @@ -31,6 +31,10 @@ vi.mock('~/lib/navigation', () => ({ assignLocation: vi.fn() }))
* - a 401 from /auth/login shows the uniform inline error;
* - a failed OIDC callback (`?error=<code>`) shows a generic message per
* code, never the raw parameter, with a "Try again" link that clears it;
* - "Sign in with a password instead" reaches the password prompt without
* `/auth/identify` (#531), from the identifier-first form and from the
* notice of a refused or failed OIDC sign-in, for every username alike and
* only while a password realm is enabled;
* - there is **no** registry-token sign-in: the UI always authenticates with
* username+password or via a realm;
* - accounts signed in on this device are remembered and offered as tiles
Expand Down Expand Up @@ -266,6 +270,226 @@ describe('Login', () => {
})
})

describe('sign in with a password instead (#531)', () => {
// An OIDC realm whose login domain takes every example.com username.
const toCorp = () =>
json({
mode: 'oidc_redirect',
realm: 'corp',
login_url: '/auth/oidc/login?realm=corp',
})
const session = (username: string) =>
json({ user_id: 1, username, is_admin: true, csrf_token: 'csrf-pw' })

it('signs in an account of an OIDC login domain with its password, without /auth/identify', async () => {
const user = userEvent.setup()
const identify = vi.fn(toCorp)
let loggedIn: unknown = null
mockAuthApi({
options: identifierFirst,
onIdentify: identify,
onLogin: (body) => {
loggedIn = body
return session('admin@example.com')
},
})

const router = await renderLogin()

await user.type(await screen.findByLabelText('Username'), 'admin@example.com')
await user.click(
screen.getByRole('button', { name: 'Sign in with a password instead' }),
)

// The password step of the entered username, as after /auth/identify.
expect(await screen.findByText('admin@example.com')).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Change' })).toBeInTheDocument()
await user.type(screen.getByLabelText('Password'), 'hunter2')
await user.click(screen.getByRole('button', { name: 'Sign in' }))

await waitFor(() =>
expect(loggedIn).toEqual({ username: 'admin@example.com', password: 'hunter2' }),
)
await waitFor(() => expect(router.state.location.pathname).toBe('/'))
expect(getCsrfToken()).toBe('csrf-pw')
expect(identify).not.toHaveBeenCalled()
expect(assignLocation).not.toHaveBeenCalled()
// Remembered as a password account, with no realm (none was named).
expect(listAccounts()).toMatchObject([
{ identifier: 'admin@example.com', method: 'password' },
])
expect(listAccounts()[0].realm).toBeUndefined()
})

it('is offered the same way for every username, before one is entered', async () => {
const user = userEvent.setup()
const identify = vi.fn(toCorp)
mockAuthApi({ options: identifierFirst, onIdentify: identify })

await renderLogin()

// Offered with the empty form: nothing about an account decides it.
await screen.findByLabelText('Username')
expect(
screen.getByRole('button', { name: 'Sign in with a password instead' }),
).toBeInTheDocument()

for (const name of ['nobody@example.com', 'someone@elsewhere.test', 'plain']) {
await user.type(screen.getByLabelText('Username'), name)
await user.click(
screen.getByRole('button', { name: 'Sign in with a password instead' }),
)
expect(await screen.findByLabelText('Password')).toBeInTheDocument()
expect(screen.getByText(name)).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Change' }))
await user.clear(await screen.findByLabelText('Username'))
}
expect(identify).not.toHaveBeenCalled()
})

it('asks for the username first', async () => {
const user = userEvent.setup()
mockAuthApi({ options: identifierFirst })

await renderLogin()

await screen.findByLabelText('Username')
await user.click(
screen.getByRole('button', { name: 'Sign in with a password instead' }),
)

expect(await screen.findByRole('alert')).toHaveTextContent('Enter your username.')
expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()
})

it('still routes by /auth/identify on Continue', async () => {
const user = userEvent.setup()
mockAuthApi({ options: identifierFirst, onIdentify: toCorp })

await renderLogin()

await user.type(await screen.findByLabelText('Username'), 'admin@example.com')
await user.click(screen.getByRole('button', { name: 'Continue' }))

await waitFor(() =>
expect(assignLocation).toHaveBeenCalledWith(
'/auth/oidc/login?realm=corp&login_hint=admin%40example.com',
),
)
})

it('is not offered without a password realm', async () => {
mockAuthApi({ options: { ...identifierFirst, password: false } })

await renderLogin('/login?error=not_permitted')

await screen.findByLabelText('Username')
expect(
screen.queryByRole('button', { name: 'Sign in with a password instead' }),
).toBeNull()
})

it.each(['not_permitted', 'oidc_failed'])(
'the %s notice offers it next to "Try again", and signs in',
async (code) => {
const user = userEvent.setup()
const identify = vi.fn(toCorp)
let loggedIn: unknown = null
mockAuthApi({
options: identifierFirst,
onIdentify: identify,
onLogin: (body) => {
loggedIn = body
return session('admin@example.com')
},
})

const router = await renderLogin(`/login?error=${code}&redirect=%2Ftokens`)

const alert = await screen.findByRole('alert')
expect(within(alert).getByRole('link', { name: 'Try again' })).toBeInTheDocument()
await user.click(
await within(alert).findByRole('button', {
name: 'Sign in with a password instead',
}),
)

// The username and the password at once; the notice stays, without
// the offer just taken.
await user.type(await screen.findByLabelText('Username'), 'admin@example.com')
await user.type(screen.getByLabelText('Password'), 'hunter2')
expect(screen.queryByRole('button', { name: 'Continue' })).toBeNull()
expect(
screen.queryByRole('button', { name: 'Sign in with a password instead' }),
).toBeNull()
await user.click(screen.getByRole('button', { name: 'Sign in' }))

await waitFor(() =>
expect(loggedIn).toEqual({ username: 'admin@example.com', password: 'hunter2' }),
)
await waitFor(() => expect(router.state.location.pathname).toBe('/tokens'))
expect(identify).not.toHaveBeenCalled()
expect(assignLocation).not.toHaveBeenCalled()
},
)

it('the notice offers it over the remembered accounts too', async () => {
const user = userEvent.setup()
rememberAccount({ identifier: 'admin@example.com', method: 'password', realm: 'local' })
mockAuthApi({ options: identifierFirst, onIdentify: toCorp })

await renderLogin('/login?error=not_permitted')

await screen.findByRole('list', { name: 'Remembered accounts' })
await user.click(
screen.getByRole('button', { name: 'Sign in with a password instead' }),
)

expect(await screen.findByLabelText('Password')).toBeInTheDocument()
expect(screen.queryByRole('list', { name: 'Remembered accounts' })).toBeNull()

await user.click(screen.getByRole('button', { name: 'Back to your accounts' }))
expect(
await screen.findByRole('list', { name: 'Remembered accounts' }),
).toBeInTheDocument()
})

it('"Try again" goes back to the username step', async () => {
const user = userEvent.setup()
mockAuthApi({ options: identifierFirst })

await renderLogin('/login?error=oidc_failed')

await user.click(
await screen.findByRole('button', { name: 'Sign in with a password instead' }),
)
await screen.findByLabelText('Password')
await user.click(screen.getByRole('link', { name: 'Try again' }))

await waitFor(() => expect(screen.queryByRole('alert')).toBeNull())
expect(screen.queryByLabelText('Password')).toBeNull()
expect(screen.getByRole('button', { name: 'Continue' })).toBeInTheDocument()
})

it('leaves the other notices and the combined form as they are', async () => {
mockAuthApi({ options: identifierFirst })
await renderLogin('/login?error=username_not_upn')
const alert = await screen.findByRole('alert')
await screen.findByLabelText('Username')
expect(
within(alert).queryByRole('button', { name: 'Sign in with a password instead' }),
).toBeNull()
cleanup()

mockAuthApi({ options: combined })
await renderLogin('/login?error=not_permitted')
await screen.findByLabelText('Password')
expect(
screen.queryByRole('button', { name: 'Sign in with a password instead' }),
).toBeNull()
})
})

describe('lockout and return-to', () => {
it('shows a lockout message (not "server unreachable") on a 429', async () => {
const user = userEvent.setup()
Expand Down
Loading
Loading