Skip to main content

aoc_toolkit/cli/output/
auth.rs

1use crate::application::auth::{AuthStatus, SessionSource};
2
3pub(crate) const MANUAL_LOGIN_HELP: &str = "\
4Manual authentication:
5  1. Open https://adventofcode.com and sign in to your account.
6  2. Open your browser's developer tools.
7  3. Locate cookies for adventofcode.com
8     (Application in Chromium, Storage in Firefox).
9  4. Copy the value of your own session cookie.
10  5. Paste it into the secure terminal prompt.
11
12Never share your session token or commit it to version control.
13The token is stored in your system credential store.";
14
15pub(crate) fn print_login_instructions() {
16    println!("{MANUAL_LOGIN_HELP}");
17    println!();
18}
19
20pub(crate) fn print_login_success() {
21    println!("✓ Session stored securely.");
22}
23
24pub(crate) fn print_auth_status(status: AuthStatus) {
25    match status {
26        AuthStatus::Authenticated(source) => {
27            println!("✓ Authenticated with Advent of Code.");
28
29            println!("  Source: {}", session_source_label(source));
30        }
31
32        AuthStatus::Expired(source) => {
33            println!("✗ Session is no longer valid.");
34
35            println!("  Source: {}", session_source_label(source));
36
37            match source {
38                SessionSource::Environment => {
39                    println!("Update or unset SESSION_ID.");
40                }
41
42                SessionSource::Dotenv => {
43                    println!("Update or remove SESSION_ID in .env.");
44                }
45
46                SessionSource::CredentialStore => {
47                    println!("Run `aoc auth login`.");
48                }
49            }
50        }
51
52        AuthStatus::Missing => {
53            println!("No session configured.");
54            println!("Run `aoc auth login`.");
55        }
56    }
57}
58
59pub(crate) fn print_logout_success() {
60    println!("✓ Stored session removed.");
61    println!("SESSION_ID overrides are not affected.");
62}
63
64pub(super) fn session_source_label(source: SessionSource) -> &'static str {
65    match source {
66        SessionSource::Environment => "SESSION_ID",
67        SessionSource::Dotenv => ".env",
68        SessionSource::CredentialStore => "System credential store",
69    }
70}