From 2266172ff06edd7dadb3d341c5460beb042d1407 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 20 Mar 2022 21:20:18 -0700 Subject: Migrate to actix-web 4 and jesterpm-sso This upgrades the package to actix-web 4, allowing me to replace the old-style indieauth token validation with standard OAuth2 Token Introspection. --- src/oauth.rs | 85 ------------------------------------------------------------ 1 file changed, 85 deletions(-) delete mode 100644 src/oauth.rs (limited to 'src/oauth.rs') diff --git a/src/oauth.rs b/src/oauth.rs deleted file mode 100644 index 4d9bd1e..0000000 --- a/src/oauth.rs +++ /dev/null @@ -1,85 +0,0 @@ -use actix_web::client::Client; -use actix_web::error::Error; -use actix_web::http::{header, StatusCode}; -use actix_web::ResponseError; -use derive_more::Display; -use futures::{FutureExt, TryFutureExt}; -use serde::{Deserialize, Serialize}; - -/// Representation of an OAuth Access Token -#[derive(Serialize, Deserialize)] -pub struct AccessToken { - me: String, - client_id: String, - scope: String, -} - -impl AccessToken { - pub fn me(&self) -> &str { - &self.me - } - - pub fn client_id(&self) -> &str { - &self.client_id - } - - pub fn scopes(&self) -> impl Iterator + '_ { - self.scope.split_ascii_whitespace() - } -} - -/// Verification Service takes an Authorization header and checks if it's valid. -pub struct VerificationService { - token_endpoint: String, - client: Client, -} - -impl VerificationService { - pub fn new(token_endpoint: S) -> VerificationService - where - S: Into, - { - VerificationService { - token_endpoint: token_endpoint.into(), - client: Client::new(), - } - } - - pub async fn validate(&self, auth_token: &str) -> Result { - self.client - .get(&self.token_endpoint) - .header(header::AUTHORIZATION, auth_token) - .send() - .map_err(Error::from) - .map(|res| { - res.and_then(|r| { - if r.status().is_success() { - Ok(r) - } else if r.status() == StatusCode::UNAUTHORIZED { - Err(VerificationError::Unauthenticated.into()) - } else { - Err(VerificationError::InternalError( - r.status() - .canonical_reason() - .unwrap_or("Unknown Error") - .to_string(), - ) - .into()) - } - }) - }) - .map_err(Error::from) - .and_then(|mut resp| resp.json().map_err(Error::from)) - .await - } -} - -#[derive(Display, Debug)] -pub enum VerificationError { - #[display(fmt = "Unauthenticated")] - Unauthenticated, - #[display(fmt = "AuthServer Error")] - InternalError(String), -} - -impl ResponseError for VerificationError {} -- cgit v1.2.3