use of nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException in project timbuctoo by HuygensING.
the class LocalFileUserAccess method addUser.
@Override
public void addUser(User user) throws AuthenticationUnavailableException {
final List<User> users;
try {
synchronized (usersFile) {
users = objectMapper.readValue(usersFile.toFile(), new TypeReference<List<User>>() {
});
}
users.add(user);
objectMapper.writeValue(usersFile.toFile(), users.toArray(new User[users.size()]));
} catch (IOException e) {
JsonBasedUserStore.LOG.error("Cannot read {}", usersFile.toAbsolutePath());
JsonBasedUserStore.LOG.error("Exception thrown", e);
throw new AuthenticationUnavailableException(e.getMessage());
}
}
use of nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException in project timbuctoo by HuygensING.
the class LoggedInUsers method userFor.
public Optional<User> userFor(String authHeader) {
if (authHeader == null || authHeader.isEmpty()) {
return Optional.empty();
} else {
User local = users.getIfPresent(authHeader);
if (local != null) {
return Optional.of(local);
} else {
try {
SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader);
// get the one that was saved to the file
Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID());
if (userFromFile.isPresent()) {
users.put(authHeader, userFromFile.get());
return userFromFile;
} else {
User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID());
users.put(authHeader, nw);
return Optional.of(nw);
}
} catch (UnauthorizedException e) {
LOG.warn("User is not retrievable", e);
return Optional.empty();
} catch (IOException | AuthenticationUnavailableException e) {
LOG.error("An exception is thrown while retrieving the user information.", e);
return Optional.empty();
}
}
}
}
use of nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException in project timbuctoo by HuygensING.
the class LoggedInUsersTest method throwsAnAuthenticationUnavailableExceptionWhenTheUserCouldNotBeRetrievedDueToASystemError.
@Test
public void throwsAnAuthenticationUnavailableExceptionWhenTheUserCouldNotBeRetrievedDueToASystemError() throws Exception {
UserStore userStore = mock(JsonBasedUserStore.class);
given(userStore.userFor(anyString())).willThrow(new AuthenticationUnavailableException(""));
Authenticator authenticator = AuthenticatorMockBuilder.authenticator().withPidFor("a", "b", "pid").build();
LoggedInUsers instance = new LoggedInUsers(authenticator, userStore, ONE_SECOND_TIMEOUT, null);
expectedException.expect(AuthenticationUnavailableException.class);
instance.userTokenFor("a", "b");
}
Aggregations