login_page.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. """Server-rendered /login page (no React, no SPA bundle, no injected token).
  2. Providers come from the registry; an OAuth provider renders an anchor to
  3. ``/auth/login?provider=<name>``, a ``supports_password`` provider renders a
  4. credential form wired by :data:`_PASSWORD_FORM_SCRIPT`. Styling mirrors the
  5. ``@nous-research/ui`` design system; fonts load from the SPA's ``/fonts/``
  6. mount, which the gate allowlists pre-auth.
  7. The ``class="provider-btn"`` anchor is test-stable: the suite extracts its
  8. href to walk the OAuth flow.
  9. """
  10. from __future__ import annotations
  11. import html
  12. from urllib.parse import quote, urlencode
  13. from hermes_cli.dashboard_auth import list_interactive_providers, list_session_providers
  14. # Single curly braces are ``str.format`` placeholders; CSS curlies are doubled.
  15. _LOGIN_HTML_TEMPLATE = """\
  16. <!doctype html>
  17. <html lang="en">
  18. <head>
  19. <meta charset="utf-8">
  20. <meta name="viewport" content="width=device-width, initial-scale=1">
  21. <title>Sign in — Hermes Agent</title>
  22. <style>
  23. /* Brand fonts shipped by @nous-research/ui — same files the SPA loads. */
  24. @font-face {{
  25. font-family: 'Collapse';
  26. font-style: normal;
  27. font-weight: 400;
  28. font-display: swap;
  29. src: url('/fonts/Collapse-Regular.woff2') format('woff2');
  30. }}
  31. @font-face {{
  32. font-family: 'Collapse';
  33. font-style: normal;
  34. font-weight: 700;
  35. font-display: swap;
  36. src: url('/fonts/Collapse-Bold.woff2') format('woff2');
  37. }}
  38. @font-face {{
  39. font-family: 'Rules Compressed';
  40. font-style: normal;
  41. font-weight: 400;
  42. font-display: swap;
  43. src: url('/fonts/RulesCompressed-Regular.woff2') format('woff2');
  44. }}
  45. @font-face {{
  46. font-family: 'Rules Compressed';
  47. font-style: normal;
  48. font-weight: 600;
  49. font-display: swap;
  50. src: url('/fonts/RulesCompressed-Medium.woff2') format('woff2');
  51. }}
  52. :root {{
  53. --background-base: #170d02;
  54. --background: #170d02;
  55. --midground: #ffac02;
  56. --foreground: #ffffff;
  57. --hairline: color-mix(in srgb, #ffac02 18%, transparent);
  58. --hairline-strong: color-mix(in srgb, #ffac02 35%, transparent);
  59. }}
  60. *, *::before, *::after {{ box-sizing: border-box; }}
  61. html, body {{
  62. margin: 0;
  63. padding: 0;
  64. min-height: 100%;
  65. background: var(--background-base);
  66. color: var(--foreground);
  67. font-family: 'Collapse', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  68. font-size: 16px;
  69. line-height: 1.5;
  70. -webkit-font-smoothing: antialiased;
  71. -moz-osx-font-smoothing: grayscale;
  72. }}
  73. /* Subtle dot-grid backdrop — DS idiom (see `.dither` in globals.css). */
  74. body {{
  75. background-image:
  76. radial-gradient(
  77. ellipse at top,
  78. color-mix(in srgb, var(--midground) 6%, transparent) 0%,
  79. transparent 55%
  80. ),
  81. repeating-conic-gradient(
  82. color-mix(in srgb, var(--midground) 4%, transparent) 0% 25%,
  83. transparent 0% 50%
  84. );
  85. background-size: auto, 3px 3px;
  86. background-attachment: fixed;
  87. }}
  88. /* Layout: vertically center on tall screens, top-anchor on short. */
  89. body {{
  90. display: grid;
  91. place-items: center;
  92. padding: clamp(1.5rem, 6vh, 6rem) 1.25rem;
  93. }}
  94. main {{
  95. width: 100%;
  96. max-width: 26rem;
  97. position: relative;
  98. animation: slide-up 0.6s ease-out both;
  99. }}
  100. @keyframes slide-up {{
  101. from {{ opacity: 0; transform: translateY(6px); }}
  102. to {{ opacity: 1; transform: translateY(0); }}
  103. }}
  104. @media (prefers-reduced-motion: reduce) {{
  105. main {{ animation: none; }}
  106. }}
  107. /* Brand wordmark above the card — same uppercase + wide-tracking
  108. idiom DS Buttons use. */
  109. .brand {{
  110. text-align: center;
  111. margin-bottom: 1.75rem;
  112. font-family: 'Rules Compressed', 'Collapse', sans-serif;
  113. font-weight: 600;
  114. font-size: 1.05rem;
  115. letter-spacing: 0.32em;
  116. text-transform: uppercase;
  117. color: var(--midground);
  118. }}
  119. .brand .dot {{
  120. display: inline-block;
  121. width: 6px;
  122. height: 6px;
  123. background: var(--midground);
  124. margin: 0 0.55em 0.18em;
  125. vertical-align: middle;
  126. border-radius: 1px;
  127. }}
  128. .card {{
  129. position: relative;
  130. padding: 2.25rem 2rem 2rem;
  131. background: color-mix(in srgb, #ffffff 2%, var(--background-base));
  132. border: 1px solid var(--hairline);
  133. /* Hairline highlight + bevel shadow — matches DS Button SHADOW_DEFAULT
  134. (`inset -1px -1px 0 #00000080, inset 1px 1px 0 #ffffff80`) at panel scale. */
  135. box-shadow:
  136. inset 1px 1px 0 0 color-mix(in srgb, #ffffff 5%, transparent),
  137. inset -1px -1px 0 0 rgba(0, 0, 0, 0.4),
  138. 0 24px 60px -20px rgba(0, 0, 0, 0.6);
  139. }}
  140. h1 {{
  141. margin: 0 0 0.4rem;
  142. font-family: 'Rules Compressed', 'Collapse', sans-serif;
  143. font-weight: 600;
  144. font-size: 1.85rem;
  145. letter-spacing: 0.05em;
  146. text-transform: uppercase;
  147. color: var(--foreground);
  148. }}
  149. .subtitle {{
  150. margin: 0 0 1.75rem;
  151. color: color-mix(in srgb, var(--foreground) 65%, transparent);
  152. font-size: 0.95rem;
  153. }}
  154. .provider-list {{
  155. display: grid;
  156. gap: 0.75rem;
  157. }}
  158. /* Provider button — mirrors DS Button (default variant):
  159. amber surface, dark text, uppercase + wide tracking, inset bevel. */
  160. .provider-btn {{
  161. display: block;
  162. width: 100%;
  163. box-sizing: border-box;
  164. padding: 0.95rem 1rem;
  165. text-align: center;
  166. background: var(--midground);
  167. color: var(--background-base);
  168. font-family: 'Collapse', sans-serif;
  169. font-weight: 700;
  170. font-size: 0.78rem;
  171. letter-spacing: 0.2em;
  172. text-transform: uppercase;
  173. text-decoration: none;
  174. border: 0;
  175. border-radius: 0; /* DS Button is squared — no rounded corners. */
  176. cursor: pointer;
  177. box-shadow:
  178. inset 1px 1px 0 0 rgba(255, 255, 255, 0.5),
  179. inset -1px -1px 0 0 rgba(0, 0, 0, 0.5);
  180. transition: filter 0.12s ease-out;
  181. }}
  182. .provider-btn:hover {{
  183. filter: brightness(1.08);
  184. }}
  185. .provider-btn:active {{
  186. /* DS Button uses `active:invert` on the default surface. */
  187. filter: invert(1);
  188. }}
  189. .provider-btn:focus-visible {{
  190. outline: 2px solid var(--midground);
  191. outline-offset: 3px;
  192. }}
  193. /* Password provider form — same visual language as the OAuth buttons:
  194. squared inputs, hairline borders, amber focus ring. */
  195. .provider-form {{
  196. display: grid;
  197. gap: 0.75rem;
  198. text-align: left;
  199. }}
  200. .form-title {{
  201. font-family: 'Rules Compressed', 'Collapse', sans-serif;
  202. font-weight: 600;
  203. font-size: 0.72rem;
  204. letter-spacing: 0.18em;
  205. text-transform: uppercase;
  206. color: color-mix(in srgb, var(--foreground) 70%, transparent);
  207. }}
  208. .field {{
  209. display: grid;
  210. gap: 0.3rem;
  211. }}
  212. .field-label {{
  213. font-size: 0.72rem;
  214. letter-spacing: 0.12em;
  215. text-transform: uppercase;
  216. color: color-mix(in srgb, var(--foreground) 55%, transparent);
  217. }}
  218. .field-input {{
  219. width: 100%;
  220. box-sizing: border-box;
  221. padding: 0.7rem 0.8rem;
  222. background: color-mix(in srgb, #000000 25%, var(--background-base));
  223. color: var(--foreground);
  224. border: 1px solid var(--hairline-strong);
  225. border-radius: 0;
  226. font-family: 'Collapse', sans-serif;
  227. font-size: 0.95rem;
  228. }}
  229. .field-input:focus-visible {{
  230. outline: none;
  231. border-color: var(--midground);
  232. box-shadow: 0 0 0 1px var(--midground);
  233. }}
  234. .form-error {{
  235. color: #ff6b6b;
  236. font-size: 0.82rem;
  237. letter-spacing: 0.02em;
  238. }}
  239. .provider-form .provider-btn {{
  240. margin-top: 0.25rem;
  241. }}
  242. footer {{
  243. margin-top: 1.75rem;
  244. text-align: center;
  245. color: color-mix(in srgb, var(--foreground) 45%, transparent);
  246. font-size: 0.75rem;
  247. letter-spacing: 0.1em;
  248. text-transform: uppercase;
  249. line-height: 1.7;
  250. }}
  251. footer .sep {{
  252. display: inline-block;
  253. width: 1.5rem;
  254. height: 1px;
  255. background: var(--hairline-strong);
  256. vertical-align: middle;
  257. margin: 0 0.6em 0.2em;
  258. }}
  259. /* Selection — DS uses midground bg + background text. */
  260. ::selection {{
  261. background: var(--midground);
  262. color: var(--background-base);
  263. }}
  264. </style>
  265. </head>
  266. <body>
  267. <main>
  268. <div class="brand">Nous<span class="dot"></span>Research</div>
  269. <div class="card">
  270. <h1>Sign in</h1>
  271. <p class="subtitle">Choose a sign-in method to continue to the Hermes Agent dashboard.</p>
  272. <div class="provider-list">
  273. {provider_buttons}
  274. </div>
  275. </div>
  276. <footer>
  277. <span class="sep"></span>Public bind &middot; Auth required<span class="sep"></span>
  278. </footer>
  279. </main>
  280. {password_script}
  281. </body>
  282. </html>
  283. """
  284. _EMPTY_HTML = """\
  285. <!doctype html>
  286. <html lang="en">
  287. <head>
  288. <meta charset="utf-8">
  289. <meta name="viewport" content="width=device-width, initial-scale=1">
  290. <title>Sign-in unavailable — Hermes Agent</title>
  291. <style>
  292. @font-face {
  293. font-family: 'Collapse';
  294. font-style: normal;
  295. font-weight: 400;
  296. font-display: swap;
  297. src: url('/fonts/Collapse-Regular.woff2') format('woff2');
  298. }
  299. @font-face {
  300. font-family: 'Rules Compressed';
  301. font-style: normal;
  302. font-weight: 600;
  303. font-display: swap;
  304. src: url('/fonts/RulesCompressed-Medium.woff2') format('woff2');
  305. }
  306. :root {
  307. --background-base: #170d02;
  308. --midground: #ffac02;
  309. --foreground: #ffffff;
  310. --hairline: color-mix(in srgb, #ffac02 18%, transparent);
  311. }
  312. *, *::before, *::after { box-sizing: border-box; }
  313. html, body {
  314. margin: 0; padding: 0; min-height: 100%;
  315. background: var(--background-base);
  316. color: var(--foreground);
  317. font-family: 'Collapse', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  318. font-size: 16px; line-height: 1.5;
  319. -webkit-font-smoothing: antialiased;
  320. }
  321. body {
  322. display: grid; place-items: center;
  323. padding: clamp(1.5rem, 6vh, 6rem) 1.25rem;
  324. }
  325. main {
  326. width: 100%; max-width: 32rem;
  327. padding: 2.25rem 2rem;
  328. background: color-mix(in srgb, #ffffff 2%, var(--background-base));
  329. border: 1px solid var(--hairline);
  330. box-shadow:
  331. inset 1px 1px 0 0 color-mix(in srgb, #ffffff 5%, transparent),
  332. inset -1px -1px 0 0 rgba(0, 0, 0, 0.4),
  333. 0 24px 60px -20px rgba(0, 0, 0, 0.6);
  334. }
  335. h1 {
  336. margin: 0 0 1rem;
  337. font-family: 'Rules Compressed', 'Collapse', sans-serif;
  338. font-weight: 600; font-size: 1.5rem;
  339. letter-spacing: 0.05em; text-transform: uppercase;
  340. color: var(--midground);
  341. }
  342. p { margin: 0 0 1rem; }
  343. code {
  344. background: var(--midground);
  345. color: var(--background-base);
  346. padding: 0.1em 0.35em;
  347. font-family: 'Courier New', monospace;
  348. font-size: 0.9em;
  349. }
  350. a { color: var(--midground); }
  351. </style>
  352. </head>
  353. <body>
  354. <main>
  355. <h1>Sign-in unavailable</h1>
  356. <p>This dashboard is bound to a non-loopback host but no authentication
  357. providers are available.</p>
  358. <p>Configure the bundled username/password provider or an OAuth provider.
  359. See the <a href="https://hermes-agent.nousresearch.com/docs/user-guide/features/web-dashboard#authentication-gated-mode">dashboard
  360. authentication documentation</a> for setup instructions.</p>
  361. <p>For auth-free local use, bind to <code>127.0.0.1</code> and connect through
  362. an SSH tunnel or Tailscale.</p>
  363. </main>
  364. </body>
  365. </html>
  366. """
  367. # Token-gated deployment (every registered provider is provisioned-credential-only):
  368. # there is nothing to click, so the page explains the credential channel instead of
  369. # rendering an empty options list. {provider_names} / {note} are pre-escaped HTML.
  370. _TOKEN_MODE_HTML = """\
  371. <!doctype html>
  372. <html lang="en">
  373. <head>
  374. <meta charset="utf-8">
  375. <meta name="viewport" content="width=device-width, initial-scale=1">
  376. <title>Token-gated dashboard — Hermes Agent</title>
  377. <style>
  378. :root {
  379. --background-base: #170d02;
  380. --midground: #ffac02;
  381. --foreground: #ffffff;
  382. --hairline: color-mix(in srgb, #ffac02 18%, transparent);
  383. }
  384. *, *::before, *::after { box-sizing: border-box; }
  385. html, body {
  386. margin: 0; padding: 0; min-height: 100%;
  387. background: var(--background-base);
  388. color: var(--foreground);
  389. font-family: 'Collapse', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  390. font-size: 16px; line-height: 1.5;
  391. -webkit-font-smoothing: antialiased;
  392. }
  393. body {
  394. display: grid; place-items: center;
  395. padding: clamp(1.5rem, 6vh, 6rem) 1.25rem;
  396. }
  397. main {
  398. width: 100%; max-width: 32rem;
  399. padding: 2.25rem 2rem;
  400. background: color-mix(in srgb, #ffffff 2%, var(--background-base));
  401. border: 1px solid var(--hairline);
  402. box-shadow:
  403. inset 1px 1px 0 0 color-mix(in srgb, #ffffff 5%, transparent),
  404. inset -1px -1px 0 0 rgba(0, 0, 0, 0.4),
  405. 0 24px 60px -20px rgba(0, 0, 0, 0.6);
  406. }
  407. h1 {
  408. margin: 0 0 1rem;
  409. font-family: 'Rules Compressed', 'Collapse', sans-serif;
  410. font-weight: 600; font-size: 1.5rem;
  411. letter-spacing: 0.05em; text-transform: uppercase;
  412. color: var(--midground);
  413. }
  414. p { margin: 0 0 1rem; }
  415. code {
  416. background: var(--midground);
  417. color: var(--background-base);
  418. padding: 0.1em 0.35em;
  419. font-family: 'Courier New', monospace;
  420. font-size: 0.9em;
  421. }
  422. </style>
  423. </head>
  424. <body>
  425. <main>
  426. <h1>Token-gated dashboard</h1>
  427. <p>This deployment authenticates with a provisioned credential
  428. ({provider_names}), not a browser sign-in.</p>
  429. <p>{note}</p>
  430. </main>
  431. </body>
  432. </html>
  433. """
  434. # Emitted ONLY when a ``supports_password`` provider is listed, so OAuth-only
  435. # login pages stay script-free. Plain string (not ``str.format``): braces are
  436. # literal. One delegated submit handler covers every form; the provider name
  437. # comes from the form's ``data-provider`` attribute.
  438. #
  439. # Reverse-proxy prefix support: the script reads ``data-prefix`` from the
  440. # ``<main>`` element (set by :func:`render_login_html`) and prepends it
  441. # to the POST target and the post-login landing path so the login flow
  442. # works behind ``X-Forwarded-Prefix`` proxies.
  443. _PASSWORD_FORM_SCRIPT = """\
  444. <script>
  445. (function () {
  446. var mainEl = document.querySelector('main');
  447. var prefix = (mainEl && mainEl.getAttribute('data-prefix')) || '';
  448. function handle(form) {
  449. form.addEventListener('submit', function (ev) {
  450. ev.preventDefault();
  451. var err = form.querySelector('.form-error');
  452. var btn = form.querySelector('button[type=submit]');
  453. if (err) { err.hidden = true; err.textContent = ''; }
  454. if (btn) { btn.disabled = true; }
  455. var body = {
  456. provider: form.getAttribute('data-provider') || '',
  457. username: (form.querySelector('input[name=username]') || {}).value || '',
  458. password: (form.querySelector('input[name=password]') || {}).value || '',
  459. next: (form.querySelector('input[name=next]') || {}).value || ''
  460. };
  461. fetch(prefix + '/auth/password-login', {
  462. method: 'POST',
  463. headers: { 'Content-Type': 'application/json' },
  464. body: JSON.stringify(body),
  465. credentials: 'same-origin'
  466. }).then(function (resp) {
  467. if (resp.ok) {
  468. return resp.json().then(function (data) {
  469. window.location.assign((data && data.next) || (prefix + '/'));
  470. });
  471. }
  472. var msg = resp.status === 429
  473. ? 'Too many attempts. Please wait and try again.'
  474. : (resp.status === 401 ? 'Invalid username or password.'
  475. : 'Sign-in failed. Please try again.');
  476. if (err) { err.textContent = msg; err.hidden = false; }
  477. if (btn) { btn.disabled = false; }
  478. }).catch(function () {
  479. if (err) { err.textContent = 'Network error. Please try again.'; err.hidden = false; }
  480. if (btn) { btn.disabled = false; }
  481. });
  482. });
  483. }
  484. var forms = document.querySelectorAll('form.provider-form');
  485. for (var i = 0; i < forms.length; i++) { handle(forms[i]); }
  486. })();
  487. </script>
  488. """
  489. def render_login_html(*, next_path: str = "", prefix: str = "") -> str:
  490. """Return the full HTML for ``GET /login``.
  491. ``next_path`` is threaded into each provider button/form so the OAuth round
  492. trip carries it end-to-end. The caller validates it same-origin; it is
  493. HTML-escaped here as defence in depth.
  494. ``prefix`` — when set, the path prefix from ``X-Forwarded-Prefix``
  495. (e.g. ``"/hermes"``). Prepended to all auth URLs so the login page
  496. works behind a reverse proxy that mounts the dashboard at a
  497. sub-path. Empty string (default) means no prefix — bare-root
  498. deploys are unaffected.
  499. """
  500. providers = list_session_providers()
  501. if not providers:
  502. return _EMPTY_HTML
  503. # URL-encode then HTML-escape, matching the gate's ``_safe_next_target``
  504. # shape so a round-tripped value is byte-identical.
  505. next_qs = f"&next={html.escape(quote(next_path, safe=''), quote=True)}" if next_path else ""
  506. interactive = list_interactive_providers()
  507. if not interactive:
  508. # A deployment authenticated by a provisioned credential (e.g. the
  509. # session-token provider): there is nothing to click. Say so instead of
  510. # rendering a page with no sign-in options.
  511. names = ", ".join(html.escape(p.display_name) for p in providers)
  512. # str.replace, not str.format: the template's CSS braces are literal.
  513. return (_TOKEN_MODE_HTML
  514. .replace("{provider_names}", names)
  515. .replace("{note}",
  516. "Clients authenticate with the deployment&rsquo;s provisioned "
  517. "credential (<code>Authorization: Bearer</code>, or "
  518. "<code>?token=</code> on the /api/ws upgrade) &mdash; e.g. Hermes "
  519. "Desktop&rsquo;s &ldquo;Session token&rdquo; remote-gateway field. "
  520. "There is no browser sign-in flow."))
  521. buttons = [
  522. _render_password_form(p, next_path, prefix=prefix)
  523. if getattr(p, "supports_password", False) else
  524. f' <a class="provider-btn" '
  525. f'href="{prefix}/auth/login?provider={html.escape(p.name, quote=True)}{next_qs}">'
  526. f'Sign in with {html.escape(p.display_name)}</a>'
  527. for p in interactive
  528. ]
  529. needs_password_script = any(getattr(p, "supports_password", False) for p in interactive)
  530. html_out = _LOGIN_HTML_TEMPLATE.format(
  531. provider_buttons="\n".join(buttons),
  532. password_script=_PASSWORD_FORM_SCRIPT if needs_password_script else "",
  533. )
  534. # Inject the reverse-proxy prefix so the password-login JS can read
  535. # it from the <main> element's data attribute, and rewrite the font
  536. # URLs to stay inside the mount. ``str.replace`` after ``str.format``
  537. # avoids interfering with the CSS ``{{ }}`` doubling in the template.
  538. return _apply_proxy_prefix(html_out, prefix)
  539. def _apply_proxy_prefix(html_out: str, prefix: str) -> str:
  540. """Rewrite a rendered login-family page for a reverse-proxy sub-path.
  541. Two rewrites, both no-ops when *prefix* is empty (bare-root deploys are
  542. byte-for-byte unaffected):
  543. * ``<main>`` gains ``data-prefix`` so the password-login JS (a plain
  544. string, never ``str.format``-aware) can prepend the prefix to its
  545. fetch target and post-login landing.
  546. * ``@font-face`` ``url('/fonts/...')`` becomes ``url('<prefix>/fonts/...')``
  547. so the fonts load from the mount instead of the origin root (404
  548. behind a prefix-stripping proxy).
  549. """
  550. if not prefix:
  551. return html_out
  552. html_out = html_out.replace("<main>", f'<main data-prefix="{prefix}">', 1)
  553. html_out = html_out.replace("url('/fonts/", f"url('{prefix}/fonts/")
  554. return html_out
  555. def render_native_provider_choice_html(
  556. *, providers, authorize_path: str, code_challenge: str,
  557. code_challenge_method: str, redirect_uri: str, state: str,
  558. prefix: str = "") -> str:
  559. """Provider picker for a native authorize request with more than one interactive provider.
  560. Every link re-enters ``/auth/native/authorize`` with the SAME desktop PKCE inputs plus an
  561. explicit ``provider``, so the choice never leaves the validated native flow. ``prefix``
  562. (from ``X-Forwarded-Prefix``) rewrites the template's font URLs so the page works behind
  563. a reverse-proxy sub-path; the buttons' ``authorize_path`` is already prefixed by the caller.
  564. """
  565. common = {"code_challenge": code_challenge, "code_challenge_method": code_challenge_method,
  566. "redirect_uri": redirect_uri, "state": state}
  567. buttons = []
  568. for p in providers:
  569. href = html.escape(f"{authorize_path}?{urlencode({**common, 'provider': p.name})}",
  570. quote=True)
  571. buttons.append(f' <a class="provider-btn" href="{href}">'
  572. f'Sign in with {html.escape(p.display_name)}</a>')
  573. if not buttons:
  574. return _EMPTY_HTML
  575. return _apply_proxy_prefix(
  576. _LOGIN_HTML_TEMPLATE.format(
  577. provider_buttons="\n".join(buttons), password_script=""),
  578. prefix)
  579. def _render_password_form(provider, next_path: str, *, prefix: str = "") -> str:
  580. """Username/password form for a ``supports_password`` provider.
  581. ``next_path`` rides in a hidden field (already validated by the caller,
  582. HTML-escaped here). The provider name is a ``data-`` attribute so the
  583. script does not depend on field ordering.
  584. ``prefix`` — reverse-proxy path prefix (e.g. ``"/hermes"``); kept for
  585. interface parity with the other renderers (the submit handler reads
  586. the prefix from ``<main data-prefix>``).
  587. """
  588. pname = html.escape(provider.name, quote=True)
  589. plabel = html.escape(provider.display_name)
  590. safe_next = html.escape(next_path, quote=True) if next_path else ""
  591. return (
  592. f' <form class="provider-form" data-provider="{pname}" '
  593. f'autocomplete="on">\n'
  594. f' <div class="form-title">Sign in with {plabel}</div>\n'
  595. f' <input type="hidden" name="next" value="{safe_next}">\n'
  596. f' <label class="field">\n'
  597. f' <span class="field-label">Username</span>\n'
  598. f' <input class="field-input" type="text" name="username" '
  599. f'autocomplete="username" autocapitalize="none" '
  600. f'autocorrect="off" spellcheck="false" required>\n'
  601. f' </label>\n'
  602. f' <label class="field">\n'
  603. f' <span class="field-label">Password</span>\n'
  604. f' <input class="field-input" type="password" name="password" '
  605. f'autocomplete="current-password" required>\n'
  606. f' </label>\n'
  607. f' <div class="form-error" role="alert" hidden></div>\n'
  608. f' <button class="provider-btn" type="submit">Sign in</button>\n'
  609. f' </form>'
  610. )