Skip to content

Commit

Permalink
sessions - Introduced new CurrentSessionResolver component providing …
Browse files Browse the repository at this point in the history
…an extension point for retrieving current Session returned on GET /sessions/current
  • Loading branch information
fcamblor committed Aug 21, 2017
1 parent 8c14dff commit 99acd3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package restx.security;

import com.google.common.base.Optional;
import restx.factory.Component;

@Component
public class CurrentSessionResolver {
public Optional<Session> resolveCurrentSession(){
Optional<String> sessionKey = RestxSession.current().get(String.class, Session.SESSION_DEF_KEY);
Optional<? extends RestxPrincipal> principal = RestxSession.current().getPrincipal();

if(!sessionKey.isPresent() || !principal.isPresent()) {
return Optional.absent();
}

return Optional.of(new Session(sessionKey.get(), principal.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public class SessionResource {
private final BasicPrincipalAuthenticator authenticator;
private final UUIDGenerator uuidGenerator;
private SessionInvalider sessionInvalider;
private CurrentSessionResolver currentSessionResolver;

public SessionResource(BasicPrincipalAuthenticator authenticator, UUIDGenerator uuidGenerator, SessionInvalider sessionInvalider) {
public SessionResource(
BasicPrincipalAuthenticator authenticator, UUIDGenerator uuidGenerator,
SessionInvalider sessionInvalider, CurrentSessionResolver currentSessionResolver) {
this.authenticator = authenticator;
this.uuidGenerator = uuidGenerator;
this.sessionInvalider = sessionInvalider;
this.currentSessionResolver = currentSessionResolver;
}


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

@GET("/sessions/current")
public Optional<Session> currentSession() {
Optional<String> sessionKey = RestxSession.current().get(String.class, Session.SESSION_DEF_KEY);
Optional<? extends RestxPrincipal> principal = RestxSession.current().getPrincipal();

if(!sessionKey.isPresent() || !principal.isPresent()) {
return Optional.absent();
}

return Optional.of(new Session(sessionKey.get(), principal.get()));
return currentSessionResolver.resolveCurrentSession();
}

@PermitAll
Expand Down

0 comments on commit 99acd3d

Please sign in to comment.