Skip to content

Commit

Permalink
rename fns
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 19, 2024
1 parent fead698 commit cf16c27
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/komainu/benches/pkce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::borrow::Cow;
static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system();

#[divan::bench]
fn s256() -> Result<(), komainu::flow::FlowError> {
fn s256() -> Result<(), komainu::flow::Error> {
let verifier_base64 = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
let challenge_base64 = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM";

Expand All @@ -19,7 +19,7 @@ fn s256() -> Result<(), komainu::flow::FlowError> {
}

#[divan::bench]
fn none() -> Result<(), komainu::flow::FlowError> {
fn none() -> Result<(), komainu::flow::Error> {
let value = "arbitrary value";

let payload = pkce::Payload {
Expand Down
16 changes: 8 additions & 8 deletions lib/komainu/src/flow/authorization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
extract::ClientCredentials,
flow::{FlowError, OptionExt, TokenResponse},
flow::{self, OptionExt, TokenResponse},
params::ParamStorage,
Authorization, ClientExtractor,
};
Expand All @@ -11,20 +11,20 @@ pub trait Issuer {
fn load_authorization(
&self,
auth_code: &str,
) -> impl Future<Output = Result<Option<Authorization<'_>>, FlowError>> + Send;
) -> impl Future<Output = Result<Option<Authorization<'_>>, flow::Error>> + Send;

fn issue_token(
&self,
authorization: &Authorization<'_>,
) -> impl Future<Output = Result<TokenResponse<'_>, FlowError>> + Send;
) -> impl Future<Output = Result<TokenResponse<'_>, flow::Error>> + Send;
}

#[instrument(skip_all)]
pub async fn perform<CE, I>(
req: http::Request<Bytes>,
client_extractor: CE,
token_issuer: I,
) -> Result<http::Response<Bytes>, FlowError>
) -> Result<http::Response<Bytes>, flow::Error>
where
CE: ClientExtractor,
I: Issuer,
Expand All @@ -44,7 +44,7 @@ where

if *grant_type != "authorization_code" {
error!(?client_id, "grant_type is not authorization_code");
return Err(FlowError::UnsupportedGrantType);
return Err(flow::Error::UnsupportedGrantType);
}

let client = client_extractor
Expand All @@ -53,16 +53,16 @@ where

if client.redirect_uri != *redirect_uri {
error!(?client_id, "redirect uri doesn't match");
return Err(FlowError::InvalidClient);
return Err(flow::Error::InvalidClient);
}

let Some(authorization) = token_issuer.load_authorization(code).await? else {
return Err(FlowError::InvalidGrant);
return Err(flow::Error::InvalidGrant);
};

// This check is constant time :3
if client != authorization.client {
return Err(FlowError::UnauthorizedClient);
return Err(flow::Error::UnauthorizedClient);
}

if let Some(ref pkce) = authorization.pkce_payload {
Expand Down
10 changes: 5 additions & 5 deletions lib/komainu/src/flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ pub mod pkce;
pub mod refresh;

trait OptionExt<T> {
fn or_invalid_request(self) -> Result<T, FlowError>;
fn or_invalid_request(self) -> Result<T, Error>;
}

impl<T> OptionExt<T> for Option<T> {
#[inline]
fn or_invalid_request(self) -> Result<T, FlowError> {
self.ok_or(FlowError::InvalidRequest)
fn or_invalid_request(self) -> Result<T, Error> {
self.ok_or(Error::InvalidRequest)
}
}

#[derive(Debug, Display, Error, Serialize)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum FlowError {
pub enum Error {
InvalidRequest,
InvalidClient,
InvalidGrant,
Expand Down Expand Up @@ -49,6 +49,6 @@ pub enum TokenResponse<'a> {
expires_in: u64,
},
Error {
errorr: FlowError,
errorr: Error,

Check warning on line 52 in lib/komainu/src/flow/mod.rs

View workflow job for this annotation

GitHub Actions / Spell-check repository source

"errorr" should be "error".
},
}
12 changes: 6 additions & 6 deletions lib/komainu/src/flow/pkce.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{error::Error, flow::FlowError};
use crate::{error::Error, flow};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::borrow::Cow;
Expand All @@ -22,7 +22,7 @@ pub struct Payload<'a> {

impl Payload<'_> {
#[inline]
fn verify_s256(&self, code_verifier: &str) -> Result<(), FlowError> {
fn verify_s256(&self, code_verifier: &str) -> Result<(), flow::Error> {
let decoded = base64_simd::URL_SAFE_NO_PAD
.decode_to_vec(self.challenge.as_bytes())
.inspect_err(|error| debug!(?error, "failed to decode pkce payload"))
Expand All @@ -32,22 +32,22 @@ impl Payload<'_> {
if decoded.ct_eq(hash.as_slice()).into() {
Ok(())
} else {
Err(FlowError::InvalidGrant)
Err(flow::Error::InvalidGrant)
}
}

#[inline]
fn verify_none(&self, code_verifier: &str) -> Result<(), FlowError> {
fn verify_none(&self, code_verifier: &str) -> Result<(), flow::Error> {
let challenge_bytes = self.challenge.as_bytes();
if challenge_bytes.ct_eq(code_verifier.as_bytes()).into() {
Ok(())
} else {
Err(FlowError::InvalidGrant)
Err(flow::Error::InvalidGrant)
}
}

#[inline]
pub fn verify(&self, code_verifier: &str) -> Result<(), FlowError> {
pub fn verify(&self, code_verifier: &str) -> Result<(), flow::Error> {
match self.method {
Method::None => self.verify_none(code_verifier),
Method::S256 => self.verify_s256(code_verifier),
Expand Down
8 changes: 4 additions & 4 deletions lib/komainu/src/flow/refresh.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
extract::ClientCredentials,
flow::{FlowError, OptionExt, TokenResponse},
flow::{self, OptionExt, TokenResponse},
params::ParamStorage,
Client, ClientExtractor,
};
Expand All @@ -12,15 +12,15 @@ pub trait Issuer {
&self,
client: &Client<'_>,
refresh_token: &str,
) -> impl Future<Output = Result<TokenResponse<'_>, FlowError>> + Send;
) -> impl Future<Output = Result<TokenResponse<'_>, flow::Error>> + Send;
}

#[instrument(skip_all)]
pub async fn perform<CE, I>(
req: http::Request<Bytes>,
client_extractor: CE,
token_issuer: I,
) -> Result<http::Response<Bytes>, FlowError>
) -> Result<http::Response<Bytes>, flow::Error>
where
CE: ClientExtractor,
I: Issuer,
Expand All @@ -39,7 +39,7 @@ where

if *grant_type != "refresh_token" {
debug!(?client_id, "grant_type is not refresh_token");
return Err(FlowError::UnsupportedGrantType);
return Err(flow::Error::UnsupportedGrantType);
}

let client = client_extractor
Expand Down
8 changes: 2 additions & 6 deletions lib/komainu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#[macro_use]
extern crate tracing;

use self::flow::pkce;
use self::{error::Error, flow::pkce};
use std::{borrow::Cow, future::Future};
use subtle::ConstantTimeEq;

pub use self::error::Error;
pub use self::params::ParamStorage;

mod error;

pub mod code_grant;
pub mod error;
pub mod extract;
pub mod flow;
pub mod params;
Expand Down

0 comments on commit cf16c27

Please sign in to comment.