From 84f26898b3e3ebdd3b7192cfecb1e99d11eae3ff Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 13 Sep 2020 14:43:35 -0700 Subject: Initial commit of media endpoint --- src/oauth.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/oauth.rs (limited to 'src/oauth.rs') diff --git a/src/oauth.rs b/src/oauth.rs new file mode 100644 index 0000000..8707b1a --- /dev/null +++ b/src/oauth.rs @@ -0,0 +1,53 @@ +use futures::{FutureExt, TryFutureExt}; +use reqwest::header; +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: reqwest::Client, +} + +impl VerificationService { + pub fn new(token_endpoint: S) -> VerificationService + where + S: Into, + { + VerificationService { + token_endpoint: token_endpoint.into(), + client: reqwest::Client::new(), + } + } + + pub async fn validate(&self, auth_token: &str) -> Result { + self.client + .get(&self.token_endpoint) + .header(header::AUTHORIZATION, auth_token) + .send() + .map(|res| res.and_then(|r| r.error_for_status())) + .and_then(|resp| resp.json()) + .await + } +} -- cgit v1.2.3