Skip to content

Instantly share code, notes, and snippets.

@alexcrown
Created October 21, 2014 15:42
Show Gist options
  • Save alexcrown/089971889fda0ca4e7b1 to your computer and use it in GitHub Desktop.
Save alexcrown/089971889fda0ca4e7b1 to your computer and use it in GitHub Desktop.
Jasig CAS auth in angular webapp
$scope.login = function () {
$http({ // getting TGT (Ticket Granting Ticket)
method: 'POST',
url: 'http://localhost/cas/v1/tickets',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({username: $scope.loginform.username, password: $scope.loginform.password})
}).success(function(data, status, headers) {
// CAS returns location where we can request service ticket
var location = headers('Location');
$http({ //requesting service ticket, rest/app/heartbeat is part of our app
method: 'POST',
url: location,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({service : 'http://localhost/myapp/rest/app/heartbeat'})
}).success(function(data) {
// on success our server session wil be authenticated (over JSESSIONID cookie if java backend used)
$http({
method: 'GET',
url: env.apiEndpoint + '/rest/app/heartbeat',
params: {'ticket': data}
}).success(function(data) {
authService.loginConfirmed();
}).error($scope.setErrorMessage)
}).error($scope.setErrorMessage);
}).error($scope.setErrorMessage);
};
@witoldsz
Copy link

I think it would be easier to switch to promise API of $http and get rid of nesting of success inside another success inside yet another success callback hell. Something like:

$http(..)
  .then(function(...) { 
    ...; 
    return $http(..); 
  })
  .then(function(...) { 
    ...;
    return $http(..); 
  })
  .catch(errorCallback);

@augcampos
Copy link

Does this ever work?
I try it and I get always a response with status = -1 and the location is not available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment