Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
</natures>
<filteredResources>
<filter>
<id>1598983010649</id>
<id>1671208548449</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.Serializable;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.userdetails.UserDetails;

public interface SessionService {
Expand All @@ -28,4 +30,8 @@ public interface SessionService {
void removeCurrentAuthor(Serializable projectdId, String authorUsername);

void removeUser(UserDetails user);

boolean isCkBoardAvailable();

void signOutOfCkBoard(HttpServletRequest request);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package org.wise.portal.service.session.impl;

import java.io.IOException;
import java.io.Serializable;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.session.Session;
Expand All @@ -15,6 +23,9 @@
@Service
public class SessionServiceImpl<S extends Session> implements SessionService {

@Autowired
private Environment appProperties;

@Autowired
private StringRedisTemplate stringRedisTemplate;

Expand Down Expand Up @@ -63,17 +74,17 @@ public void addCurrentAuthor(Serializable projectId, String authorUsername) {

@Override
public void removeCurrentAuthor(UserDetails author) {
Set<String> currentlyAuthoredProjects =
stringRedisTemplate.opsForSet().members("currentlyAuthoredProjects");
Set<String> currentlyAuthoredProjects = stringRedisTemplate.opsForSet()
.members("currentlyAuthoredProjects");
for (String projectId : currentlyAuthoredProjects) {
removeCurrentAuthor(projectId, author.getUsername());
}
}

public void removeCurrentAuthor(Serializable projectId, String authorUsername) {
stringRedisTemplate.opsForSet().remove("currentAuthors:" + projectId, authorUsername);
Long numCurrentAuthorsForProject =
stringRedisTemplate.opsForSet().size("currentAuthors:" + projectId);
Long numCurrentAuthorsForProject = stringRedisTemplate.opsForSet()
.size("currentAuthors:" + projectId);
if (numCurrentAuthorsForProject == 0) {
stringRedisTemplate.opsForSet().remove("currentlyAuthoredProjects", projectId.toString());
}
Expand All @@ -90,4 +101,48 @@ public void removeUser(UserDetails user) {
public Set<String> getCurrentAuthors(Serializable projectId) {
return stringRedisTemplate.opsForSet().members("currentAuthors:" + projectId);
}

public boolean isCkBoardAvailable() {
String ckBoardUrl = appProperties.getProperty("ck_board_url");
return ckBoardUrl != null && !ckBoardUrl.equals("");
}

public void signOutOfCkBoard(HttpServletRequest request) {
String ckSessionCookie = getCkSessionCookie(request);
HttpClient client = HttpClientBuilder.create().build();
HttpPost ckBoardLogoutRequest = new HttpPost(getCkBoardLogoutUrl());
ckBoardLogoutRequest.setHeader("Authorization", "Bearer " + ckSessionCookie);
try {
client.execute(ckBoardLogoutRequest);
} catch (IOException e) {
e.printStackTrace();
}
}

private String getCkBoardLogoutUrl() {
String ckBoardUrl = appProperties.getProperty("ck_board_url");

// The CK Board local backend url is only used for local development and should only be set in
// local development environments. When we are running locally, we need the local IP and port of
// the CK Board backend because the SCORE API is served using Docker. If the SCORE API makes a
// request to localhost:8001, it won't be able to access the CK Board backend. This is because
// the SCORE API expects localhost to be within the container but the CK Board backend is not in
// the container.
String ckBoardLocalBackendUrl = appProperties.getProperty("ck_board_local_backend_url");
if (ckBoardLocalBackendUrl != null && !ckBoardLocalBackendUrl.equals("")) {
ckBoardUrl = ckBoardLocalBackendUrl;
}
return ckBoardUrl + "/api/auth/logout";
}

private String getCkSessionCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("CK_SESSION")) {
return cookie.getValue();
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
import org.springframework.session.Session;
import org.wise.portal.service.session.SessionService;

public class WISELogoutHandler<S extends Session> implements LogoutHandler,
ApplicationListener<SessionDestroyedEvent> {
public class WISELogoutHandler<S extends Session>
implements LogoutHandler, ApplicationListener<SessionDestroyedEvent> {

@Autowired
protected SessionService sessionService;

@Override
public void logout(HttpServletRequest request, HttpServletResponse response,
public void logout(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) {
if (authentication != null) {
sessionService.removeUser((UserDetails) authentication.getPrincipal());
if (sessionService.isCkBoardAvailable()) {
sessionService.signOutOfCkBoard(request);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-dockerdev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,8 @@ google.tokens.dir=
ck_board_url=
ck_board_sso_secret_key=

# Only set this when in local development environment
#ck_board_local_backend_url=

# backwards compatibility purpose only.
system-wide-salt=secret