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.
@@ -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()));
}
}
Expand Up @@ -20,11 +20,15 @@ public class SessionResource {
private final BasicPrincipalAuthenticator authenticator; private final BasicPrincipalAuthenticator authenticator;
private final UUIDGenerator uuidGenerator; private final UUIDGenerator uuidGenerator;
private SessionInvalider sessionInvalider; 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.authenticator = authenticator;
this.uuidGenerator = uuidGenerator; this.uuidGenerator = uuidGenerator;
this.sessionInvalider = sessionInvalider; this.sessionInvalider = sessionInvalider;
this.currentSessionResolver = currentSessionResolver;
} }




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


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

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

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


@PermitAll @PermitAll
Expand Down

0 comments on commit 99acd3d

Please sign in to comment.