Search in sources :

Example 1 with UnauthorizedException

use of nl.knaw.huygens.security.client.UnauthorizedException 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 2 with UnauthorizedException

use of nl.knaw.huygens.security.client.UnauthorizedException in project timbuctoo by HuygensING.

the class LoggedInUsersTest method setUp.

@Before
public void setUp() throws Exception {
    Authenticator authenticator = AuthenticatorMockBuilder.authenticator().withPidFor("a", "b", "pid").build();
    UserStore userStore = userStore().withUserFor("pid").build();
    MockAuthenticationHandler authHandler = new MockAuthenticationHandler();
    userStoreWithUserA = new LoggedInUsers(authenticator, userStore, ONE_SECOND_TIMEOUT, x -> {
        throw new UnauthorizedException();
    });
    Authenticator authenticator1 = AuthenticatorMockBuilder.authenticator().withPidFor("a", "b", "pid").withPidFor("c", "d", "otherPid").build();
    UserStore userStore1 = userStore().withUserFor("pid").withUserFor("otherPid").build();
    userStoreWithUserAAndB = new LoggedInUsers(authenticator1, userStore1, ONE_SECOND_TIMEOUT, x -> {
        throw new UnauthorizedException();
    });
}
Also used : Timeout(nl.knaw.huygens.timbuctoo.util.Timeout) UnauthorizedException(nl.knaw.huygens.security.client.UnauthorizedException) Matchers.not(org.hamcrest.Matchers.not) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) AuthenticationUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException) LocalLoginUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.LocalLoginUnavailableException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) Rule(org.junit.Rule) BDDMockito.given(org.mockito.BDDMockito.given) MockAuthenticationHandler(nl.knaw.huygens.security.client.MockAuthenticationHandler) Is.is(org.hamcrest.core.Is.is) Optional(java.util.Optional) OptionalPresentMatcher.present(nl.knaw.huygens.hamcrest.OptionalPresentMatcher.present) UserStoreMockBuilder.userStore(nl.knaw.huygens.timbuctoo.security.UserStoreMockBuilder.userStore) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExpectedException(org.junit.rules.ExpectedException) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Before(org.junit.Before) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) MockAuthenticationHandler(nl.knaw.huygens.security.client.MockAuthenticationHandler) UnauthorizedException(nl.knaw.huygens.security.client.UnauthorizedException) Before(org.junit.Before)

Aggregations

UnauthorizedException (nl.knaw.huygens.security.client.UnauthorizedException)2 AuthenticationUnavailableException (nl.knaw.huygens.timbuctoo.security.exceptions.AuthenticationUnavailableException)2 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)2 IOException (java.io.IOException)1 Optional (java.util.Optional)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 OptionalPresentMatcher.present (nl.knaw.huygens.hamcrest.OptionalPresentMatcher.present)1 MockAuthenticationHandler (nl.knaw.huygens.security.client.MockAuthenticationHandler)1 SecurityInformation (nl.knaw.huygens.security.client.model.SecurityInformation)1 UserStoreMockBuilder.userStore (nl.knaw.huygens.timbuctoo.security.UserStoreMockBuilder.userStore)1 LocalLoginUnavailableException (nl.knaw.huygens.timbuctoo.security.exceptions.LocalLoginUnavailableException)1 Timeout (nl.knaw.huygens.timbuctoo.util.Timeout)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 Matchers.not (org.hamcrest.Matchers.not)1 Is.is (org.hamcrest.core.Is.is)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1