Skip to content

Commit

Permalink
if cookies are rejected, try passing sessionId in url (as expected by…
Browse files Browse the repository at this point in the history
… java servlets)

NB: also configuring CAS to not redirectAfterValidation since:
- it needs cookies
- it is useless for /rest/login which is a web service
  • Loading branch information
prigaux committed Jul 1, 2014
1 parent 96a24c7 commit 5ec293f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/esupportail/smsu/domain/beans/User.java
Expand Up @@ -36,6 +36,8 @@ public class User implements Serializable {

public Set<String> rights;

public String sessionId;

/**
* Bean constructor.
*/
Expand Down
Expand Up @@ -34,6 +34,7 @@ public Response get(@Context HttpServletRequest request) throws IOException {
}

User user = domainService.getUser(request.getRemoteUser());
user.sessionId = request.getSession().getId();
String jsUser = new ObjectMapper().writeValueAsString(user);
String content, type;
if (request.getParameter("postMessage") != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/properties/auth/auth.xml
Expand Up @@ -16,7 +16,7 @@
<bean id="CASValidationFilter"
class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter">
<property name="serverName" value="${server.url}" />
<property name="redirectAfterValidation" value="true" />
<property name="redirectAfterValidation" value="false" />
<property name="ticketValidator">
<bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<constructor-arg index="0" value="${cas.url}" />
Expand Down
20 changes: 19 additions & 1 deletion src/main/webapp/js/helpers.js
Expand Up @@ -214,6 +214,8 @@ function tryRelog() {
this.setLoggedUser = function (loggedUser) {
console.log('user logged in: ' + loggedUser.id);

$rootScope.sessionId = loggedUser.sessionId;
delete loggedUser.sessionId;
$rootScope.loggedUser = h.userWithCapabilities(loggedUser);
};

Expand All @@ -233,10 +235,21 @@ function setHttpHeader(methods, name, val) {
});
}

var cookiesRejected = false;
function xhrRequest(args, flags) {
var onError401 = function (resp) {
if (flags.justSuccessfullyLogged) {
if (!flags.cookiesRejected) {
console.log("It looks like our cookies are rejected. Trying to pass sessionId in URLs...");
cookiesRejected = true;
return xhrRequest(args, flags);
} else {
alert("FATAL : both cookies and URL parameter jsessionid are rejected");
return $q.reject(resp);
}
}
return tryRelog().then(function () {
return xhrRequest(args);
return xhrRequest(args, { justSuccessfullyLogged: true });
});
};
var onErrorCsrf = function (resp, err) {
Expand Down Expand Up @@ -272,6 +285,11 @@ function xhrRequest(args, flags) {
alert("unknown error " + status);
return $q.reject(resp);
};
if (cookiesRejected && !flags.cookiesRejected) {
flags.cookiesRejected = true;
args = angular.copy(args);
args.url = args.url + ";jsessionid=" + $rootScope.sessionId;
}
return $http(args).then(function (resp) {
return resp;
}, onError);
Expand Down

0 comments on commit 5ec293f

Please sign in to comment.