From af02b01421dd5cc24dd723e5bdb02bfd7213b95f Mon Sep 17 00:00:00 2001
From: Claude `) 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
@@ -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()
diff --git a/web/src/routes/Login.tsx b/web/src/routes/Login.tsx
index 8c8e83bc..6aa1c1b6 100644
--- a/web/src/routes/Login.tsx
+++ b/web/src/routes/Login.tsx
@@ -163,6 +163,17 @@ export const Route = createFileRoute('/login')({
* opens reaches the app (`completePendingSignIn`).
* A failed OIDC callback lands here with `?error=`: a short, generic
* message is shown above the form with a "Try again" link that clears it.
+ *
+ * Identifier-first routing is decided by realm configuration alone, so a
+ * username in an OIDC realm's login domain always goes to the identity
+ * provider, also when its account signs in with a password. While a password
+ * realm is enabled (`password`), the identifier-first form therefore offers
+ * "Sign in with a password instead", which asks for the password without
+ * `/auth/identify`, and so does the notice of a sign-in the identity provider
+ * refused or failed (`not_permitted`, `oidc_failed`). Both are offered
+ * whatever the username is, so they tell nothing about an account. (Linking
+ * an identity to an existing account at sign-in, #530, is a step of its own
+ * and does not come back here as `not_permitted`.)
* Signing in with a registry token is intentionally not offered: the UI always
* authenticates with username+password or via a realm. Tokens are minted on
* the Tokens screen for NuGet clients only.
@@ -184,10 +195,22 @@ function Login() {
const [accounts, setAccounts] = useState(listAccounts)
const [picked, setPicked] = useState{claim} claim.
+ +
+ )} )} @@ -264,6 +306,8 @@ function Login() { onForget={forget} onAnother={() => setAnother(true)} /> + ) : passwordInstead ? ( +Forge checks which sign-in your account uses. With your organization’s single sign-on you continue at its sign-in page; @@ -307,6 +354,9 @@ function Login() { ) } +/** The way to the password prompt that does not ask `/auth/identify`. */ +const PASSWORD_INSTEAD = 'Sign in with a password instead' + /** * Append the post-login destination to an OIDC login URL so the server can * send the browser back after the IdP round-trip @@ -421,15 +471,23 @@ function FormError({ children }: { children: React.ReactNode }) { * with a back link) or navigates the browser to the IdP, with the username * as its login hint. With `initialRealm` (a remembered account `identify` * already routed to a password realm) it starts at step 2. + * + * With `passwordOffered` (a password realm is enabled), step 1 also offers + * "Sign in with a password instead": step 2 for the entered username without + * asking `/auth/identify`, for an account whose domain goes to an identity + * provider that cannot sign it in (it is down, or the account is not linked + * to it). It is offered for every username alike. */ function IdentifierFirstLogin({ redirect, remember, + passwordOffered, initialUsername, initialRealm, }: { redirect?: string remember: boolean + passwordOffered: boolean initialUsername?: string initialRealm?: string }) { @@ -472,6 +530,18 @@ function IdentifierFirstLogin({ } } + function onPasswordInstead() { + if (!username.trim()) { + setError('Enter your username.') + return + } + // No realm: `/auth/identify` was not asked, and `/auth/login` tries the + // password realms in their order. + setRealm(undefined) + setError(null) + setStep('password') + } + function onPassword(e: React.FormEvent) { e.preventDefault() if (!password) { @@ -496,6 +566,16 @@ function IdentifierFirstLogin({ + {passwordOffered && ( + + )} ) }