-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cat-gateway): Add APIKey and CatToken auth to some endpoints. Add…
… 401 and 403 common responses.
- Loading branch information
Showing
17 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,31 @@ | ||
//! API Key authorization scheme is used ONLY by internal endpoints. | ||
//! | ||
//! Its purpose is to prevent their use externally, if they were accidentally exposed. | ||
//! | ||
//! It is NOT to be used on any endpoint intended to be publicly facing. | ||
use poem::Request; | ||
use poem_openapi::{auth::ApiKey, SecurityScheme}; | ||
|
||
use crate::settings::Settings; | ||
|
||
/// `ApiKey` authorization for Internal Endpoints | ||
#[derive(SecurityScheme)] | ||
#[oai( | ||
ty = "api_key", | ||
key_name = "X-API-Key", | ||
key_in = "header", | ||
checker = "api_checker" | ||
)] | ||
#[allow(dead_code)] | ||
pub(crate) struct InternalApiKeyAuthorization(String); | ||
|
||
/// Check the provided API Key matches the API Key defined by for the deployment. | ||
#[allow(clippy::unused_async)] | ||
async fn api_checker(_req: &Request, api_key: ApiKey) -> Option<String> { | ||
if Settings::check_internal_api_key(&api_key.key) { | ||
Some(api_key.key) | ||
} else { | ||
None | ||
} | ||
} |
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,7 @@ | ||
//! Catalyst RBAC Token Authentication | ||
pub(crate) mod api_key; | ||
/// Cat security scheme | ||
pub(crate) mod scheme; | ||
/// Token encoding decoding logic | ||
mod token; |
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
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
35 changes: 35 additions & 0 deletions
35
catalyst-gateway/bin/src/service/common/responses/code_401_unauthorized.rs
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,35 @@ | ||
//! Define `Unauthorized` response type. | ||
use poem_openapi::{types::Example, Object}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Object)] | ||
#[oai(example, skip_serializing_if_is_none)] | ||
/// Server Error response to a Bad request. | ||
pub(crate) struct Unauthorized { | ||
/// Unique ID of this Server Error so that it can be located easily for debugging. | ||
id: Uuid, | ||
/// Error message. | ||
// Will not contain sensitive information, internal details or backtraces. | ||
#[oai(validator(max_length = "1000", pattern = "^[0-9a-zA-Z].*$"))] | ||
msg: String, | ||
} | ||
|
||
impl Unauthorized { | ||
/// Create a new Server Error Response Payload. | ||
pub(crate) fn new(msg: Option<String>) -> Self { | ||
let msg = msg.unwrap_or( | ||
"Your request was not successful because it lacks valid authentication credentials for the requested resource.".to_string(), | ||
); | ||
let id = Uuid::new_v4(); | ||
|
||
Self { id, msg } | ||
} | ||
} | ||
|
||
impl Example for Unauthorized { | ||
/// Example for the Too Many Requests Payload. | ||
fn example() -> Self { | ||
Self::new(None) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
catalyst-gateway/bin/src/service/common/responses/code_403_forbidden.rs
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 @@ | ||
//! Define `Forbidden` response type. | ||
use poem_openapi::{types::Example, Object}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Object)] | ||
#[oai(example, skip_serializing_if_is_none)] | ||
/// Server Error response to a Bad request. | ||
pub(crate) struct Forbidden { | ||
/// Unique ID of this Server Error so that it can be located easily for debugging. | ||
id: Uuid, | ||
/// Error message. | ||
// Will not contain sensitive information, internal details or backtraces. | ||
#[oai(validator(max_length = "1000", pattern = "^[0-9a-zA-Z].*$"))] | ||
msg: String, | ||
/// List or Roles required to access the resource. | ||
// TODO: This should be a Vector of defined Roles/Grants. | ||
// When those are defined, use that type instead of "String" | ||
// It should look like an enum. | ||
#[oai(validator(max_items = 100, max_length = "100", pattern = "^[0-9a-zA-Z].*$"))] | ||
required: Option<Vec<String>>, | ||
} | ||
|
||
impl Forbidden { | ||
/// Create a new Server Error Response Payload. | ||
pub(crate) fn new(msg: Option<String>, roles: Option<Vec<String>>) -> Self { | ||
let msg = msg.unwrap_or( | ||
"Your request was not successful because your authentication credentials do not have the required roles for the requested resource.".to_string(), | ||
); | ||
let id = Uuid::new_v4(); | ||
|
||
Self { | ||
id, | ||
msg, | ||
required: roles, | ||
} | ||
} | ||
} | ||
|
||
impl Example for Forbidden { | ||
/// Example for the Too Many Requests Payload. | ||
fn example() -> Self { | ||
Self::new( | ||
None, | ||
Some(vec!["VOTER".to_string(), "PROPOSER".to_string()]), | ||
) | ||
} | ||
} |
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
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
Oops, something went wrong.