Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Origin Resource Sharing with Credentials

Tags:

http

cors

I have a common authentication form across multiple subdomains (example.com, blog.example.com, and app.example.com). The login form must submit this data to example.com irrespective of where it is shown, so I thought of using CORS, but this:

header("Access-Control-Allow-Origin: http://example.com http://blog.example.com http://app.example.com") 

does not work

So I thought of doing the next thing, and checking the Origin header manually on server side, and allowing a Access-Control-Allow-Origin: * so that requests might be made, but unfortunately, this crops up in the MDN

Important note: when responding to a credentialed request, server must specify a domain, and cannot use wild carding.

Is there any way to make my request work across multiple domains, and still send credentials using CORS ?

like image 882
Nemo Avatar asked Nov 10 '11 03:11

Nemo


People also ask

What is cross-origin resource sharing in cyber security?

Cross-origin resource sharing (CORS) is a browser mechanism which enables controlled access to resources located outside of a given domain. It extends and adds flexibility to the same-origin policy (SOP).

What is cross-origin authentication?

What is cross-origin authentication? When authentication requests are made from your application (via the Lock widget or a custom login form) to Auth0, the user's credentials are sent to a domain that differs from the one that serves your application.

What is cross-origin resource sharing example?

Simple CORS example domainy.com receives that request and will respond back with either: Access-Control-Allow-Origin: http://domainx.com. Access-Control-Allow-Origin: * (meaning all domains are allowed) An error if the cross-origin requests are not allowed.

What does cross-origin resource sharing CORS enable?

Cross-origin resource sharing (CORS) defines a way for client web applications that are loaded in one domain to interact with resources in a different domain. With CORS support, you can build rich client-side web applications with Amazon S3 and selectively allow cross-origin access to your Amazon S3 resources.


1 Answers

Two thoughts:

1) are you also including the "Access-Control-Allow-Credentials: true" header? This is needed for passing cookie credentials (and the corresponding XHR client must set .withCredentials = true)

2) Have you tried the suggestion from your link and only include the origin for the current request. For example, if a request comes in with the header "Origin: http://blog.example.com", you would respond with "Access-Control-Allow-Origin: http://blog.example.com", and not a list of origins. This requires a little more work on your server side implementation.

3) One other thought, you mention that you have a single login form that must be shared by various domains. Well, if it is a standard HTML form, you can do a regular form-post across domains. You don't need to use CORS. Just set the "action" property of the form to the url you wish to post to. For example:

<form name="login" action="http://login.example.com/doLogin"> 
like image 196
monsur Avatar answered Oct 04 '22 17:10

monsur