Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Add allow credentials support #12

Closed
wants to merge 5 commits into from
Closed

Add allow credentials support #12

wants to merge 5 commits into from

Conversation

dnp1
Copy link

@dnp1 dnp1 commented Nov 22, 2017

No description provided.

Copy link
Owner

@dbrgn dbrgn left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated
}
}

pub fn allow_credentials(&mut self) {
self.allow_credentials = true;
}
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure yet about this API. I think a setter method like this is not a good solution.

I could imagine two ways to configure the credentials: Either as constructor argument, or as a builder. Maybe something like this?

CorsMiddlewareBuilder::with_whitelist(allowed_hosts) // Or: with_allow_any()
    .allow_credentials(true)
    .build();

What are your thoughts?

Copy link
Author

@dnp1 dnp1 Nov 22, 2017

Choose a reason for hiding this comment

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

I agree
I'm going to change tonight.

But this way the contract will change.

Is this a problem for you?

Copy link
Owner

Choose a reason for hiding this comment

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

We could deprecate the constructor functions (with_whitelist and allow_credentials) on the CorsMiddleware object itself and simply add the builder in parallel. This way it's compatible and we can remove it in the next version.

Copy link
Owner

@dbrgn dbrgn left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the changes and sorry for the late review - I somehow overlooked the updates!

I left a few additional comments.

@@ -155,7 +193,7 @@ impl CorsHandlerWhitelist {
}

// If we don't have an Access-Control-Request-Method header, treat as a possible OPTION CORS call
return self.process_possible_cors_request(req, origin)
return self.process_possible_cors_request(req, origin);
Copy link
Owner

Choose a reason for hiding this comment

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

If you're at it, can you change this to self.process_possible_cors_request(req, origin) (without the return)?

@@ -239,13 +286,19 @@ impl CorsHandlerAllowAny {
}

// If we don't have an Access-Control-Request-Method header, treat as a possible OPTION CORS call
return self.process_possible_cors_request(req)
return self.process_possible_cors_request(req);
Copy link
Owner

Choose a reason for hiding this comment

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

And here too: Remove the return.


pub fn build(&self) -> CorsMiddleware {
CorsMiddleware { allowed_hosts: self.allowed_hosts.clone(), allow_credentials: self.allow_credentials }
}
Copy link
Owner

Choose a reason for hiding this comment

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

Can you change the build method to consume self? Then you can avoid the clone().

@@ -58,16 +59,45 @@ use iron::method::Method;
use iron::status;
use iron::headers;

/// The struct that builds a CorsMiddleware
pub struct CorsMiddlewareBuilder {
Copy link
Owner

Choose a reason for hiding this comment

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

Please use #[derive(Debug, Clone, PartialEq)].

/// The struct that holds the CORS configuration.
pub struct CorsMiddleware {
allowed_hosts: Option<HashSet<String>>,
allow_credentials: bool,
}

impl CorsMiddleware {
/// Specify which origin hosts are allowed to access the resource.
pub fn with_whitelist(allowed_hosts: HashSet<String>) -> Self {
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add the deprecation warning here?

#[deprecated(since="0.7.0", note="please use the `CorsMiddlewareBuilder` instead")]

@@ -77,6 +107,7 @@ impl CorsMiddleware {
pub fn with_allow_any() -> Self {
Copy link
Owner

Choose a reason for hiding this comment

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

And also here.

#[deprecated(since="0.7.0", note="please use the `CorsMiddlewareBuilder` instead")]

CorsMiddlewareBuilder { allow_credentials: false, allowed_hosts: None }
}

/// Specify which origin hosts are allowed to access the resource.
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add another line here?

/// If you don't specify any allowed hosts, then any host will be allowed to access the resource.

self.allowed_hosts = Some(allowed_hosts);
self
}
/// Specify Access-Control-Allow-Credentials
Copy link
Owner

Choose a reason for hiding this comment

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

Can you document the default value?

/// By default, the `AccessControlAllowCredentials` header will be set to `false`.

@dbrgn dbrgn mentioned this pull request Dec 28, 2017
@dnp1 dnp1 closed this Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants