Files
windmill/frontend/src/lib/components/Login.svelte
T
8ec6beed3a feat: require two-factor authentication for password logins (#12704)
* feat: add native totp two-factor authentication for password login

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep login field spacing when the code field shows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep two-factor across a user overwrite and key the prompt to the submitted email

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: snapshot totp in the overwrite delete and audit wrong two-factor codes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: move two-factor to ee, add recovery codes, encrypt secrets, db-backed limit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: refuse a two-factor reset for an account without one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: serialize two-factor races with key rotation and ce enrollment

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: argon2 recovery codes, otpauth link, totp field name, settings styling

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: serialize totp setup with admins key rotation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: two-factor setup wizard that asks for the password

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: ask for the two-factor code on its own login screen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: say two-factor is on with the recovery codes, red turn-off confirm

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref to 5456ade3c09011206acc20ab026dda49f15f9517

This commit updates the EE repository reference after PR #1005 was merged in windmill-ee-private.

Previous ee-repo-ref: 84c64720d625ed5a41b6ff0851f9d9b568b31c0b

New ee-repo-ref: 5456ade3c09011206acc20ab026dda49f15f9517

Automated by sync-ee-ref workflow.

* feat: require two-factor authentication for password logins

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: shorten the require two-factor setting description

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: ignore a login submit while one is in flight

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: note that enrollment at login trusts the password alone

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: point OAuth and SAML users to the identity provider for 2FA

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: finish the root account swap when two-factor is required

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: email the owner when their two-factor settings change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: recommend two-factor for the root account in instance setup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: offer two-factor setup on the login card after instance setup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: stop renewing sessions without two-factor once it is required

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: show a user's two-factor status in the superadmin editor

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: show two-factor in the instance users table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf: load the two-factor setup screens on demand in the login card

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: update ee-repo-ref to 526e8ad937cc09cdbd36f06434e2a0cee9e575bb

This commit updates the EE repository reference after PR #1021 was merged in windmill-ee-private.

Previous ee-repo-ref: 9c14ad5e266144bd8f21eee9d9514570d5c9fbef

New ee-repo-ref: 526e8ad937cc09cdbd36f06434e2a0cee9e575bb

Automated by sync-ee-ref workflow.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
2026-11-27 18:23:17 +01:00

1107 lines
36 KiB
Svelte

<script module lang="ts">
import { noteSessionEmail } from '$lib/onboardingProfile'
import LabeledDivider from './LabeledDivider.svelte'
import type { LastLoginMethod } from '$lib/lastLoginMethod'
/** Feeds the login card a fixed instance configuration instead of the live one.
* Only the kitchen sink at /kitchen_sink/login sets it; production always fetches. */
export type LoginPreview = {
logins?: { type: string; displayName: string }[]
saml?: boolean
disablePasswordLogin?: boolean
smtpConfigured?: boolean
cloud?: boolean
autoRedirecting?: boolean
lastUsed?: LastLoginMethod
}
</script>
<script lang="ts">
import { goto } from '$lib/navigation'
import Auth0Icon from '$lib/components/icons/Auth0Icon.svelte'
import GithubIcon from '$lib/components/icons/GithubIcon.svelte'
import GitlabIcon from '$lib/components/icons/GitlabIcon.svelte'
import GoogleIcon from '$lib/components/icons/GoogleIcon.svelte'
import MicrosoftIcon from '$lib/components/icons/MicrosoftIcon.svelte'
import NextcloudIcon from '$lib/components/icons/NextcloudIcon.svelte'
import OktaIcon from '$lib/components/icons/OktaIcon.svelte'
import PocketIdIcon from '$lib/components/icons/PocketIdIcon.svelte'
import { OauthService, UserService, WorkspaceService } from '$lib/gen'
import { usersWorkspaceStore, workspaceStore, userStore, roleCapabilities } from '$lib/stores'
import { emptyString, escapeHtml, parseQueryParams } from '$lib/utils'
import { getUserExt } from '$lib/user'
import { sendUserToast } from '$lib/toast'
import { isCloudHosted } from '$lib/cloud'
import { refreshSuperadmin } from '$lib/refreshUser'
import { onDestroy, onMount, tick } from 'svelte'
import Skeleton from './common/skeleton/Skeleton.svelte'
import Button from './common/button/Button.svelte'
import Password from './Password.svelte'
import TextInput from './text_input/TextInput.svelte'
import { sameTopDomainOrigin } from '$lib/cookies'
import { isValidLogoutRedirect, toSameOriginRelativePath } from '$lib/logoutRedirect'
import InputError from './InputError.svelte'
import {
loginErrorDetail,
loginErrorMessage,
TOTP_INVALID_MESSAGE,
TOTP_REQUIRED,
TOTP_SETUP_REQUIRED
} from '$lib/loginError'
import { parseAccountSettingsHash } from './sidebar/settings'
import Badge from './common/badge/Badge.svelte'
import {
getLastLoginMethod,
clearPendingLoginMethod,
confirmPendingLoginMethod,
markLoginMethodPending,
rememberLoginMethod,
sameLoginMethod
} from '$lib/lastLoginMethod'
interface Props {
rd?: string | undefined
email?: string | undefined
password?: string | undefined
error?: string | undefined
popup?: boolean
firstTime?: boolean
autoRedirect?: boolean
/** Supplying this replaces the post-login redirect: the card hands back instead of
* navigating, and the host is expected to re-check access in place. For a gate on a
* page that stays mounted, `rd` is the URL already shown, so navigating there would
* re-run nothing. */
onLoginSuccess?: () => void
/** A refusal the popup relayed back, in the server's words. */
onLoginError?: (message: string) => void
preview?: LoginPreview
/** Reports the instance's login options once loaded, so the page around the card can
* adapt its heading: a third-party login also creates the account on first use. */
onOptionsLoaded?: (options: { hasThirdParty: boolean }) => void
/** `<workspace>/<app_path>` when this sign-in is someone opening an app that is
* open to guests. A third-party login then mints a guest session -- no account,
* no seat -- instead of creating a user. Omitting it is what promotion is: the
* same sign-in without this, which provisions them for real. Password login
* ignores it: a guest has no stored credential to check. */
guestApp?: string | undefined
/** Once a password login succeeds, offer to set up two-factor authentication before
* leaving the card, for an account that has none. The user can skip it. */
offerTotpSetup?: boolean
}
let {
rd = undefined,
email = $bindable(undefined),
password = $bindable(undefined),
error = undefined,
popup = false,
firstTime = false,
autoRedirect = true,
onLoginSuccess = undefined,
onLoginError = undefined,
preview = undefined,
onOptionsLoaded = undefined,
guestApp = undefined,
offerTotpSetup = false
}: Props = $props()
// The harness never takes effect in a production bundle, whatever a caller passes.
let previewConfig = $derived(import.meta.env.DEV ? preview : undefined)
let cloudHosted = $derived(previewConfig ? !!previewConfig.cloud : isCloudHosted())
let lastUsed = $state<LastLoginMethod | undefined>(undefined)
let lastUsedPassword = $derived(!!lastUsed && lastUsed.kind === 'password')
// Scoped per instance: the kitchen sink mounts every card at once, and a hardcoded id
// would point each card's labels and aria-describedby at the first card's fields.
const uid = $props.id()
const emailId = `${uid}-email`
const passwordId = `${uid}-password`
const errorId = `${uid}-error`
const totpId = `${uid}-totp`
const emailErrorId = `${uid}-email-error`
const providers = [
{
type: 'github',
name: 'GitHub',
icon: GithubIcon
},
{
type: 'gitlab',
name: 'GitLab',
icon: GitlabIcon
},
{
type: 'google',
name: 'Google',
icon: GoogleIcon
},
{
type: 'microsoft',
name: 'Microsoft',
icon: MicrosoftIcon
},
{
type: 'okta',
name: 'Okta',
icon: OktaIcon
},
{
type: 'auth0',
name: 'Auth0',
icon: Auth0Icon
},
{
type: 'nextcloud',
name: 'Nextcloud',
icon: NextcloudIcon
},
{
type: 'pocketid',
name: 'Pocket ID',
icon: PocketIdIcon
}
] as const
type ThirdPartyMethod = {
method: { kind: 'oauth'; provider: string } | { kind: 'saml' }
displayName: string
icon?: any
}
// rank() maps an unknown type to known.length, not indexOf's -1, so a custom OAuth client
// sorts after the known providers rather than ahead of all of them. SAML sits at the end,
// and whatever worked last time is hoisted to the front.
let orderedThirdParty = $derived.by(() => {
const known = providers.map((p) => p.type as string)
const rank = (type: string) => (known.indexOf(type) === -1 ? known.length : known.indexOf(type))
const oauth: ThirdPartyMethod[] = [...(logins ?? [])]
.sort((a, b) => rank(a.type) - rank(b.type))
.map((login) => ({
method: { kind: 'oauth', provider: login.type },
displayName: login.displayName,
icon: providers.find((p) => p.type === login.type)?.icon
}))
const all: ThirdPartyMethod[] = saml
? [...oauth, { method: { kind: 'saml' }, displayName: 'SSO' }]
: oauth
const lastIdx = all.findIndex((m) => sameLoginMethod(lastUsed, m.method))
if (lastIdx > 0) all.unshift(...all.splice(lastIdx, 1))
return all
})
let showPassword = $state(false)
let passwordField = $state<Password | undefined>(undefined)
// The account the server asked an authenticator code for, or asked to set up two-factor
// authentication when the instance requires it. These steps replace the form and resend the
// password kept here, so the server needs no half-logged-in state.
let totpFor = $state<string | undefined>(undefined)
let totpCode = $state('')
let totpField = $state<TextInput | undefined>(undefined)
let showTotp = $derived(totpFor !== undefined && totpFor === email)
let enrollSetup = $state<{ secret: string; otpauth_url: string } | undefined>(undefined)
// Kept across Back: asking the server again replaces the pending secret, and the QR code the
// user already scanned would stop working.
let issuedSetup: { email: string; setup: { secret: string; otpauth_url: string } } | undefined
let enrollCodes = $state<string[] | undefined>(undefined)
// The setup was offered after a successful login, rather than required before one: a
// session exists, so it goes through the signed-in routes and can be skipped.
let enrollOptional = $state(false)
let enrollSaved = $state(false)
async function openTotpStep(forEmail: string, setup?: { secret: string; otpauth_url: string }) {
formError = undefined
shake = false
totpFor = forEmail
totpCode = ''
enrollSetup = setup
await tick()
totpField?.focus()
}
async function leaveTotp() {
totpFor = undefined
totpCode = ''
enrollSetup = undefined
formError = undefined
// The swapped-in markup would otherwise replay a shake left on from a failed attempt.
shake = false
await tick()
passwordField?.focus()
}
// Type argument rather than annotation: annotating narrows the declaration to the
// initializer's `undefined`, so a top-level read sees `never` instead of the array.
let logins = $state<OAuthLogin[] | undefined>(undefined)
let saml: string | undefined = $state(undefined)
let smtpConfigured: boolean | undefined = $state(undefined)
let disablePasswordLogin = $state(false)
let autoRedirecting = $state(false)
let oauthFlowDone = false
// The method that worked last time leads: the form takes the top of the card, already open.
let passwordFirst = $derived(lastUsedPassword && !disablePasswordLogin && !autoRedirecting)
// Errors that belong to the credentials the user just submitted: they stay under the
// password field until either field changes, so a stale message can't outlive its attempt.
let formError = $state<
| {
message: string
fields: 'both' | 'email' | 'password' | 'totp'
email: string | undefined
password: string | undefined
}
| undefined
>(undefined)
let shake = $state(false)
let fieldsEl = $state<HTMLDivElement | undefined>(undefined)
let credentialsError = $derived(
formError && formError.email === email && formError.password === password
? formError.message
: undefined
)
// 'both' sits under the password field, at the end of the form, where a rejected
// credential pair belongs; a single missing field gets the message under itself.
let errorField = $derived(credentialsError ? (formError?.fields ?? 'both') : undefined)
let emailErrored = $derived(errorField === 'email' || errorField === 'both')
let passwordErrored = $derived(errorField === 'password' || errorField === 'both')
async function failLogin(
message: string,
fields: 'both' | 'email' | 'password' | 'totp' = 'both',
// The pair the message is about. Defaults to what is in the fields right now, but a
// rejected request passes what it actually submitted: the user may have typed on since.
attempted: { email: string | undefined; password: string | undefined } = { email, password }
) {
// The shake is for a retry that fails the same way: on the first failure the message
// appearing is the signal, and shaking it in would be noise.
const wasAlreadyShown = credentialsError != undefined
formError = { message, fields, ...attempted }
// tick() only writes the DOM. Without a layout read between the removal and the re-add,
// the browser coalesces both into one style recalculation, sees a class that never left,
// and replays nothing from the second retry onwards.
shake = false
if (!wasAlreadyShown) return
await tick()
void fieldsEl?.offsetWidth
shake = true
}
type OAuthLogin = {
type: string
displayName: string
}
// One submit at a time: a second setup request would replace the pending secret, and the
// page could then show a QR code the server no longer accepts.
let submitting = false
async function login(): Promise<void> {
if (submitting) return
submitting = true
try {
await submitLogin()
} finally {
submitting = false
}
}
async function submitLogin(): Promise<void> {
if (!email || !password) {
if (!email && !password) failLogin('Enter both your email and password.')
else if (!email) failLogin('Enter your email.', 'email')
else failLogin('Enter your password.', 'password')
return
}
if (previewConfig) {
failLogin('Invalid email or password.')
return
}
if (showTotp && !totpCode.trim()) {
failLogin('Enter the code from your authenticator app.', 'totp')
return
}
if (showTotp && enrollSetup) {
await enableTotpAtLogin()
return
}
const requestBody = {
email,
password,
totp_code: showTotp ? totpCode : undefined
}
// Await the DOM update: the field must be back to type="password" before the
// request goes out, or the browser may not offer to save the credential
passwordField?.conceal()
await tick()
try {
await UserService.login({ requestBody })
// The session changed under a page that stays mounted: what was cached for the
// previous account must not be served to this one.
noteSessionEmail(email)
} catch (err) {
const detail = loginErrorDetail(err)
if (detail === TOTP_REQUIRED) {
await openTotpStep(requestBody.email)
return
}
if (detail === TOTP_SETUP_REQUIRED) {
try {
const setup =
issuedSetup?.email === requestBody.email
? issuedSetup.setup
: await UserService.loginTotpSetup({
requestBody: { email: requestBody.email, password: requestBody.password }
})
issuedSetup = { email: requestBody.email, setup }
await openTotpStep(requestBody.email, setup)
} catch (setupErr) {
failLogin(loginErrorMessage(setupErr), 'both', requestBody)
}
return
}
const message = loginErrorMessage(err)
failLogin(message, message === TOTP_INVALID_MESSAGE ? 'totp' : 'both', requestBody)
return
}
if (offerTotpSetup && !requestBody.totp_code) {
// Refused when the account already has a second factor, or by a build or edition
// that cannot give it one: the login then simply goes on.
const setup = await UserService.setupTotp({
requestBody: { password: requestBody.password }
}).catch(() => undefined)
if (setup) {
enrollOptional = true
await openTotpStep(requestBody.email, setup)
return
}
}
await finishLogin()
}
// Turns the second factor on and, when it was required, starts the session in the same
// request; the recovery codes it returns are shown before leaving the page.
async function enableTotpAtLogin(): Promise<void> {
const requestBody = { email: email!, password: password!, code: totpCode }
try {
const { recovery_codes } = enrollOptional
? await UserService.enableTotp({ requestBody: { code: totpCode } })
: await UserService.loginTotpEnable({ requestBody })
noteSessionEmail(requestBody.email)
formError = undefined
enrollSetup = undefined
enrollCodes = recovery_codes
} catch (err) {
const message = loginErrorMessage(err)
failLogin(message, message === TOTP_INVALID_MESSAGE ? 'totp' : 'both', requestBody)
}
}
async function finishLogin(): Promise<void> {
formError = undefined
rememberLoginMethod({ kind: 'password' })
if (firstTime) {
goto('/user/first-time')
return
}
// Once logged in, we can fetch the workspaces
$usersWorkspaceStore = await WorkspaceService.listUserWorkspaces()
// trigger a reload of the user
if ($workspaceStore) {
$userStore = await getUserExt($workspaceStore)
}
// Finally, we check whether the user is a superadmin
refreshSuperadmin()
if (onLoginSuccess) {
onLoginSuccess()
return
}
redirectUser()
}
async function redirectUser() {
// Reduce same-origin full URLs to relative paths so deep links from
// e.g. /a/[...path] (which carry the full URL as rd) still navigate
// correctly instead of falling through to the cross-origin branch.
let resolvedRd = toSameOriginRelativePath(rd) ?? rd
if (resolvedRd?.startsWith('http')) {
if (isValidLogoutRedirect(resolvedRd)) {
window.location.href = resolvedRd
return
}
goto('/')
return
}
if ($workspaceStore) {
goto(resolvedRd ?? '/')
} else {
let workspaceTarget = parseQueryParams(resolvedRd ?? undefined)['workspace']
if (resolvedRd && workspaceTarget) {
$workspaceStore = workspaceTarget
goto(resolvedRd)
return
}
if (!$usersWorkspaceStore) {
try {
usersWorkspaceStore.set(await WorkspaceService.listUserWorkspaces())
} catch {}
}
const allWorkspaces = $usersWorkspaceStore?.workspaces.filter((x) => x.id != 'admins')
if (allWorkspaces?.length == 1) {
workspaceStore.set(allWorkspaces[0].id)
$userStore = await getUserExt($workspaceStore!)
if (!$roleCapabilities.code) {
let defaultApp = await WorkspaceService.getWorkspaceDefaultApp({
workspace: $workspaceStore!
})
if (!emptyString(defaultApp.default_app_path)) {
const prefix = defaultApp.default_app_raw ? '/apps_raw/get' : '/apps/get'
goto(`${prefix}/${defaultApp.default_app_path}`)
} else {
goto(resolvedRd ?? '/')
}
} else {
goto(resolvedRd ?? '/')
}
// See (root)/+layout.svelte for why these skip the picker.
} else if (
resolvedRd?.startsWith('/user/workspaces') ||
resolvedRd?.startsWith('/oauth/mcp_authorize') ||
resolvedRd?.startsWith('/projects/import')
) {
goto(resolvedRd)
} else if (resolvedRd?.startsWith('/#') && parseAccountSettingsHash(resolvedRd.slice(1))) {
goto(`/user/workspaces${resolvedRd.slice(1)}`)
} else {
goto(`/user/workspaces${resolvedRd ? `?rd=${encodeURIComponent(resolvedRd)}` : ''}`)
}
}
}
async function loadLogins() {
if (previewConfig) {
logins = previewConfig.logins ?? []
saml = previewConfig.saml ? 'https://idp.example.com/sso' : undefined
disablePasswordLogin = previewConfig.disablePasswordLogin ?? false
autoRedirecting = previewConfig.autoRedirecting ?? false
lastUsed = previewConfig.lastUsed
showPassword =
!disablePasswordLogin &&
(lastUsedPassword ||
(logins.length === 0 && !saml) ||
(email != undefined && email.length > 0))
onOptionsLoaded?.({ hasThirdParty: logins.length > 0 || !!saml })
return
}
const [loginsResult, disabledResult] = await Promise.allSettled([
OauthService.listOauthLogins(),
UserService.isPasswordLoginDisabled()
])
if (disabledResult.status === 'fulfilled') {
disablePasswordLogin = disabledResult.value ?? false
} else {
disablePasswordLogin = false
console.error('Could not load password login setting', disabledResult.reason)
}
let autoLogin: string | undefined = undefined
if (loginsResult.status === 'fulfilled') {
logins = loginsResult.value.oauth.map((login) => ({
type: login.type,
displayName:
login.display_name || providers.find((p) => p.type === login.type)?.name || login.type
}))
saml = loginsResult.value.saml
autoLogin = loginsResult.value.auto_login
} else {
logins = []
saml = undefined
console.error('Could not load logins', loginsResult.reason)
}
lastUsed = getLastLoginMethod()
showPassword =
!disablePasswordLogin &&
(lastUsedPassword ||
(logins?.length === 0 && !saml) ||
(email != undefined && email.length > 0))
onOptionsLoaded?.({ hasThirdParty: (logins?.length ?? 0) > 0 || !!saml })
if (autoRedirect && autoLogin && !error && !shouldSkipAutoRedirect()) {
if (autoLogin === 'saml' && saml) {
autoRedirecting = true
if (!redirectSaml()) autoRedirecting = false
} else if (logins?.some((l) => l.type === autoLogin)) {
autoRedirecting = true
if (!storeRedirect(autoLogin, true)) {
autoRedirecting = false
sendUserToast('Popup blocked — please click the sign-in button to continue.', true)
}
}
}
}
function shouldSkipAutoRedirect(): boolean {
try {
const params = new URLSearchParams(window.location.search)
return params.get('no_sso') === '1'
} catch {
return false
}
}
loadLogins()
$effect(() => {
if (firstTime && !email && !password) {
email = 'admin@windmill.dev'
password = 'changeme'
}
})
async function checkSmtpConfigured() {
if (previewConfig) {
smtpConfigured = previewConfig.smtpConfigured ?? false
return
}
try {
smtpConfigured = await UserService.isSmtpConfigured()
} catch (err) {
console.error('Could not check if SMTP is configured', err)
smtpConfigured = false
}
}
checkSmtpConfigured()
function handleKeyDown(event: KeyboardEvent) {
const key = event.key
// keydown auto-repeats while held, and Enter also confirms an IME candidate —
// either would submit the form more than once per keypress
if (key === 'Enter' && !event.isComposing && !event.repeat) {
event.preventDefault()
login()
}
}
onMount(() => {
try {
localStorage.removeItem('closeUponLogin')
} catch (e) {
console.error('Could not remove closeUponLogin from local storage', e)
}
})
function popupListener(event) {
let data = event.data
// console.log('popupListener', data, event.origin, window.location.origin)
if (!sameTopDomainOrigin(event.origin, window.location.origin)) {
console.log('popupListener from different origin', event.origin, window.location.origin)
return
}
processPopupData(data)
if (data.type === 'success' || data.type === 'error') {
console.log('Removing popup listener')
window.removeEventListener('message', popupListener)
}
}
function processPopupData(data) {
if (data.type === 'error') {
clearPendingLoginMethod()
sendUserToast(data.error, true)
onLoginError?.(data.error)
} else if (data.type === 'success') {
finishOauthFlow('postMessage')
}
}
function handleStorageEvent(event) {
if (event.key === 'oauth-success') {
try {
const data = JSON.parse(event.newValue)
console.log('oauth-success from storage')
// Clean up
localStorage.removeItem('oauth-success')
window.removeEventListener('storage', handleStorageEvent)
if (data?.type === 'success') {
finishOauthFlow('storage')
} else {
processPopupData(data)
}
} catch (e) {
console.error('Could not process oauth-success from storage', e)
}
} else {
console.log('Storage event', event.key)
}
}
function finishOauthFlow(via: 'postMessage' | 'storage' | 'poll', win?: Window) {
if (oauthFlowDone) return
oauthFlowDone = true
// The popup replaced the session under this still-mounted page; whatever the
// previous account had cached must not be served to the new one.
noteSessionEmail(undefined)
confirmPendingLoginMethod()
console.log(`oauth: signaled via ${via}`)
if (win && !win.closed) win.close()
window.removeEventListener('message', popupListener)
window.removeEventListener('storage', handleStorageEvent)
onLoginSuccess?.()
}
onDestroy(() => {
window.removeEventListener('message', popupListener)
window.removeEventListener('storage', handleStorageEvent)
})
function persistRd() {
if (!rd) return
// Only persist a same-origin relative path. Storing a full URL pollutes
// the localStorage key with a value that the post-login redirect logic
// can't reuse safely (it would fall through the open-redirect guard).
const safe = toSameOriginRelativePath(rd)
if (!safe) return
try {
localStorage.setItem('rd', safe)
} catch (e) {
console.error('Could not persist redirection to local storage', e)
}
}
// `automatic` marks the auto-login redirect, the one login that has to reach the
// provider without drawing anything. It suppresses the provider's extra params —
// Google's and Microsoft's account chooser — which every other login gets.
function storeRedirect(provider: string, automatic: boolean): boolean {
// The kitchen sink renders real provider buttons; clicking one must not leave the page.
if (previewConfig) return true
markLoginMethodPending({ kind: 'oauth', provider })
persistRd()
const params = new URLSearchParams()
if (popup) params.set('close', 'true')
if (automatic) params.set('auto', 'true')
if (guestApp) params.set('guest_app', guestApp)
const query = params.size > 0 ? '?' + params.toString() : ''
let url = '/api/oauth/login/' + provider + query
console.log('storeRedirect', popup, url)
if (popup) {
localStorage.setItem('closeUponLogin', 'true')
window.addEventListener('message', popupListener)
window.addEventListener('storage', handleStorageEvent)
const win = window.open(url, '_blank', 'popup')
if (!win) {
window.removeEventListener('message', popupListener)
window.removeEventListener('storage', handleStorageEvent)
clearPendingLoginMethod()
return false
}
// Safety net for Safari: when the popup is opened without a fresh user
// gesture (auto-login), ITP can partition cookies/localStorage between
// popup and parent, so neither the close cookie, the postMessage, nor
// the localStorage 'oauth-success' signal reaches us. The session
// cookie is set same-origin and isn't subject to that partitioning, so
// polling whoami catches the success and lets us force-close the popup.
pollForLoginSuccess(win)
return true
} else {
localStorage.setItem('closeUponLogin', 'false')
window.location.href = url
return true
}
}
function pollForLoginSuccess(win: Window) {
const startedAt = Date.now()
const interval = setInterval(async () => {
if (oauthFlowDone) {
clearInterval(interval)
return
}
if (Date.now() - startedAt > 5 * 60 * 1000) {
clearInterval(interval)
console.log('oauth: poll timed out after 5 minutes')
return
}
if (win.closed) {
clearInterval(interval)
console.log('oauth: popup closed before login completed')
return
}
// A guest session is pinned to its workspace and cannot answer the global
// probe; an ordinary session for a non-member cannot answer the workspace
// one. Either answering means the popup signed someone in.
const guestWorkspace = guestApp?.split('/')[0]
const probes: Promise<unknown>[] = [UserService.getCurrentEmail()]
if (guestWorkspace) probes.push(UserService.whoami({ workspace: guestWorkspace }))
try {
await Promise.any(probes)
} catch {
return
}
clearInterval(interval)
finishOauthFlow('poll', win)
}, 1500)
}
function redirectSaml(): boolean {
if (!saml) {
sendUserToast('No SAML login available', true)
return false
}
if (previewConfig) return true
markLoginMethodPending({ kind: 'saml' })
let target = saml
let relayStateSet = false
// Carry the SP-initiated deep link through the IdP round-trip via SAML
// RelayState so the ACS redirects straight back to it (bypassing
// /user/login). Same-origin relative paths are passed verbatim;
// full URLs (e.g. the page URL from /a/[...path]) are reduced to their
// path component first. The backend re-validates. Cross-origin or
// otherwise unsafe values fall through to the localStorage fallback.
// A guest entry rides in the same RelayState as a `guest_app` query parameter
// the ACS lifts out: SAML never passes through `/api/oauth/login/<client>`,
// where the OAuth path hands its target to the server.
let safePath = toSameOriginRelativePath(rd)
if (guestApp && safePath) {
const hashAt = safePath.indexOf('#')
const pathAndQuery = hashAt === -1 ? safePath : safePath.slice(0, hashAt)
const hash = hashAt === -1 ? '' : safePath.slice(hashAt)
const sep = pathAndQuery.includes('?') ? '&' : '?'
safePath = `${pathAndQuery}${sep}guest_app=${encodeURIComponent(guestApp)}${hash}`
}
if (safePath) {
try {
const url = new URL(saml)
url.searchParams.set('RelayState', safePath)
target = url.toString()
relayStateSet = true
} catch (e) {
console.error('Could not set SAML RelayState', e)
}
}
if (guestApp && !relayStateSet) {
// Without the target the callback provisions an account, so a guest
// sign-in that cannot carry it does not start.
clearPendingLoginMethod()
sendUserToast('Could not start sign-in, please try again.', true)
return false
}
// Only use the localStorage fallback when RelayState is NOT carrying the
// deep link. With RelayState the ACS redirects straight to the target and
// /user/login never consumes/clears the key, so a persisted value would
// go stale and hijack a later plain visit to /user/login.
if (!relayStateSet) {
persistRd()
}
window.location.href = target
return true
}
$effect(() => {
error && sendUserToast(escapeHtml(error), true)
})
</script>
<!-- The red borders are colour-only, so role="alert" is what makes a failed attempt reach a
screen reader. -->
{#snippet errorMessage()}
<div id={errorId} role="alert" class="min-h-5">
{#if errorField !== 'email'}
<InputError error={credentialsError} />
{/if}
</div>
{/snippet}
<!-- Straddles the button's top edge, so the row keeps its height and the badge reads as a
label on the button rather than another line of content. -->
{#snippet lastUsedBadge()}
<!-- Hung off the corner like a notification badge: a long provider name wraps to two lines
inside the button, and anything sitting further in would land on the label. The ring is
the card's own colour so the badge punches through the button border. -->
<div class="absolute top-0 right-0 -translate-y-1/2 translate-x-1/4 z-10">
<Badge color="blue" small class="ring-2 ring-surface !px-1 !py-0 !text-[10px] leading-4">
Last used
</Badge>
</div>
{/snippet}
{#snippet providerButtons()}
<div class="grid gap-4 {autoRedirecting ? 'hidden' : ''}">
{#if !logins}
{#each Array(4) as _}
<Skeleton layout={[0.5, [2.375]]} />
{/each}
{:else}
{#each orderedThirdParty as entry (entry.method.kind === 'saml' ? 'saml:' : `oauth:${entry.method.provider}`)}
<div class="relative">
{#if sameLoginMethod(lastUsed, entry.method)}
{@render lastUsedBadge()}
{/if}
<Button
variant="default"
unifiedSize="lg"
startIcon={entry.icon ? { icon: entry.icon, classes: 'h-4' } : undefined}
onClick={() =>
entry.method.kind === 'saml'
? redirectSaml()
: storeRedirect(entry.method.provider, false)}
>
Continue with {entry.displayName}
</Button>
</div>
{/each}
{/if}
</div>
{/snippet}
{#snippet orDivider()}
<LabeledDivider class="gap-3 my-6">
<span class="text-2xs uppercase text-secondary">or</span>
</LabeledDivider>
{/snippet}
<div class="bg-surface px-4 py-8 border sm:rounded-lg sm:px-10">
{#if autoRedirecting}
<p class="text-sm text-center text-secondary py-4">Signing you in…</p>
{/if}
{#if enrollCodes}
<div class="space-y-4">
<div class="space-y-1">
<h2 class="text-sm font-semibold text-emphasis">Two-factor authentication is on</h2>
<p class="text-xs text-secondary">
Logging in with your password now also asks for a code from your authenticator app.
</p>
</div>
<!-- Loaded on demand: the login page every visitor gets stays free of the QR encoder
and the code highlighter, which only these screens use. -->
{#await import('./settings/TotpRecoveryCodes.svelte') then { default: TotpRecoveryCodes }}
<TotpRecoveryCodes codes={enrollCodes} bind:saved={enrollSaved} />
{/await}
<Button onClick={finishLogin} variant="accent" unifiedSize="lg" disabled={!enrollSaved}>
Continue
</Button>
</div>
{:else if showTotp}
<div class="space-y-4">
<div class="space-y-1">
{#if enrollSetup && enrollOptional}
<h2 class="text-sm font-semibold text-emphasis">Set up two-factor authentication</h2>
<p class="text-xs text-secondary">
Protect {email} with a code from an authenticator app on top of its password. You can
also do this later from your user settings.
</p>
{:else if enrollSetup}
<h2 class="text-sm font-semibold text-emphasis">Set up two-factor authentication</h2>
<p class="text-xs text-secondary">
This instance asks for a code from an authenticator app at each password login. Add
{email} to your app to continue.
</p>
{:else}
<h2 class="text-sm font-semibold text-emphasis">Two-factor authentication</h2>
<p class="text-xs text-secondary">
Enter the code from your authenticator app for {email}, or one of your recovery codes.
</p>
{/if}
</div>
{#if enrollSetup}
{#await import('./settings/TotpAuthenticator.svelte') then { default: TotpAuthenticator }}
<TotpAuthenticator setup={enrollSetup} />
{/await}
{/if}
<div bind:this={fieldsEl} class="space-y-1 {shake ? 'motion-safe:animate-shake' : ''}">
<label for={totpId} class="block text-xs font-semibold text-emphasis">
{enrollSetup ? 'The 6-digit code your app shows' : 'Authentication code'}
</label>
<TextInput
bind:this={totpField}
size="md"
bind:value={totpCode}
error={!!errorField}
inputProps={{
id: totpId,
name: 'totp',
'aria-invalid': errorField ? 'true' : undefined,
'aria-describedby': errorField ? errorId : undefined,
autocomplete: 'one-time-code',
onkeydown: handleKeyDown
}}
/>
{@render errorMessage()}
</div>
<div class="space-y-2">
<Button onClick={login} variant="accent" unifiedSize="lg" disabled={!totpCode.trim()}>
{enrollOptional ? 'Turn on' : enrollSetup ? 'Turn on and log in' : 'Verify'}
</Button>
<div class="center-center">
{#if enrollOptional}
<Button unifiedSize="sm" variant="subtle" onClick={finishLogin}>Skip for now</Button>
{:else}
<Button unifiedSize="sm" variant="subtle" onClick={leaveTotp}>Back</Button>
{/if}
</div>
</div>
</div>
{:else}
{#if !passwordFirst}
{@render providerButtons()}
{#if !autoRedirecting && !disablePasswordLogin && (saml || (logins && logins.length > 0))}
{@render orDivider()}
<!-- Only an entry point to the form below: once that is open the divider is what
separates the two ways in. -->
{#if !showPassword}
<div class="center-center">
<Button
unifiedSize="sm"
variant="subtle"
onClick={() => {
showPassword = true
}}
>
Log in without third-party
</Button>
</div>
{/if}
{/if}
{/if}
{#if !autoRedirecting && showPassword && !disablePasswordLogin}
<div>
{#if firstTime}
<p class="text-xs text-center w-full pb-4 text-secondary">
Welcome! Default credentials admin@windmill.dev / changeme have been prefilled.
</p>
{/if}
<div class="space-y-4">
{#if cloudHosted}
<p class="text-xs text-secondary pb-6">
To get credentials without the OAuth providers above, send an email at
contact@windmill.dev
</p>
{/if}
<div bind:this={fieldsEl} class="space-y-2 {shake ? 'motion-safe:animate-shake' : ''}">
<div class="space-y-1">
<label for={emailId} class="block text-xs font-semibold text-emphasis"> Email </label>
<div>
<TextInput
size="md"
error={emailErrored}
bind:value={email}
inputProps={{
id: emailId,
type: 'email',
autocomplete: 'username',
'aria-invalid': emailErrored ? 'true' : undefined,
'aria-describedby':
errorField === 'email' ? emailErrorId : emailErrored ? errorId : undefined,
onkeydown: (e) => {
// Only move on once the field holds something: while the browser's
// credential dropdown is open, Enter belongs to the dropdown
if (
e.key === 'Enter' &&
!e.isComposing &&
!e.repeat &&
e.currentTarget.value
) {
e.preventDefault()
passwordField?.focus()
}
}
}}
/>
</div>
<div id={emailErrorId} role="alert" class="min-h-5">
{#if errorField === 'email'}
<InputError error={credentialsError} />
{/if}
</div>
</div>
<div class="space-y-1">
<label for={passwordId} class="block text-xs font-semibold text-emphasis">
Password
</label>
<div>
<Password
bind:this={passwordField}
bind:password
id={passwordId}
placeholder=""
autocomplete="current-password"
allowMultiline={false}
error={passwordErrored}
describedBy={passwordErrored ? errorId : undefined}
onKeyDown={handleKeyDown}
/>
</div>
{@render errorMessage()}
</div>
</div>
<div>
<Button
onClick={login}
variant="accent"
unifiedSize="lg"
disabled={!email || !password}
>
Log in
</Button>
{#if smtpConfigured}
<div class="text-center pt-2">
<a
href="/user/forgot-password"
class="text-2xs text-blue-500 hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300"
>
Forgot password?
</a>
</div>
{/if}
</div>
</div>
{#if cloudHosted}
<p class="text-2xs text-secondary mt-10 text-center">
By logging in, you agree to our
<a href="https://windmill.dev/terms_of_service" target="_blank" rel="noreferrer">
Terms of service
</a>
and
<a href="https://windmill.dev/privacy_policy" target="_blank" rel="noreferrer">
Privacy policy
</a>
</p>
{/if}
</div>
{/if}
{#if passwordFirst && (saml || (logins && logins.length > 0))}
{@render orDivider()}
{@render providerButtons()}
{/if}
{/if}
</div>