11package org .wise .portal .service .session .impl ;
22
3+ import java .io .IOException ;
34import java .io .Serializable ;
45import 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 ;
613import org .springframework .beans .factory .annotation .Autowired ;
14+ import org .springframework .core .env .Environment ;
715import org .springframework .data .redis .core .StringRedisTemplate ;
816import org .springframework .security .core .userdetails .UserDetails ;
917import org .springframework .session .Session ;
1523@ Service
1624public 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}
0 commit comments