-
Notifications
You must be signed in to change notification settings - Fork 1
[73] modify cors to allow including credentials #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| use axum::http::{header::CONTENT_TYPE, HeaderValue, Method}; | ||
| use sqlx::{Pool, Postgres}; | ||
| use std::net::{Ipv4Addr, SocketAddrV4}; | ||
| use tokio::net::TcpListener; | ||
| use tower_http::cors::CorsLayer; | ||
| use tracing::{error, info, warn, Level}; | ||
| use tracing_subscriber::FmtSubscriber; | ||
|
|
||
|
|
@@ -9,6 +11,7 @@ use crate::database; | |
| const CRYPTO_SECRET_CORRECT: &str = "Cryptographic SECRET is set."; | ||
| const CRYPTO_SECRET_NOT_SET: &str = "Cryptographic SECRET is not set. This may lead to increased predictability in token generation."; | ||
| const CRYPTO_SECRET_ERROR: &str = "Could not read SECRET. Is it valid UTF-8?"; | ||
| const FRONTEND_ORIGIN_NOT_SET: &str = "FRONTEND_ORIGIN is not set. Please provide a valid URL leading to an accepted origin."; | ||
|
|
||
| pub fn initialise_logging() { | ||
| let subscriber = FmtSubscriber::builder() | ||
|
|
@@ -94,3 +97,33 @@ pub fn check_secret_env_var() { | |
| }, | ||
| } | ||
| } | ||
|
|
||
| pub fn configure_cors() -> CorsLayer { | ||
| let default_origin = "http://localhost:3000".to_owned(); | ||
| let result = std::env::var("FRONTEND_ORIGIN"); | ||
|
|
||
| #[cfg(not(debug_assertions))] | ||
| if result.is_err() { | ||
| error!("{}", FRONTEND_ORIGIN_NOT_SET); | ||
| panic!(); | ||
| } | ||
|
|
||
| let frontend_origin = result.unwrap_or(default_origin); | ||
| info!( | ||
| "FRONTEND_ORIGIN set to {}. Requests made from any other origins will be disallowed at browser level", | ||
| &frontend_origin | ||
| ); | ||
| let layer = CorsLayer::new() | ||
| .allow_origin(frontend_origin.parse::<HeaderValue>().unwrap()) | ||
| .allow_methods([ | ||
| Method::GET, | ||
| Method::POST, | ||
| Method::DELETE, | ||
| Method::PATCH, | ||
| Method::PUT, | ||
| ]) | ||
| .allow_headers([CONTENT_TYPE]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, does it require us to specify allowed headers? Please tell me it doesn't. And let's not do that if we can avoid it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, it does.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you tell me exactly why? How does this need manifest?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When trying to make requests at browser level, errors are returned: this one and others relating to it. This forced me to define allowed origins, methods and headers.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After testing this turns out to be true; I meant specifically Allowed-Headers, since the other ones are commonly seen and all. This is so extremely dumb. But yes, I concede. This works. |
||
| .allow_credentials(true); | ||
|
|
||
| return layer; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.