RestxSession.current().clearPrincipal();
RestxSession.current().define(String.class, Session.SESSION_DEF_KEY, null);
Map<String, ?> principal = getPrincipal(session);
if (principal == null) {
throw new WebException(HttpStatus.UNAUTHORIZED);
}
String name = (String) principal.get("name");
String passwordHash = (String) principal.get("passwordHash");
Optional<? extends RestxPrincipal> principalOptional = authenticator.authenticate(
name, passwordHash, ImmutableMap.copyOf(principal));
if (principalOptional.isPresent()) {
String sessionKey = uuidGenerator.doGenerate();
RestxSession.current().authenticateAs(principalOptional.get());
RestxSession.current().define(String.class, Session.SESSION_DEF_KEY, sessionKey);
return new Session(sessionKey, principalOptional.get());
} else {
throw new WebException(HttpStatus.UNAUTHORIZED);
}
}