Skip to content
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

Add support for configuring cross-origin resource sharing to the server #35

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use subtle::ConstantTimeEq;
use thiserror::Error;
use tokio::sync::{mpsc, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use warp::filters::ws::Message;
use warp::Filter;
use warp::{filters::cors::Builder, Filter};

use nimiq_jsonrpc_core::{
Request, Response, RpcError, Sensitive, SingleOrBatch, SubscriptionId, SubscriptionMessage,
Expand Down Expand Up @@ -64,7 +64,6 @@ pub enum Error {
///
/// #TODO
///
/// - CORS header
/// - allowed methods
///
#[derive(Clone, Debug)]
Expand All @@ -80,6 +79,9 @@ pub struct Config {

/// Username and password for HTTP basic authentication.
pub basic_auth: Option<Credentials>,

/// Cross-Origin Resource Sharing configuration
pub cors: Option<Cors>,
}

impl Default for Config {
Expand All @@ -89,6 +91,7 @@ impl Default for Config {
enable_websocket: true,
ip_whitelist: None,
basic_auth: None,
cors: None,
}
}
}
Expand All @@ -97,6 +100,43 @@ fn blake2b(bytes: &[u8]) -> [u8; 32] {
*Blake2b::<U32>::digest(bytes).as_ref()
}

#[derive(Clone, Debug)]
/// CORS configuration
pub struct Cors(Builder);

impl Cors {
/// Create a new instance with `Content-Type` as mandatory header and `POST` as mandatory method.
pub fn new() -> Self {
Self(
warp::cors()
.allow_header("Content-Type")
.allow_method("POST"),
)
}

/// Configure CORS to only allow specific origins.
pub fn with_origins(mut self, origins: Vec<&str>) -> Self {
self.0 = self.0.allow_origins(origins);
self
}

/// Configure CORS to allow every origin. Also known as the `*` wildcard.
pub fn with_any_origin(mut self) -> Self {
self.0 = self.0.allow_any_origin();
self
}

pub(crate) fn into_wrapper(self) -> Builder {
self.0
}
}

impl Default for Cors {
fn default() -> Self {
Self::new()
}
}

/// Basic auth credentials, containing username and password.
#[derive(Clone, Debug)]
pub struct Credentials {
Expand Down Expand Up @@ -253,6 +293,13 @@ impl<D: Dispatcher> Server<D> {

warp::serve(
root.and(json_rpc_route)
.with(
self.inner
.config
.cors
.clone()
.map_or(warp::cors(), |cors| cors.into_wrapper()),
)
.recover(auth_filter::handle_auth_rejection),
)
.run(self.inner.config.bind_to)
Expand Down
Loading