Commit 3c1764df authored by Randall Leeds's avatar Randall Leeds

Isolate XSRF Token handling to session service

parent b47b6bd0
...@@ -5,42 +5,31 @@ imports = [ ...@@ -5,42 +5,31 @@ imports = [
] ]
configure = ['$httpProvider', 'identityProvider', ($httpProvider, identityProvider) -> configure = [
defaults = $httpProvider.defaults '$httpProvider', 'identityProvider',
($httpProvider, identityProvider) ->
# Use the Pyramid XSRF header name # Use the Pyramid XSRF header name
defaults.xsrfHeaderName = 'X-CSRF-Token' $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token'
$httpProvider.interceptors.push ['documentHelpers', (documentHelpers) -> identityProvider.checkAuthorization = [
request: (config) -> 'session',
endpoint = documentHelpers.absoluteURI('/app') (session) ->
if config.url.indexOf(endpoint) == 0 session.load().$promise
# Set the cross site request forgery token ]
cookieName = config.xsrfCookieName || defaults.xsrfCookieName
headerName = config.xsrfHeaderName || defaults.xsrfHeaderName identityProvider.forgetAuthorization = [
config.headers[headerName] ?= csrfToken 'session',
config (session) ->
] session.logout({}).$promise
]
identityProvider.checkAuthorization = [
'session', identityProvider.requestAuthorization = [
(session) -> '$q', '$rootScope',
session.load().$promise ($q, $rootScope) ->
] deferred = $q.defer()
$rootScope.$on 'session', (event, data) -> deferred.resolve data
identityProvider.forgetAuthorization = [ deferred.promise
'session', ]
(session) ->
session.logout({}).$promise
]
identityProvider.requestAuthorization = [
'$q', '$rootScope',
($q, $rootScope) ->
deferred = $q.defer()
$rootScope.$on 'session', (event, data) -> deferred.resolve data
deferred.promise
]
] ]
......
...@@ -21,12 +21,6 @@ for action in ACTION ...@@ -21,12 +21,6 @@ for action in ACTION
withCredentials: true withCredentials: true
# Global because $resource doesn't support request interceptors, so a
# the default http request interceptor and the session resource interceptor
# need to share it.
csrfToken = null
###* ###*
# @ngdoc provider # @ngdoc provider
# @name sessionProvider # @name sessionProvider
...@@ -66,9 +60,20 @@ class SessionProvider ...@@ -66,9 +60,20 @@ class SessionProvider
# }); # });
### ###
$get: [ $get: [
'$q', '$resource', 'documentHelpers', 'flash', '$http', '$q', '$resource', 'documentHelpers', 'flash',
($q, $resource, documentHelpers, flash) -> ($http, $q, $resource, documentHelpers, flash) ->
actions = {} actions = {}
provider = this
# Capture the state of the cross site request forgery token.
# If cookies are blocked this is our only way to get it.
xsrfToken = null
prepare = (data, headersGetter) ->
if xsrfToken
headers = headersGetter()
headers[$http.defaults.xsrfHeaderName] = xsrfToken
return angular.toJson data
process = (data, headersGetter) -> process = (data, headersGetter) ->
# Parse as json # Parse as json
...@@ -83,15 +88,14 @@ class SessionProvider ...@@ -83,15 +88,14 @@ class SessionProvider
for q, msgs of data.flash for q, msgs of data.flash
flash q, msgs flash q, msgs
# Capture the cross site request forgery token without cookies. xsrfToken = model.csrf
# If cookies are blocked this is our only way to get it.
csrfToken = model.certificate
# Return the model # Return the model
model model
for name, options of ACTION_OPTION for name, options of ACTION_OPTION
actions[name] = angular.extend {}, options, @options actions[name] = angular.extend {}, options, @options
actions[name].transformRequest = prepare
actions[name].transformResponse = process actions[name].transformResponse = process
endpoint = documentHelpers.absoluteURI('/app') endpoint = documentHelpers.absoluteURI('/app')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment