Skip to content

Commit

Permalink
Implement Display for Token
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsSR committed Nov 27, 2023
1 parent 4e1fd80 commit 3d8e3d5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ impl Token {
}

/// Returns the token as a string.
pub fn to_string(&self) -> String {
self.token.clone()
}
// pub fn to_string(&self) -> String {
// self.token.clone()
// }
/// Extracts the token as a string slice.
///
/// # Examples
Expand Down Expand Up @@ -193,6 +193,12 @@ impl Token {
}
}

impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.token)
}
}

#[derive(Debug)]
pub enum TokenError {
InvalidToken,
Expand Down Expand Up @@ -239,15 +245,16 @@ impl From<TokenError> for Status {
}
}

/// This trait is used to add auth related methods to the tonic request.
pub trait RequestTrait {
/// An extension trait for [Request]`s that provides a variety of convenient
/// auth related methods.
pub trait RequestExt {
fn token_string(&self) -> Option<String>;
fn token_str(&self) -> Option<&str>;

fn uid(&self) -> Option<i32>;
}

impl<T> RequestTrait for Request<T> {
impl<T> RequestExt for Request<T> {
/// Returns the token string from the request metadata.
fn token_string(&self) -> Option<String> {
self.metadata().get("authorization").map(|token| {
Expand Down
2 changes: 1 addition & 1 deletion src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::api::auth::{RequestTrait, Token, TokenType};
use crate::api::auth::{RequestExt, Token, TokenType};
use crate::api::server::server::get_auth_token_request::user_credentials;
use crate::entities::session::Model;
use chrono::Local;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/api/auth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
mod auth {
use crate::api::auth::{RequestTrait, Token, TokenType};
use crate::api::auth::{RequestExt, Token, TokenType};
use std::{env, str::FromStr};
use tonic::{metadata::MetadataValue, Request};

Expand Down

0 comments on commit 3d8e3d5

Please sign in to comment.