Skip to content

Commit

Permalink
squashme: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Jun 23, 2024
1 parent 3fce588 commit eaea30d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub async fn command(args: Args) -> anyhow::Result<()> {
.await
.context("Failed to get token")?;

println!("auth token:\n{}", auth_token.signature);

let res = client
.post(format!("{}/test-auth", get_api_url()))
.header(header::AUTHORIZATION, format!("Bearer {}", auth_token))
Expand Down
36 changes: 29 additions & 7 deletions src/utils/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use anyhow::{anyhow, Context};
use chrono::Utc;
use pgp::composed::message::Message;
use pgp::{crypto, Deserializable, SignedSecretKey};
use serde::{Deserialize, Serialize};

use super::keyring::try_get_password;

pub async fn get_token(
fingerprint: &str,
token: &str,
) -> anyhow::Result<String> {
) -> anyhow::Result<AuthToken> {
let config = get_config().context("Failed to get config")?;
let key = config
.keys
Expand Down Expand Up @@ -54,12 +55,33 @@ pub async fn get_token(
));
}
};

let auth_token = serde_json::json!({
"token": token,
"signature": signature,
})
.to_string();
let auth_token = AuthToken::new(token.into(), signature);

Ok(auth_token)
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthToken {
pub token: String,
pub signature: String,
}

impl AuthToken {
pub fn new(token: String, signature: String) -> Self {
Self { token, signature }
}
}

impl From<AuthToken> for String {
fn from(auth_token: AuthToken) -> String {
serde_json::to_string(&auth_token).unwrap()
}
}

impl std::fmt::Display for AuthToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let string_token = serde_json::to_string(&self).unwrap();

write!(f, "{}", string_token)
}
}

0 comments on commit eaea30d

Please sign in to comment.