-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API Key authentication. Due to a recurring question about authentication of clients I've implemented a Interceptor layer to the tonic server to check all calls for valid api keys. example config: ``` auth.enabled = false --defaults to false auth.tokens = { { client = "test", token = "Sometest" }, { client = "another client", token = "Some other test" } } ``` `auth.tokens` is a table of auth keys with their client name. There can be a "default" client or a single key for all clients, but this is up to configuration. There can be as many client keys as needed. In the debug log the client that authenticates is logged. ### Performance Performance wise there is no notable difference. ### Possible future features * Possibly in the future "expiration_date" can be added to automatically revoke issues, but I didn't think that was needed for a first implementation. * Add per client `eval` authorisation
- Loading branch information
1 parent
3d05d60
commit 36a187d
Showing
11 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,39 @@ | ||
use crate::config::AuthConfig; | ||
use tonic::codegen::http::Request; | ||
use tonic::transport::Body; | ||
use tonic::{async_trait, Status}; | ||
use tonic_middleware::RequestInterceptor; | ||
|
||
#[derive(Clone)] | ||
pub struct AuthInterceptor { | ||
pub auth_config: AuthConfig, | ||
} | ||
|
||
#[async_trait] | ||
impl RequestInterceptor for AuthInterceptor { | ||
async fn intercept(&self, req: Request<Body>) -> Result<Request<Body>, Status> { | ||
if !self.auth_config.enabled { | ||
Ok(req) | ||
} else { | ||
match req.headers().get("X-API-Key").map(|v| v.to_str()) { | ||
Some(Ok(token)) => { | ||
let mut client: Option<&String> = None; | ||
for key in &self.auth_config.tokens { | ||
if key.token == token { | ||
client = Some(&key.client); | ||
break; | ||
} | ||
} | ||
|
||
if client.is_some() { | ||
log::debug!("Authenticated client: {}", client.unwrap()); | ||
Ok(req) | ||
} else { | ||
Err(Status::unauthenticated("Unauthenticated")) | ||
} | ||
} | ||
_ => Err(Status::unauthenticated("Unauthenticated")), | ||
} | ||
} | ||
} | ||
} |
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