Search in sources :

Example 1 with AuthenticationUnavailableException

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());
    }
}
Also used : AuthenticationUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Example 2 with AuthenticationUnavailableException

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();
            }
        }
    }
}
Also used : AuthenticationUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) SecurityInformation(nl.knaw.huygens.security.client.model.SecurityInformation) UnauthorizedException(nl.knaw.huygens.security.client.UnauthorizedException) IOException(java.io.IOException)

Example 3 with AuthenticationUnavailableException

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");
}
Also used : AuthenticationUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException) Test(org.junit.Test)

Aggregations

AuthenticationUnavailableException (nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException)3 IOException (java.io.IOException)2 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 UnauthorizedException (nl.knaw.huygens.security.client.UnauthorizedException)1 SecurityInformation (nl.knaw.huygens.security.client.model.SecurityInformation)1 Test (org.junit.Test)1