Skip to content

Commit 99acd3d

Browse files
committed
sessions - Introduced new CurrentSessionResolver component providing an extension point for retrieving current Session returned on GET /sessions/current
1 parent 8c14dff commit 99acd3d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package restx.security;
2+
3+
import com.google.common.base.Optional;
4+
import restx.factory.Component;
5+
6+
@Component
7+
public class CurrentSessionResolver {
8+
public Optional<Session> resolveCurrentSession(){
9+
Optional<String> sessionKey = RestxSession.current().get(String.class, Session.SESSION_DEF_KEY);
10+
Optional<? extends RestxPrincipal> principal = RestxSession.current().getPrincipal();
11+
12+
if(!sessionKey.isPresent() || !principal.isPresent()) {
13+
return Optional.absent();
14+
}
15+
16+
return Optional.of(new Session(sessionKey.get(), principal.get()));
17+
}
18+
}

restx-security-basic/src/main/java/restx/security/SessionResource.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public class SessionResource {
2020
private final BasicPrincipalAuthenticator authenticator;
2121
private final UUIDGenerator uuidGenerator;
2222
private SessionInvalider sessionInvalider;
23+
private CurrentSessionResolver currentSessionResolver;
2324

24-
public SessionResource(BasicPrincipalAuthenticator authenticator, UUIDGenerator uuidGenerator, SessionInvalider sessionInvalider) {
25+
public SessionResource(
26+
BasicPrincipalAuthenticator authenticator, UUIDGenerator uuidGenerator,
27+
SessionInvalider sessionInvalider, CurrentSessionResolver currentSessionResolver) {
2528
this.authenticator = authenticator;
2629
this.uuidGenerator = uuidGenerator;
2730
this.sessionInvalider = sessionInvalider;
31+
this.currentSessionResolver = currentSessionResolver;
2832
}
2933

3034

@@ -62,14 +66,7 @@ public Session authenticate(Map session) {
6266

6367
@GET("/sessions/current")
6468
public Optional<Session> currentSession() {
65-
Optional<String> sessionKey = RestxSession.current().get(String.class, Session.SESSION_DEF_KEY);
66-
Optional<? extends RestxPrincipal> principal = RestxSession.current().getPrincipal();
67-
68-
if(!sessionKey.isPresent() || !principal.isPresent()) {
69-
return Optional.absent();
70-
}
71-
72-
return Optional.of(new Session(sessionKey.get(), principal.get()));
69+
return currentSessionResolver.resolveCurrentSession();
7370
}
7471

7572
@PermitAll

0 commit comments

Comments
 (0)