Skip to content

Commit 958a334

Browse files
geoffreykwanhirokiterashima
authored andcommitted
feat(CK Board): Log out of CK Board when user logs out of SCORE (#8)
1 parent 1ca0170 commit 958a334

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/main/java/org/wise/portal/service/session/SessionService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.Serializable;
44
import java.util.Set;
55

6+
import javax.servlet.http.HttpServletRequest;
7+
68
import org.springframework.security.core.userdetails.UserDetails;
79

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

3032
void removeUser(UserDetails user);
33+
34+
boolean isCkBoardAvailable();
35+
36+
void signOutOfCkBoard(HttpServletRequest request);
3137
}

src/main/java/org/wise/portal/service/session/impl/SessionServiceImpl.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package org.wise.portal.service.session.impl;
22

3+
import java.io.IOException;
34
import java.io.Serializable;
45
import java.util.Set;
56

7+
import javax.servlet.http.Cookie;
8+
import javax.servlet.http.HttpServletRequest;
9+
10+
import org.apache.http.client.HttpClient;
11+
import org.apache.http.client.methods.HttpPost;
12+
import org.apache.http.impl.client.HttpClientBuilder;
613
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.core.env.Environment;
715
import org.springframework.data.redis.core.StringRedisTemplate;
816
import org.springframework.security.core.userdetails.UserDetails;
917
import org.springframework.session.Session;
@@ -15,6 +23,9 @@
1523
@Service
1624
public class SessionServiceImpl<S extends Session> implements SessionService {
1725

26+
@Autowired
27+
private Environment appProperties;
28+
1829
@Autowired
1930
private StringRedisTemplate stringRedisTemplate;
2031

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

6475
@Override
6576
public void removeCurrentAuthor(UserDetails author) {
66-
Set<String> currentlyAuthoredProjects =
67-
stringRedisTemplate.opsForSet().members("currentlyAuthoredProjects");
77+
Set<String> currentlyAuthoredProjects = stringRedisTemplate.opsForSet()
78+
.members("currentlyAuthoredProjects");
6879
for (String projectId : currentlyAuthoredProjects) {
6980
removeCurrentAuthor(projectId, author.getUsername());
7081
}
7182
}
7283

7384
public void removeCurrentAuthor(Serializable projectId, String authorUsername) {
7485
stringRedisTemplate.opsForSet().remove("currentAuthors:" + projectId, authorUsername);
75-
Long numCurrentAuthorsForProject =
76-
stringRedisTemplate.opsForSet().size("currentAuthors:" + projectId);
86+
Long numCurrentAuthorsForProject = stringRedisTemplate.opsForSet()
87+
.size("currentAuthors:" + projectId);
7788
if (numCurrentAuthorsForProject == 0) {
7889
stringRedisTemplate.opsForSet().remove("currentlyAuthoredProjects", projectId.toString());
7990
}
@@ -90,4 +101,48 @@ public void removeUser(UserDetails user) {
90101
public Set<String> getCurrentAuthors(Serializable projectId) {
91102
return stringRedisTemplate.opsForSet().members("currentAuthors:" + projectId);
92103
}
104+
105+
public boolean isCkBoardAvailable() {
106+
String ckBoardUrl = appProperties.getProperty("ck_board_url");
107+
return ckBoardUrl != null && !ckBoardUrl.equals("");
108+
}
109+
110+
public void signOutOfCkBoard(HttpServletRequest request) {
111+
String ckSessionCookie = getCkSessionCookie(request);
112+
HttpClient client = HttpClientBuilder.create().build();
113+
HttpPost ckBoardLogoutRequest = new HttpPost(getCkBoardLogoutUrl());
114+
ckBoardLogoutRequest.setHeader("Authorization", "Bearer " + ckSessionCookie);
115+
try {
116+
client.execute(ckBoardLogoutRequest);
117+
} catch (IOException e) {
118+
e.printStackTrace();
119+
}
120+
}
121+
122+
private String getCkBoardLogoutUrl() {
123+
String ckBoardUrl = appProperties.getProperty("ck_board_url");
124+
125+
// The CK Board local backend url is only used for local development and should only be set in
126+
// local development environments. When we are running locally, we need the local IP and port of
127+
// the CK Board backend because the SCORE API is served using Docker. If the SCORE API makes a
128+
// request to localhost:8001, it won't be able to access the CK Board backend. This is because
129+
// the SCORE API expects localhost to be within the container but the CK Board backend is not in
130+
// the container.
131+
String ckBoardLocalBackendUrl = appProperties.getProperty("ck_board_local_backend_url");
132+
if (ckBoardLocalBackendUrl != null && !ckBoardLocalBackendUrl.equals("")) {
133+
ckBoardUrl = ckBoardLocalBackendUrl;
134+
}
135+
return ckBoardUrl + "/api/auth/logout";
136+
}
137+
138+
private String getCkSessionCookie(HttpServletRequest request) {
139+
Cookie[] cookies = request.getCookies();
140+
for (Cookie cookie : cookies) {
141+
if (cookie.getName().equals("CK_SESSION")) {
142+
return cookie.getValue();
143+
}
144+
}
145+
return null;
146+
}
147+
93148
}

src/main/java/org/wise/portal/spring/impl/WISELogoutHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import org.springframework.session.Session;
3939
import org.wise.portal.service.session.SessionService;
4040

41-
public class WISELogoutHandler<S extends Session> extends SecurityContextLogoutHandler implements
42-
ApplicationListener<SessionDestroyedEvent> {
41+
public class WISELogoutHandler<S extends Session> extends SecurityContextLogoutHandler
42+
implements ApplicationListener<SessionDestroyedEvent> {
4343

4444
@Autowired
4545
protected SessionService sessionService;
@@ -49,6 +49,9 @@ public void logout(HttpServletRequest request, HttpServletResponse response,
4949
Authentication authentication) {
5050
if (authentication != null) {
5151
sessionService.removeUser((UserDetails) authentication.getPrincipal());
52+
if (sessionService.isCkBoardAvailable()) {
53+
sessionService.signOutOfCkBoard(request);
54+
}
5255
}
5356
super.logout(request, response, authentication);
5457
}

src/main/resources/application-dockerdev-sample.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ google.tokens.dir=
194194
ck_board_url=
195195
ck_board_sso_secret_key=
196196

197+
# Only set this when in local development environment
198+
#ck_board_local_backend_url=
199+
197200
# backwards compatibility purpose only.
198201
system-wide-salt=secret
199202

0 commit comments

Comments
 (0)