mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-13 05:41:37 +00:00
Some checks failed
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
40 lines
1.3 KiB
Rust
40 lines
1.3 KiB
Rust
use once_cell::sync::Lazy;
|
|
use rocket::http::Status;
|
|
use rocket::Request;
|
|
use rocket::request::FromRequest;
|
|
use crate::api_token::{generate_api_token, APIToken};
|
|
|
|
pub static API_TOKEN: Lazy<APIToken> = Lazy::new(|| generate_api_token());
|
|
|
|
/// The request outcome type used to handle API token requests.
|
|
type RequestOutcome<R, T> = rocket::request::Outcome<R, T>;
|
|
|
|
/// The request outcome implementation for the API token.
|
|
#[rocket::async_trait]
|
|
impl<'r> FromRequest<'r> for APIToken {
|
|
type Error = APITokenError;
|
|
|
|
/// Handles the API token requests.
|
|
async fn from_request(request: &'r Request<'_>) -> RequestOutcome<Self, Self::Error> {
|
|
let token = request.headers().get_one("token");
|
|
match token {
|
|
Some(token) => {
|
|
let received_token = APIToken::from_hex_text(token);
|
|
if API_TOKEN.validate(&received_token) {
|
|
RequestOutcome::Success(received_token)
|
|
} else {
|
|
RequestOutcome::Error((Status::Unauthorized, APITokenError::Invalid))
|
|
}
|
|
}
|
|
|
|
None => RequestOutcome::Error((Status::Unauthorized, APITokenError::Missing)),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The API token error types.
|
|
#[derive(Debug)]
|
|
pub enum APITokenError {
|
|
Missing,
|
|
Invalid,
|
|
} |