Begin work on statically configured API tokens

This commit is contained in:
Brandon Thomas committed 2022-04-27 16:37:58 -04:00
1 parent 70dccbe4fa
commit f8bd7fb681
6 files changed
+52

No files matched your search

@@ -0,0 +1,9 @@
use actix_web::HttpRequest;
use crate::request::get_request_header_optional::get_request_header_optional;
const API_TOKEN_HEADER : &'static str = "AUTHORIZATION";
/// Get the optional API token sent with the request
pub fn get_request_api_token(request: &HttpRequest) -> Option<String> {
get_request_header_optional(request, API_TOKEN_HEADER)
}
@@ -1,3 +1,4 @@
pub mod get_request_api_token;
pub mod get_request_header_optional;
pub mod get_request_host;
pub mod get_request_ip;
@@ -0,0 +1 @@
pub mod static_api_tokens;
@@ -0,0 +1,33 @@
//! Static FakeYou API tokens can be read from a config file.
//! This will be a stopgap until we deploy the full system.
use std::fs::File;
use std::io::Read;
use container_common::anyhow_result::AnyhowResult;
#[derive(Clone, Deserialize, Debug)]
pub struct StaticApiTokens {
pub api_tokens: Vec<StaticApiTokenConfig>
}
#[derive(Clone, Deserialize, Debug)]
pub struct StaticApiTokenConfig {
/// API token.
pub api_token: String,
/// Force this user token if present.
pub maybe_user_token: Option<String>,
/// Priority level to force
/// Defaults to "1" if not set.
pub maybe_priority_level: Option<u8>,
}
impl StaticApiTokens {
pub fn read_from_file(filename: &str) -> AnyhowResult<StaticApiTokens> {
let mut file = File::open(filename)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
let api_tokens = toml::from_str(&buffer)?;
Ok(api_tokens)
}
}
@@ -191,6 +191,9 @@ pub async fn infer_tts_handler(
.or(maybe_user_preferred_visibility)
.unwrap_or(RecordVisibility::Public);
// API tokens
let is_from_api = false;
// This token is returned to the client.
let job_token = Tokens::new_tts_inference_job()
.map_err(|e| {
@@ -213,6 +216,7 @@ SET
creator_ip_address = ?,
creator_set_visibility = ?,
priority_level = ?,
is_from_api = ?,
status = "pending"
"#,
&job_token,
@@ -223,6 +227,7 @@ SET
ip_address,
set_visibility.to_str(),
priority_level,
is_from_api,
)
.execute(&server_state.mysql_pool)
.await;
@@ -16,6 +16,7 @@ pub const RESERVED_USERNAMES : &'static str = include_str!("../../../../db/reser
pub const RESERVED_SUBSTRINGS : &'static str = include_str!("../../../../db/reserved_usernames_including.txt");
pub mod common_env;
pub mod configs;
pub mod http_clients;
pub mod http_server;
pub mod routes;
@@ -24,9 +25,11 @@ pub mod threads;
pub mod util;
pub mod validations;
// TODO: Eventually move all of these to the `database_queries` crate and no longer write inline MySQL.
// NB: This is included so sqlx can generate all the queries.
mod job_queries;
// TODO: Eventually move all of these to the `database_queries` crate and no longer write inline MySQL.
// NB: Also so sqlx codegens everything.
// Not sure if this is strictly necessary.
mod shared_queries {