mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-03-23 02:51:36 +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>
42 lines
1.1 KiB
Rust
42 lines
1.1 KiB
Rust
use rand::{RngCore, SeedableRng};
|
|
use rand_chacha::ChaChaRng;
|
|
|
|
/// The API token data structure used to authenticate requests.
|
|
pub struct APIToken {
|
|
hex_text: String,
|
|
}
|
|
|
|
impl APIToken {
|
|
/// Creates a new API token from a byte vector.
|
|
fn from_bytes(bytes: Vec<u8>) -> Self {
|
|
APIToken {
|
|
hex_text: bytes.iter().fold(String::new(), |mut result, byte| {
|
|
result.push_str(&format!("{:02x}", byte));
|
|
result
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Creates a new API token from a hexadecimal text.
|
|
pub fn from_hex_text(hex_text: &str) -> Self {
|
|
APIToken {
|
|
hex_text: hex_text.to_string(),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn to_hex_text(&self) -> &str {
|
|
self.hex_text.as_str()
|
|
}
|
|
|
|
/// Validates the received token against the valid token.
|
|
pub fn validate(&self, received_token: &Self) -> bool {
|
|
received_token.to_hex_text() == self.to_hex_text()
|
|
}
|
|
}
|
|
|
|
pub fn generate_api_token() -> APIToken {
|
|
let mut token = [0u8; 32];
|
|
let mut rng = ChaChaRng::from_os_rng();
|
|
rng.fill_bytes(&mut token);
|
|
APIToken::from_bytes(token.to_vec())
|
|
} |