use secrecy::{ExposeSecret, SecretString};
use std::fmt;
#[derive(Clone, Debug)]
pub struct AuthToken(SecretString);
impl AuthToken {
#[must_use]
pub const fn new(token: SecretString) -> Self {
Self(token)
}
#[must_use]
pub fn expose(&self) -> &str {
self.0.expose_secret()
}
}
impl From<SecretString> for AuthToken {
fn from(value: SecretString) -> Self {
Self::new(value)
}
}
impl fmt::Display for AuthToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<redacted token>")
}
}
pub trait TokenProvider: Send + Sync {
fn personal_access_token(&self) -> SecretString;
}
#[derive(Clone, Debug)]
pub struct StaticTokenProvider {
token: AuthToken,
}
impl StaticTokenProvider {
#[must_use]
pub const fn new(token: AuthToken) -> Self {
Self { token }
}
}
impl TokenProvider for StaticTokenProvider {
fn personal_access_token(&self) -> SecretString {
SecretString::new(self.token.expose().to_owned().into())
}
}
impl From<AuthToken> for StaticTokenProvider {
fn from(token: AuthToken) -> Self {
Self::new(token)
}
}