Skip to main content

subspace_auth_return/
lib.rs

1//! Fixed browser return copy and artwork, shared by identity providers.
2//! Only the caller knows when an exchange has actually completed.
3
4use base64::Engine as _;
5use base64::engine::general_purpose::STANDARD;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum State {
9    Completing,
10    Succeeded,
11    Cancelled,
12    TimedOut,
13    Failed,
14    NotFound,
15}
16
17/// No provider messages, codes or tokens can enter this page.
18pub fn page(state: State) -> String {
19    let (title, message) = copy(state);
20    include_str!("page.html")
21        .replace("{{title}}", title)
22        .replace("{{message}}", message)
23        .replace(
24            "{{subspace_logo}}",
25            &STANDARD.encode(include_bytes!("../assets/subspace.svg")),
26        )
27}
28
29fn copy(state: State) -> (&'static str, &'static str) {
30    match state {
31        State::Completing => (
32            "Completing sign-in",
33            "Subspace is checking your account. Keep Subspace open.",
34        ),
35        State::Succeeded => (
36            "Signed in to Subspace",
37            "You can close this tab and return to Subspace.",
38        ),
39        State::Cancelled => (
40            "Sign-in cancelled",
41            "Your account has not changed. Return to Subspace when you are ready to try again.",
42        ),
43        State::TimedOut => (
44            "Sign-in timed out",
45            "Return to Subspace and choose Sign in again.",
46        ),
47        State::Failed => (
48            "Sign-in did not finish",
49            "Return to Subspace to see why and try again.",
50        ),
51        State::NotFound => (
52            "Not found",
53            "This address only completes a Subspace sign-in.",
54        ),
55    }
56}
57
58/// Fixed script data for a streamed return page. The nonce belongs to the
59/// HTTP response, never to the OAuth exchange. No request value is rendered.
60pub fn completion_script(state: State) -> String {
61    let (title, message) = copy(state);
62    format!(
63        "document.title={title};document.querySelector('h1').textContent={title};document.querySelector('main p').textContent={message};",
64        title = serde_json::json!(title),
65        message = serde_json::json!(message)
66    )
67}
68
69#[cfg(test)]
70mod tests {
71    use super::{State, page};
72
73    #[test]
74    fn every_outcome_uses_the_same_self_contained_branded_page() {
75        for state in [
76            State::Completing,
77            State::Succeeded,
78            State::Cancelled,
79            State::TimedOut,
80            State::Failed,
81            State::NotFound,
82        ] {
83            let html = page(state);
84            assert!(html.contains("alt=\"Subspace\""));
85            assert!(!html.contains("{{") && !html.contains("src=\"http"));
86            assert_eq!(html.matches("src=\"data:image/svg+xml;base64,").count(), 1);
87        }
88        assert!(!page(State::Completing).contains("Signed in to Subspace"));
89    }
90}