Skip to main content

hyprstream_core/services/oauth/
login_page.rs

1//! Provider selection login page for the OAuth authorize flow.
2//!
3//! Renders an HTML page listing available authentication methods:
4//! external OIDC providers and local Ed25519 challenge-response.
5
6/// Render the login page with provider selection.
7///
8/// `providers` is a list of `(slug, display_name)` pairs for external OIDC providers.
9/// `authorize_query` is the original authorize query string to pass through.
10pub fn render_login_page(
11    client_name: &str,
12    scopes: &str,
13    redirect_host: &str,
14    authorize_query: &str,
15    providers: &[(String, String)],
16    has_local_auth: bool,
17) -> String {
18    let mut provider_buttons = String::new();
19    for (slug, display_name) in providers {
20        provider_buttons.push_str(&format!(
21            r#"<a class="provider-btn" href="/oauth/external/authorize/{slug}?{query}">
22                Sign in with {display_name}
23            </a>"#,
24            slug = html_escape(slug),
25            query = authorize_query,
26            display_name = html_escape(display_name),
27        ));
28    }
29
30    let local_section = if has_local_auth {
31        let collapsed = if providers.is_empty() {
32            ""
33        } else {
34            "collapsed"
35        };
36        format!(
37            r#"<details class="local-auth {collapsed}" {open}>
38                <summary>Sign in with Ed25519 key</summary>
39                <p>Run the following command and paste the result:</p>
40                <code>hyprstream sign-challenge --nonce &lt;nonce&gt; --code-challenge &lt;cc&gt;</code>
41                <p><a href="/oauth/authorize?{query}&prompt=local">Use Ed25519 challenge form</a></p>
42            </details>"#,
43            collapsed = collapsed,
44            open = if providers.is_empty() { "open" } else { "" },
45            query = authorize_query,
46        )
47    } else {
48        String::new()
49    };
50
51    format!(
52        r#"<!DOCTYPE html>
53<html lang="en">
54<head>
55  <meta charset="UTF-8">
56  <meta name="viewport" content="width=device-width, initial-scale=1.0">
57  <title>Sign In — Hyprstream</title>
58  <style>
59    body {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
60           max-width: 480px; margin: 80px auto; padding: 0 20px; color: #333; }}
61    h1 {{ font-size: 1.5em; text-align: center; }}
62    .info {{ color: #666; font-size: 0.9em; text-align: center; margin-bottom: 2em; }}
63    .provider-btn {{ display: block; padding: 12px 20px; margin: 10px 0;
64                     background: #2563eb; color: white; text-align: center;
65                     text-decoration: none; border-radius: 6px; font-size: 1em; }}
66    .provider-btn:hover {{ background: #1d4ed8; }}
67    .local-auth {{ margin-top: 2em; border-top: 1px solid #e5e7eb; padding-top: 1em; }}
68    .local-auth summary {{ cursor: pointer; color: #666; }}
69    .local-auth code {{ display: block; background: #f3f4f6; padding: 8px 12px;
70                        border-radius: 4px; margin: 8px 0; font-size: 0.85em;
71                        word-break: break-all; }}
72    .local-auth a {{ color: #2563eb; }}
73  </style>
74</head>
75<body>
76  <h1>Sign In</h1>
77  <div class="info">
78    <strong>{client_name}</strong> at {redirect_host} is requesting access.<br>
79    Scopes: {scopes}
80  </div>
81  {provider_buttons}
82  {local_section}
83</body>
84</html>"#,
85        client_name = html_escape(client_name),
86        redirect_host = html_escape(redirect_host),
87        scopes = html_escape(scopes),
88        provider_buttons = provider_buttons,
89        local_section = local_section,
90    )
91}
92
93fn html_escape(s: &str) -> String {
94    s.replace('&', "&amp;")
95        .replace('<', "&lt;")
96        .replace('>', "&gt;")
97        .replace('"', "&quot;")
98        .replace('\'', "&#39;")
99}