Skip to content

Commit

Permalink
feat: add multiple origin URLs for CORS (#180)
Browse files Browse the repository at this point in the history
* feat: add multiple origin URLs for CORS

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>

* chore: add CHANGELOG entry

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>

* fix: use correct string conversion

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>

Signed-off-by: Brandon H. Gomes <bhgomes@pm.me>
  • Loading branch information
bhgomes authored Oct 5, 2022
1 parent f2bbace commit eaa1851
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]

### Added
- [\#180](https://github.com/Manta-Network/manta-signer/pull/180) Add support for mutliple CORS allowed origins

### Changed

Expand Down
16 changes: 12 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ pub struct Config {
pub data_path: PathBuf,

/// Service URL
///
/// This URL defines the listening URL for the service.
pub service_url: String,

/// Origin URL
pub origin_url: Option<String>,
/// Origin URLs
///
/// These URLs are the allowed origins that can send requests to the service. An empty list
/// means any origin is allowed to send requests to the service.
pub origin_urls: Vec<String>,
}

impl Config {
Expand All @@ -64,9 +69,12 @@ impl Config {
data_path: file(dirs_next::config_dir(), "storage.dat")?,
service_url: "127.0.0.1:29987".into(),
#[cfg(feature = "unsafe-disable-cors")]
origin_url: None,
origin_urls: vec![],
#[cfg(not(feature = "unsafe-disable-cors"))]
origin_url: Some("https://app.dolphin.manta.network".into()),
origin_urls: vec![
"https://app.manta.network".into(),
"https://app.dolphin.manta.network".into(),
],
})
}

Expand Down
7 changes: 4 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@ where
let socket_address = config.service_url.parse::<SocketAddr>()?;
let cors = CorsMiddleware::new()
.allow_methods("GET, POST".parse::<HeaderValue>().unwrap())
.allow_origin(match &config.origin_url {
Some(origin_url) => Origin::from(origin_url.as_str()),
_ => Origin::from("*"),
.allow_origin(if config.origin_urls.is_empty() {
Origin::Any
} else {
Origin::List(config.origin_urls)
})
.allow_credentials(false);
let mut api = tide::Server::with_state(self);
Expand Down

1 comment on commit eaa1851

@vercel
Copy link

@vercel vercel bot commented on eaa1851 Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.