-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::{flow::pkce, primitive::Client, scope::Scope}; | ||
use std::borrow::Cow; | ||
|
||
#[derive(Clone)] | ||
pub struct Authorization<'a> { | ||
pub code: Cow<'a, str>, | ||
pub client: Client<'a>, | ||
pub pkce_payload: Option<pkce::Payload<'a>>, | ||
pub scopes: Scope, | ||
pub user_id: Cow<'a, str>, | ||
} | ||
|
||
impl Authorization<'_> { | ||
pub fn into_owned(self) -> Authorization<'static> { | ||
Authorization { | ||
code: self.code.into_owned().into(), | ||
client: self.client.into_owned(), | ||
pkce_payload: self.pkce_payload.map(pkce::Payload::into_owned), | ||
scopes: self.scopes, | ||
user_id: self.user_id.into_owned().into(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::scope::Scope; | ||
use std::{borrow::Cow, fmt}; | ||
use subtle::ConstantTimeEq; | ||
|
||
#[derive(Clone)] | ||
pub struct Client<'a> { | ||
pub client_id: Cow<'a, str>, | ||
pub client_secret: Cow<'a, str>, | ||
pub scopes: Scope, | ||
pub redirect_uri: Cow<'a, str>, | ||
} | ||
|
||
impl Client<'_> { | ||
#[must_use] | ||
pub fn into_owned(self) -> Client<'static> { | ||
Client { | ||
client_id: self.client_id.into_owned().into(), | ||
client_secret: self.client_secret.into_owned().into(), | ||
scopes: self.scopes, | ||
redirect_uri: self.redirect_uri.into_owned().into(), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for Client<'_> { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct(std::any::type_name::<Self>()) | ||
.field("client_id", &self.client_id) | ||
.field("client_secret", &"[redacted]") | ||
.field("scopes", &self.scopes) | ||
.field("redirect_uri", &self.redirect_uri) | ||
.finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl PartialEq for Client<'_> { | ||
#[inline] | ||
fn eq(&self, other: &Self) -> bool { | ||
let client_id_l = self.client_id.as_bytes(); | ||
let client_id_r = other.client_id.as_bytes(); | ||
|
||
let client_secret_l = self.client_secret.as_bytes(); | ||
let client_secret_r = other.client_secret.as_bytes(); | ||
|
||
(client_id_l.ct_eq(client_id_r) & client_secret_l.ct_eq(client_secret_r)).into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub use self::{authorization::Authorization, client::Client, request::Request}; | ||
|
||
mod authorization; | ||
mod client; | ||
mod request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::{ | ||
error::{BoxError, Error}, | ||
params::ParamStorage, | ||
}; | ||
use http_body_util::BodyExt; | ||
use std::borrow::Cow; | ||
|
||
pub struct Request<'a> { | ||
pub headers: http::HeaderMap, | ||
pub query: ParamStorage<Cow<'a, str>, Cow<'a, str>>, | ||
pub body: ParamStorage<Cow<'a, str>, Cow<'a, str>>, | ||
} | ||
|
||
impl Request<'_> { | ||
#[inline] | ||
#[instrument(skip_all)] | ||
pub async fn read_from<B>(req: http::Request<B>) -> Result<Self, Error> | ||
where | ||
B: http_body::Body, | ||
B::Error: Into<BoxError>, | ||
{ | ||
let raw_query = req.uri().query().unwrap_or(""); | ||
let query = serde_urlencoded::from_str(raw_query).map_err(Error::query)?; | ||
|
||
let (parts, body) = req.into_parts(); | ||
let collected = body.collect().await.map_err(Error::body)?.to_bytes(); | ||
let req = http::Request::from_parts(parts, collected.clone()); | ||
|
||
let body = if collected.is_empty() { | ||
ParamStorage::default() | ||
} else { | ||
crate::extract::body(&req).map_err(Error::body)? | ||
}; | ||
|
||
Ok(Self { | ||
headers: req.headers().clone(), | ||
query, | ||
body, | ||
}) | ||
} | ||
} |