codeberg_cli/actions/auth/
login.rs

1use std::fs::Permissions;
2use std::os::unix::fs::PermissionsExt;
3use std::path::PathBuf;
4
5use crate::actions::GlobalArgs;
6use crate::client::BergClient;
7use crate::paths::token_path;
8use crate::render::spinner::spin_until_ready;
9use crate::render::ui::confirm_with_prompt;
10use crate::types::config::BergConfig;
11use crate::types::token::Token;
12use anyhow::Context;
13use inquire::CustomUserError;
14use inquire::validator::Validation;
15
16use crate::actions::text_manipulation::input_prompt_for;
17
18use clap::Parser;
19
20/// Login via generating authentication token
21#[derive(Parser, Debug)]
22pub struct LoginArgs {
23    /// Access Token
24    #[arg(short, long)]
25    pub token: Option<String>,
26}
27
28impl LoginArgs {
29    pub async fn run(self, _global_args: GlobalArgs) -> anyhow::Result<()> {
30        let config = BergConfig::new()?;
31        let token = self
32            .token
33            .map(Token)
34            .map(Ok)
35            .unwrap_or_else(prompt_for_token)?;
36
37        // verify
38        spin_until_ready(verify_setup(&token)).await?;
39
40        // this is where the token gets stored
41        let token_path = create_token_storage_path(config.base_url)?;
42
43        // save the token
44        std::fs::write(token_path.as_path(), token.as_str()).context("Failed to save token")?;
45        // make the token secret
46        std::fs::set_permissions(token_path.as_path(), Permissions::from_mode(0o0600))?;
47
48        Ok(())
49    }
50}
51
52fn prompt_for_token() -> anyhow::Result<Token> {
53    let config = BergConfig::new()?;
54    let url = config.url()?;
55    let token_generation_url = url.join("/user/settings/applications")?;
56    let token_generation_url = token_generation_url.as_str();
57    // ask for usage of browser
58    if confirm_with_prompt("Authenticating. Open Browser to generate token for `berg`?")? {
59        println!(
60            "\nOpening {token_generation_url:?} in the browser.\n\nPlease log in, generate a token and provide it after the following prompt:\n\n"
61        );
62        webbrowser::open(token_generation_url)?;
63    } else {
64        println!(
65            "\nYou chose not to authenticate via browser. Visit\n\n\t{}\n\nto generate a token.\n",
66            token_generation_url
67        );
68    }
69
70    // get token from user
71    let token = ask_for_token()?;
72
73    Ok(token)
74}
75
76async fn verify_setup(token: &Token) -> anyhow::Result<()> {
77    let config = BergConfig::new()?;
78    let base_url = config.url()?;
79    let client = BergClient::new(token, base_url).context("Couldn't create `berg` client.")?;
80
81    _ = client.user_get_current().await.map_err(|e| {
82        anyhow::anyhow!("Verification API call didn't contain expected information.\n\n{e}")
83    })?;
84
85    println!("\nAuthentication success!");
86
87    Ok(())
88}
89
90fn create_token_storage_path(instance: impl AsRef<str>) -> anyhow::Result<PathBuf> {
91    let token_path = token_path(instance)?;
92    let token_dir = token_path
93        .parent()
94        .context("Parent directory of token path '{token_path}' should exist")?;
95    std::fs::create_dir_all(token_dir)
96        .context("Couldn't create directory for saving the token.")?;
97    Ok(token_path)
98}
99
100fn validate_token(input: &str) -> Result<Validation, CustomUserError> {
101    let v = validate_word_count(input);
102    if let Validation::Invalid(_) = v {
103        return Ok(v);
104    }
105    Ok(validate_token_length(input))
106}
107
108fn validate_word_count(input: &str) -> Validation {
109    let words = input.split_whitespace().collect::<Vec<_>>();
110    if words.len() != 1 {
111        Validation::Invalid(
112            format!(
113                "Token is just one word. Your input words were\n{}",
114                words
115                    .iter()
116                    .map(|word| format!("  - {word}"))
117                    .collect::<Vec<_>>()
118                    .join("\n")
119            )
120            .into(),
121        )
122    } else {
123        Validation::Valid
124    }
125}
126
127fn validate_token_length(token: &str) -> Validation {
128    if token.len() != 40 {
129        Validation::Invalid(
130            format!(
131                "Usual token length is 40. Token\n\n\t{token:?}\n\nhas length {}",
132                token.len()
133            )
134            .into(),
135        )
136    } else {
137        Validation::Valid
138    }
139}
140
141fn ask_for_token() -> anyhow::Result<Token> {
142    inquire::Text::new(input_prompt_for("Token").as_str())
143        .with_validator(validate_token)
144        .prompt()
145        .map(Token::new)
146        .map_err(anyhow::Error::from)
147}