I think,
- Generate the token (sensitive info at server side)
- Sign and Encrypt the generated token with machine key which is only known to server. And get the encrypted token.
- Then save the encrypted token obtained at step2 in cookies.
- Cookies expiration should be very less. Make httponly cookie.
When authenticating the cookie
- Validate the cookie
- Decrypt with machine key and verify it is sent by our server only and with the same crc.
- Authenticate the obtained token if step2 above is good.
Angularjs Automatically add headers in each $http request,
AngularAppFactory.GetApp=function(appName){
var app = angular.module(appName, []);
app.factory('httpRequestInterceptor', ['$rootScope', function($rootScope)
{
return {
request: function($config) {
if( $rootScope.user.authToken )
{
$config.headers['id'] = $rootScope.user.id;
$config.headers['auth-token'] = $rootScope.user.authToken;
}
return $config;
}
};
}]);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
return app;
}
//Whenever you need to get new angular app, you can call this function.
app = AngularAppFactory.GetApp('appName');