feat: Implement token-based authentication for user data endpoints.
This commit is contained in:
@@ -18,6 +18,38 @@ impl AuthState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Token(pub String);
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> rocket::request::FromRequest<'r> for Token {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(
|
||||
request: &'r rocket::Request<'_>,
|
||||
) -> rocket::request::Outcome<Self, Self::Error> {
|
||||
let token = request.headers().get_one("Authorization");
|
||||
|
||||
match token {
|
||||
Some(token) => {
|
||||
// Check if token starts with "Bearer "
|
||||
if let Some(token) = token.strip_prefix("Bearer ") {
|
||||
let state = request.guard::<&State<AuthState>>().await.unwrap();
|
||||
let tokens = state.tokens.lock().unwrap();
|
||||
|
||||
if tokens.contains_key(token) {
|
||||
return rocket::request::Outcome::Success(Token(token.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
rocket::request::Outcome::Error((rocket::http::Status::Unauthorized, ()))
|
||||
}
|
||||
None => rocket::request::Outcome::Error((rocket::http::Status::Unauthorized, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/login", data = "<request>")]
|
||||
pub fn login(
|
||||
state: &State<AuthState>,
|
||||
|
||||
Reference in New Issue
Block a user