Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jan 15, 2025
1 parent f13f3c3 commit 951dcc8
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 110 deletions.
116 changes: 6 additions & 110 deletions lib/komainu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#[macro_use]
extern crate tracing;

use self::{
error::{BoxError, Error},
flow::pkce,
params::ParamStorage,
scope::Scope,
};
use http_body_util::BodyExt;
use std::{borrow::Cow, fmt, future::Future};
use subtle::ConstantTimeEq;
use self::{error::Error, flow::pkce, scope::Scope};
use std::future::Future;

pub use self::primitive::{Authorization, Client, Request};

mod primitive;

pub mod code_grant;
pub mod error;
Expand All @@ -18,113 +15,12 @@ pub mod flow;
pub mod params;
pub mod scope;

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,
})
}
}

#[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(),
}
}
}

pub struct AuthInstruction<'a, 'b> {
pub client: &'b Client<'a>,
pub pkce_payload: Option<&'b pkce::Payload<'a>>,
pub scopes: &'b Scope,
}

#[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()
}
}

pub trait ClientExtractor {
fn extract(
&self,
Expand Down
23 changes: 23 additions & 0 deletions lib/komainu/src/primitive/authorization.rs
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(),
}
}
}
48 changes: 48 additions & 0 deletions lib/komainu/src/primitive/client.rs
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()
}
}
5 changes: 5 additions & 0 deletions lib/komainu/src/primitive/mod.rs
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;
41 changes: 41 additions & 0 deletions lib/komainu/src/primitive/request.rs
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,
})
}
}

0 comments on commit 951dcc8

Please sign in to comment.