Skip to content

Commit 31d70fd

Browse files
committed
cors and openai security stuff in apps/ai
1 parent 3c9ef9f commit 31d70fd

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

apps/ai/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ owhisper-client = { workspace = true }
1212

1313
axum = { workspace = true, features = ["ws"] }
1414
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "signal"] }
15-
tower-http = { workspace = true, features = ["trace"] }
15+
tower-http = { workspace = true, features = ["trace", "cors"] }
1616
tracing = { workspace = true }
1717
tracing-subscriber = { workspace = true, features = ["env-filter"] }
1818

apps/ai/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use std::time::Duration;
99
use axum::{Router, body::Body, extract::MatchedPath, http::Request, middleware};
1010
use sentry::integrations::tower::{NewSentryLayer, SentryHttpLayer};
1111
use tower::ServiceBuilder;
12-
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
12+
use tower_http::{
13+
classify::ServerErrorsFailureClass,
14+
cors::{self, CorsLayer},
15+
trace::TraceLayer,
16+
};
1317
use tracing_subscriber::prelude::*;
1418

1519
use hypr_analytics::AnalyticsClientBuilder;
@@ -49,6 +53,12 @@ fn app() -> Router {
4953
.route("/health", axum::routing::get(|| async { "ok" }))
5054
.route("/openapi.json", axum::routing::get(openapi_json))
5155
.merge(protected_routes)
56+
.layer(
57+
CorsLayer::new()
58+
.allow_origin(cors::Any)
59+
.allow_methods(cors::Any)
60+
.allow_headers(cors::Any),
61+
)
5262
.layer(
5363
ServiceBuilder::new()
5464
.layer(NewSentryLayer::<Request<Body>>::new_from_top())

apps/ai/src/openapi.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use utoipa::OpenApi;
1+
use utoipa::openapi::security::{ApiKey, ApiKeyValue, Http, HttpAuthScheme, SecurityScheme};
2+
use utoipa::{Modify, OpenApi};
23

34
#[derive(OpenApi)]
45
#[openapi(
@@ -10,7 +11,8 @@ use utoipa::OpenApi;
1011
tags(
1112
(name = "stt", description = "Speech-to-text transcription endpoints"),
1213
(name = "llm", description = "LLM chat completions endpoints")
13-
)
14+
),
15+
modifiers(&SecurityAddon)
1416
)]
1517
pub struct ApiDoc;
1618

@@ -25,3 +27,29 @@ pub fn openapi() -> utoipa::openapi::OpenApi {
2527

2628
doc
2729
}
30+
31+
struct SecurityAddon;
32+
33+
impl Modify for SecurityAddon {
34+
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
35+
if let Some(components) = openapi.components.as_mut() {
36+
components.add_security_scheme(
37+
"bearer_auth",
38+
SecurityScheme::Http(
39+
Http::builder()
40+
.scheme(HttpAuthScheme::Bearer)
41+
.bearer_format("JWT")
42+
.description(Some("Supabase JWT token"))
43+
.build(),
44+
),
45+
);
46+
components.add_security_scheme(
47+
"device_fingerprint",
48+
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::with_description(
49+
"x-device-fingerprint",
50+
"Optional device fingerprint for analytics",
51+
))),
52+
);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)