-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
137 additions
and
26 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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod api_token; | ||
pub mod auth; | ||
pub mod publish; | ||
|
||
use rocket::{ | ||
http::Status, | ||
|
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,8 @@ | ||
use rocket::serde::Deserialize; | ||
|
||
/// The publish request. | ||
#[derive(Deserialize, Debug)] | ||
pub struct PublishRequest { | ||
pub name: String, | ||
pub version: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod api_token; | ||
pub mod api_token; | ||
pub mod error; | ||
mod user_session; | ||
|
||
|
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod cors; | ||
pub mod session_auth; | ||
pub mod token_auth; |
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,52 @@ | ||
use crate::db::api_token::PlainToken; | ||
use crate::db::Database; | ||
use crate::models; | ||
use rocket::http::Status; | ||
use rocket::request::{FromRequest, Outcome}; | ||
use rocket::Request; | ||
|
||
|
||
|
||
pub const SESSION_COOKIE_NAME: &str = "session"; | ||
|
||
pub struct TokenAuth { | ||
pub token: models::ApiToken, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TokenAuthError { | ||
Missing, | ||
Invalid, | ||
DatabaseConnection, | ||
} | ||
|
||
#[rocket::async_trait] | ||
impl<'r> FromRequest<'r> for TokenAuth { | ||
type Error = TokenAuthError; | ||
|
||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { | ||
// TODO: use fairing for db connection? | ||
// let db = try_outcome!(request.guard::<Database>().await); | ||
|
||
let mut db = match request.rocket().state::<Database>() { | ||
Some(db) => db.conn(), | ||
None => { | ||
return Outcome::Failure(( | ||
Status::InternalServerError, | ||
TokenAuthError::DatabaseConnection, | ||
)) | ||
} | ||
}; | ||
|
||
if let Some(auth_header) = request.headers().get_one("Authorization") { | ||
if auth_header.starts_with("Bearer ") { | ||
let token = auth_header.trim_start_matches("Bearer "); | ||
if let Ok(token) = db.get_token(PlainToken::from(token.to_string())) { | ||
return Outcome::Success(TokenAuth { token }); | ||
} | ||
} | ||
return Outcome::Failure((Status::Unauthorized, TokenAuthError::Invalid)); | ||
} | ||
return Outcome::Failure((Status::Unauthorized, TokenAuthError::Missing)); | ||
} | ||
} |
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