Last active
September 4, 2020 18:11
-
-
Save mlynch/be92735ce4c547bd45f6 to your computer and use it in GitHub Desktop.
Revisions
-
mlynch revised this gist
Sep 2, 2014 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,4 +40,8 @@ angular.module('myApp', ['ngCookies']) .run(['$http', '$cookies', function($http, $cookies) { $http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken; }]); ``` ### Credentials and CORS One thing to note when using `withCredentials: true` in your app and configuring the server for CORS is that you may not have your `Access-Control-Allow-Origin` header set to `'*'`. It must be configured to a few select origins. If you absolutely must have this set to `*`, then I suggest doing something beyond cookie based authentication, such as token-based authentication. -
mlynch revised this gist
Sep 1, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ CORS is an oft-misunderstood feature of new browsers that is configured by a rem Like many browser features, CORS works because we all agree that it works. So all major browsers like Chrome, Firefox, and IE support and enforce it. By using these browsers, you benefit from the security of CORS. That means certain browsers do not enforce it, so it is not relevant there. One large example is a native Web View for things like Cordova and Phonegap. However, these tools often have configuration options for whitelisting domains so you can add some security that way. ### Configuring CORS on the Server -
mlynch revised this gist
Sep 1, 2014 . 1 changed file with 21 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,10 +14,30 @@ The way CORS works is the server decides which domains it will accept as clients I won't get into the details of configuring CORS on the server side, but it's really just setting some headers. Here's how you might do it [in nginx](https://gist.github.com/michiel/1064640). There is one header in particular you must have if you want to do session based authentication in a single page app: `Access-Control-Allow-Credentials: true` which we show next. ### AngularJS Auth If you use the standard `$http` service to access remote APIs, it will Just Work as long as the server is configured to allow requests from your domain. But for many applications, we also need to set and store cookie information for things like logins. By default this is not allowed in most browsers and you'll be smashing your head wondering why the cookie information isn't being saved! Enter: `withCredentials`. `withCredentials` is a flag set on a low-level `XMLHttpRequest` (AJAX) object, but in Angular we can configure our `$http` requests to set this flag for everything by doing: ```javascript angular.module('myApp') .config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.withCredentials = true; }]) ``` As for CSRF, we can tell `$http` to set the correct header for CSRF (might depend on your server framework, this one is for Django) using the specific cookie name: ```javascript angular.module('myApp', ['ngCookies']) .run(['$http', '$cookies', function($http, $cookies) { $http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken; }]); ``` -
mlynch revised this gist
Sep 1, 2014 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,4 +12,12 @@ That means certain browsers do not enforce it, so it is not relevant there. One The way CORS works is the server decides which domains it will accept as clients. This means an open API like Twitter might allow any clients, or a closed API might decide to only allow access from the domain of the running client app. I won't get into the details of configuring CORS on the server side, but it's really just setting some headers. Here's how you might do it [in nginx](https://gist.github.com/michiel/1064640). ### AngularJS Auth If you use the standard `$http` service to access remote APIs, it will Just Work as long as the server is configured to allow requests from your domain. But for many applications, we also need to set and store cookie information for things like logins. By default this is not allowed in most browsers and you'll be smashing your head wondering why the cookie information isn't being saved! Enter: `withCredentials`. -
mlynch revised this gist
Sep 1, 2014 . 1 changed file with 8 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,12 @@ Single Page Apps are ruling the world and AngularJS is leading the charge. But m CORS is an oft-misunderstood feature of new browsers that is configured by a remote server. CORS stands for Cross-Origin-Resource-Sharing, and was designed to make it possible to access services outside of the current origin (or domain) of the current page. Like many browser features, CORS works because we all agree that it works. So all major browsers like Chrome, Firefox, and IE support and enforce it. By using these browsers, you benefit from the security of CORS. That means certain browsers do not enforce it, so it is not relevant there. One large example is a native Web View for things like Cordova and Phonegap. ### Configuring CORS on the Server The way CORS works is the server decides which domains it will accept as clients. This means an open API like Twitter might allow any clients, or a closed API might decide to only allow access from the domain of the running client app. I won't get into the details of configuring CORS on the server side, but it's really just setting some headers. Here's how you might do it [in nginx](https://gist.github.com/michiel/1064640). -
mlynch created this gist
Sep 1, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ Single Page Apps are ruling the world and AngularJS is leading the charge. But many of the lessons we learned in the Web 2.0 era no longer apply, and few are as drastically different as authentication. ### CORS CORS is an oft-misunderstood feature of new browsers that is configured by a remote server. CORS stands for Cross-Origin-Resource-Sharing, and was designed to make it possible to access services outside of the current origin (or domain) of the current page. Like many browser features, CORS works because we all agree that it works. So all major browsers like Chrome, Firefox, and IE support and enforce it. That means certain browsers do not enforce it, so it is not relevant there. One large example is a native Web View for things like Cordova and Phonegap.