-
Notifications
You must be signed in to change notification settings - Fork 54
Allow the homeserver to perform introspection using a shared secret #4808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+207
−47
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -9,13 +9,12 @@ use std::collections::HashMap; | |||||
use axum::{ | ||||||
BoxError, Json, | ||||||
extract::{ | ||||||
Form, FromRequest, FromRequestParts, | ||||||
Form, FromRequest, | ||||||
rejection::{FailedToDeserializeForm, FormRejection}, | ||||||
}, | ||||||
response::IntoResponse, | ||||||
}; | ||||||
use axum_extra::typed_header::{TypedHeader, TypedHeaderRejectionReason}; | ||||||
use headers::{Authorization, authorization::Basic}; | ||||||
use headers::authorization::{Basic, Bearer, Credentials as _}; | ||||||
use http::{Request, StatusCode}; | ||||||
use mas_data_model::{Client, JwksOrJwksUri}; | ||||||
use mas_http::RequestBuilderExt; | ||||||
|
@@ -60,17 +59,30 @@ pub enum Credentials { | |||||
client_id: String, | ||||||
jwt: Box<Jwt<'static, HashMap<String, serde_json::Value>>>, | ||||||
}, | ||||||
BearerToken { | ||||||
token: String, | ||||||
}, | ||||||
} | ||||||
|
||||||
impl Credentials { | ||||||
/// Get the `client_id` of the credentials | ||||||
#[must_use] | ||||||
pub fn client_id(&self) -> &str { | ||||||
pub fn client_id(&self) -> Option<&str> { | ||||||
match self { | ||||||
Credentials::None { client_id } | ||||||
| Credentials::ClientSecretBasic { client_id, .. } | ||||||
| Credentials::ClientSecretPost { client_id, .. } | ||||||
| Credentials::ClientAssertionJwtBearer { client_id, .. } => client_id, | ||||||
| Credentials::ClientAssertionJwtBearer { client_id, .. } => Some(client_id), | ||||||
Credentials::BearerToken { .. } => None, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Get the bearer token from the credentials. | ||||||
#[must_use] | ||||||
pub fn bearer_token(&self) -> Option<&str> { | ||||||
match self { | ||||||
Credentials::BearerToken { token } => Some(token), | ||||||
_ => None, | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -89,6 +101,7 @@ impl Credentials { | |||||
| Credentials::ClientSecretBasic { client_id, .. } | ||||||
| Credentials::ClientSecretPost { client_id, .. } | ||||||
| Credentials::ClientAssertionJwtBearer { client_id, .. } => client_id, | ||||||
Credentials::BearerToken { .. } => return Ok(None), | ||||||
}; | ||||||
|
||||||
repo.oauth2_client().find_by_client_id(client_id).await | ||||||
|
@@ -239,7 +252,7 @@ pub struct ClientAuthorization<F = ()> { | |||||
impl<F> ClientAuthorization<F> { | ||||||
/// Get the `client_id` from the credentials. | ||||||
#[must_use] | ||||||
pub fn client_id(&self) -> &str { | ||||||
pub fn client_id(&self) -> Option<&str> { | ||||||
self.credentials.client_id() | ||||||
} | ||||||
} | ||||||
|
@@ -360,25 +373,36 @@ where | |||||
req: Request<axum::body::Body>, | ||||||
state: &S, | ||||||
) -> Result<Self, Self::Rejection> { | ||||||
// Split the request into parts so we can extract some headers | ||||||
let (mut parts, body) = req.into_parts(); | ||||||
|
||||||
let header = | ||||||
TypedHeader::<Authorization<Basic>>::from_request_parts(&mut parts, state).await; | ||||||
|
||||||
// Take the Authorization header | ||||||
let credentials_from_header = match header { | ||||||
Ok(header) => Some((header.username().to_owned(), header.password().to_owned())), | ||||||
Err(err) => match err.reason() { | ||||||
// If it's missing it is fine | ||||||
TypedHeaderRejectionReason::Missing => None, | ||||||
// If the header could not be parsed, return the error | ||||||
_ => return Err(ClientAuthorizationError::InvalidHeader), | ||||||
}, | ||||||
}; | ||||||
enum Authorization { | ||||||
Basic(String, String), | ||||||
Bearer(String), | ||||||
} | ||||||
|
||||||
// Sadly, the typed-header 'Authorization' doesn't let us check for both | ||||||
// Basic and Bearer at the same time, so we need to parse them manually | ||||||
let authorization = if let Some(header) = req.headers().get(http::header::AUTHORIZATION) { | ||||||
let bytes = header.as_bytes(); | ||||||
if bytes.len() >= 6 && bytes[..6].eq_ignore_ascii_case(b"Basic ") { | ||||||
let Some(decoded) = Basic::decode(header) else { | ||||||
return Err(ClientAuthorizationError::InvalidHeader); | ||||||
}; | ||||||
|
||||||
// Reconstruct the request from the parts | ||||||
let req = Request::from_parts(parts, body); | ||||||
Some(Authorization::Basic( | ||||||
decoded.username().to_owned(), | ||||||
decoded.password().to_owned(), | ||||||
)) | ||||||
} else if bytes.len() >= 7 && bytes[..7].eq_ignore_ascii_case(b"Bearer ") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let Some(decoded) = Bearer::decode(header) else { | ||||||
return Err(ClientAuthorizationError::InvalidHeader); | ||||||
}; | ||||||
|
||||||
Some(Authorization::Bearer(decoded.token().to_owned())) | ||||||
} else { | ||||||
return Err(ClientAuthorizationError::InvalidHeader); | ||||||
} | ||||||
} else { | ||||||
None | ||||||
}; | ||||||
|
||||||
// Take the form value | ||||||
let ( | ||||||
|
@@ -407,13 +431,19 @@ where | |||||
|
||||||
// And now, figure out the actual auth method | ||||||
let credentials = match ( | ||||||
credentials_from_header, | ||||||
authorization, | ||||||
client_id_from_form, | ||||||
client_secret_from_form, | ||||||
client_assertion_type, | ||||||
client_assertion, | ||||||
) { | ||||||
(Some((client_id, client_secret)), client_id_from_form, None, None, None) => { | ||||||
( | ||||||
Some(Authorization::Basic(client_id, client_secret)), | ||||||
client_id_from_form, | ||||||
None, | ||||||
None, | ||||||
None, | ||||||
) => { | ||||||
if let Some(client_id_from_form) = client_id_from_form { | ||||||
// If the client_id was in the body, verify it matches with the header | ||||||
if client_id != client_id_from_form { | ||||||
|
@@ -483,6 +513,11 @@ where | |||||
}); | ||||||
} | ||||||
|
||||||
(Some(Authorization::Bearer(token)), None, None, None, None) => { | ||||||
// Got a bearer token | ||||||
Credentials::BearerToken { token } | ||||||
} | ||||||
|
||||||
(None, None, None, None, None) => { | ||||||
// Special case when there are no credentials anywhere | ||||||
return Err(ClientAuthorizationError::MissingCredentials); | ||||||
|
@@ -677,4 +712,29 @@ mod tests { | |||||
jwt.verify_with_shared_secret(b"client-secret".to_vec()) | ||||||
.unwrap(); | ||||||
} | ||||||
|
||||||
#[tokio::test] | ||||||
async fn bearer_token_test() { | ||||||
let req = Request::builder() | ||||||
.method(Method::POST) | ||||||
.header( | ||||||
http::header::CONTENT_TYPE, | ||||||
mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(), | ||||||
) | ||||||
.header(http::header::AUTHORIZATION, "Bearer token") | ||||||
.body(Body::new("foo=bar".to_owned())) | ||||||
.unwrap(); | ||||||
|
||||||
assert_eq!( | ||||||
ClientAuthorization::<serde_json::Value>::from_request(req, &()) | ||||||
.await | ||||||
.unwrap(), | ||||||
ClientAuthorization { | ||||||
credentials: Credentials::BearerToken { | ||||||
token: "token".to_owned(), | ||||||
}, | ||||||
form: Some(serde_json::json!({"foo": "bar"})), | ||||||
} | ||||||
); | ||||||
} | ||||||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to make a case-insensitive comparison, hence the
eq_ignore_ascii_case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah derp, fine :)