diff --git a/crates/lib/http_server_common/src/request/get_request_api_token.rs b/crates/lib/http_server_common/src/request/get_request_api_token.rs new file mode 100644 index 0000000000..1ee887ddc7 --- /dev/null +++ b/crates/lib/http_server_common/src/request/get_request_api_token.rs @@ -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 { + get_request_header_optional(request, API_TOKEN_HEADER) +} diff --git a/crates/lib/http_server_common/src/request/mod.rs b/crates/lib/http_server_common/src/request/mod.rs index c4a13b3a69..61296e7ebc 100644 --- a/crates/lib/http_server_common/src/request/mod.rs +++ b/crates/lib/http_server_common/src/request/mod.rs @@ -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; diff --git a/crates/service/storyteller_web/src/configs/mod.rs b/crates/service/storyteller_web/src/configs/mod.rs new file mode 100644 index 0000000000..0918f71344 --- /dev/null +++ b/crates/service/storyteller_web/src/configs/mod.rs @@ -0,0 +1 @@ +pub mod static_api_tokens; \ No newline at end of file diff --git a/crates/service/storyteller_web/src/configs/static_api_tokens.rs b/crates/service/storyteller_web/src/configs/static_api_tokens.rs new file mode 100644 index 0000000000..f29a97b0bb --- /dev/null +++ b/crates/service/storyteller_web/src/configs/static_api_tokens.rs @@ -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 +} + +#[derive(Clone, Deserialize, Debug)] +pub struct StaticApiTokenConfig { + /// API token. + pub api_token: String, + /// Force this user token if present. + pub maybe_user_token: Option, + /// Priority level to force + /// Defaults to "1" if not set. + pub maybe_priority_level: Option, +} + +impl StaticApiTokens { + + pub fn read_from_file(filename: &str) -> AnyhowResult { + 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) + } +} diff --git a/crates/service/storyteller_web/src/http_server/endpoints/tts/enqueue_infer_tts.rs b/crates/service/storyteller_web/src/http_server/endpoints/tts/enqueue_infer_tts.rs index b140c2250c..325a5d8ccc 100644 --- a/crates/service/storyteller_web/src/http_server/endpoints/tts/enqueue_infer_tts.rs +++ b/crates/service/storyteller_web/src/http_server/endpoints/tts/enqueue_infer_tts.rs @@ -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; diff --git a/crates/service/storyteller_web/src/main.rs b/crates/service/storyteller_web/src/main.rs index 7b7c0fd2e2..c267d88e28 100644 --- a/crates/service/storyteller_web/src/main.rs +++ b/crates/service/storyteller_web/src/main.rs @@ -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 {