Skip to content

Commit

Permalink
add oauth errors as strum enum
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Dec 7, 2024
1 parent af4f6da commit b24bf2a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/komainu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ http.workspace = true
serde.workspace = true
serde_urlencoded.workspace = true
sonic-rs.workspace = true
strum.workspace = true
thiserror.workspace = true
tracing.workspace = true

Expand Down
21 changes: 16 additions & 5 deletions lib/komainu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate tracing;
use bytes::Bytes;
use std::collections::HashSet;
use std::{borrow::Cow, future::Future};
use strum::AsRefStr;

pub use self::error::{Error, Result};
pub use self::params::ParamStorage;
Expand Down Expand Up @@ -49,6 +50,18 @@ pub struct AuthorizerExtractor<CE> {
client_extractor: CE,
}

#[derive(AsRefStr)]
#[strum(serialize_all = "snake_case")]
pub enum OAuthError {
InvalidRequest,
UnauthorizedClient,
AccessDenied,
UnsupportedResponseType,
InvalidScope,
ServerError,
TemporarilyUnavailable,
}

#[inline]
fn get_from_either<'a>(
key: &str,
Expand All @@ -67,11 +80,9 @@ where
}

pub async fn extract<'a>(&'a self, req: &'a http::Request<()>) -> Result<Authorizer<'a>> {
let query: ParamStorage<&str, &str> = if let Some(raw_query) = req.uri().query() {
serde_urlencoded::from_str(raw_query).map_err(Error::query)?
} else {
ParamStorage::new()
};
let query: ParamStorage<&str, &str> =
serde_urlencoded::from_str(req.uri().query().or_missing_param()?)
.map_err(Error::query)?;

// TODO: Load client and verify the parameters (client ID, client secret, redirect URI, scopes, etc.) check out
// Error out if that's not the case
Expand Down

0 comments on commit b24bf2a

Please sign in to comment.