#[macro_use]
extern crate log;
#[macro_use]
extern crate failure;
use std::fmt;
#[cfg(feature = "async")]
pub mod async_client;
pub mod client;
mod error;
pub mod metrics;
pub mod parsers;
pub mod token_manager;
pub use error::{TokenInfoError, TokenInfoErrorKind, TokenInfoResult};
#[derive(Clone)]
pub struct AccessToken(pub String);
impl AccessToken {
pub fn new<T: Into<String>>(token: T) -> Self {
AccessToken(token.into())
}
}
impl fmt::Display for AccessToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<secret-access-token>")
}
}
impl fmt::Debug for AccessToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AccessToken(<secret>)")
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct Scope(pub String);
impl Scope {
pub fn new<T: Into<String>>(scope: T) -> Scope {
Scope(scope.into())
}
}
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
pub trait TokenInfoService {
fn introspect(&self, token: &AccessToken) -> TokenInfoResult<TokenInfo>;
}
pub type InitializationResult<T> = ::std::result::Result<T, InitializationError>;
#[derive(Debug, Fail)]
#[fail(display = "{}", _0)]
pub struct InitializationError(pub String);
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct UserId(pub String);
impl UserId {
pub fn new<T: Into<String>>(uid: T) -> UserId {
UserId(uid.into())
}
}
impl fmt::Display for UserId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, PartialEq)]
pub struct TokenInfo {
pub active: bool,
pub user_id: Option<UserId>,
pub scope: Vec<Scope>,
pub expires_in_seconds: Option<u64>,
}
impl TokenInfo {
pub fn has_scope(&self, scope: &Scope) -> bool {
self.scope.iter().any(|s| s == scope)
}
pub fn has_scopes(&self, scopes: &[Scope]) -> bool {
scopes.iter().all(|scope| self.has_scope(scope))
}
pub fn must_have_scope(&self, scope: &Scope) -> ::std::result::Result<(), NotAuthorized> {
if self.has_scope(scope) {
Ok(())
} else {
Err(NotAuthorized(format!(
"Required scope '{}' not present.",
scope
)))
}
}
}
#[derive(Debug, Fail)]
pub struct NotAuthorized(pub String);
impl NotAuthorized {
pub fn new<T: Into<String>>(msg: T) -> NotAuthorized {
NotAuthorized(msg.into())
}
}
impl fmt::Display for NotAuthorized {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Not authorized: {}", self.0)
}
}