Skip to main content

forest/cli/subcommands/
auth_cmd.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::rpc::{self, auth::AuthNewParams, prelude::*};
5use chrono::Duration;
6use clap::Subcommand;
7
8use super::print_rpc_res_bytes;
9
10#[derive(Debug, Subcommand)]
11pub enum AuthCommands {
12    /// Create a new Authentication token with given permission
13    CreateToken {
14        /// Permission to assign to the token, one of: read, write, sign, admin
15        #[arg(short, long)]
16        perm: String,
17        /// Token is revoked after this duration
18        #[arg(long, default_value = "2 months")]
19        expire_in: humantime::Duration,
20    },
21    /// Get RPC API Information
22    ApiInfo {
23        /// permission to assign the token, one of: read, write, sign, admin
24        #[arg(short, long)]
25        perm: String,
26        /// Token is revoked after this duration
27        #[arg(long, default_value = "2 months")]
28        expire_in: humantime::Duration,
29    },
30}
31
32impl AuthCommands {
33    pub async fn run(self, client: rpc::Client) -> anyhow::Result<()> {
34        match self {
35            Self::CreateToken { perm, expire_in } => {
36                let perm: String = perm.parse()?;
37                let perms = AuthNewParams::process_perms(perm)?;
38                let token_exp = Duration::from_std(expire_in.into())?;
39                let res = AuthNew::call(&client, AuthNewParams { perms, token_exp }.into()).await?;
40                print_rpc_res_bytes(res)
41            }
42            Self::ApiInfo { perm, expire_in } => {
43                let perm: String = perm.parse()?;
44                let perms = AuthNewParams::process_perms(perm)?;
45                let token_exp = Duration::from_std(expire_in.into())?;
46                let token = String::from_utf8(
47                    AuthNew::call(&client, AuthNewParams { perms, token_exp }.into()).await?,
48                )?;
49                let addr = multiaddr::from_url(client.base_url().as_str())?;
50                println!("FULLNODE_API_INFO=\"{token}:{addr}\"");
51                Ok(())
52            }
53        }
54    }
55}