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