Search in sources :

Example 1 with Login

use of nl.knaw.huygens.timbuctoo.security.dto.Login in project timbuctoo by HuygensING.

the class LocalFileLoginAccess method addLogin.

@Override
public void addLogin(Login login) throws LoginCreationException {
    synchronized (loginsFile) {
        try {
            List<Login> logins = objectMapper.readValue(loginsFile.toFile(), new TypeReference<List<Login>>() {
            });
            if (containsLoginForUserName(logins, login.getUsername())) {
                LOG.warn("Already contains a login for userName '{}'. No login added.", login.getUsername());
                return;
            }
            if (logins.stream().anyMatch(itemLogin -> Objects.equals(itemLogin.getUserPid(), login.getUserPid()))) {
                LOG.warn("Already contains a login for userPid '{}'. No login added", login.getUserPid());
                return;
            }
            logins.add(login);
            objectMapper.writeValue(loginsFile.toFile(), logins.toArray(new Login[logins.size()]));
        } catch (IOException e) {
            LOG.error("Could not read \"{}\"", loginsFile.toAbsolutePath());
            LOG.error("Exception", e);
            throw new LoginCreationException(e.getMessage());
        }
    }
}
Also used : LoginCreationException(nl.knaw.huygens.timbuctoo.security.exceptions.LoginCreationException) List(java.util.List) Login(nl.knaw.huygens.timbuctoo.security.dto.Login) IOException(java.io.IOException)

Example 2 with Login

use of nl.knaw.huygens.timbuctoo.security.dto.Login in project timbuctoo by HuygensING.

the class JsonBasedAuthenticatorTest method createLoginAddsALoginToTheLoginsFile.

@Test
public void createLoginAddsALoginToTheLoginsFile() throws Exception {
    Login[] logins = new Login[0];
    Path emptyLoginsFile = FileHelpers.makeTempFilePath(true);
    new ObjectMapper().writeValue(emptyLoginsFile.toFile(), logins);
    JsonBasedAuthenticator instance = backedByFile(emptyLoginsFile);
    instance.createLogin("userPid", "userName", "password", "givenName", "surname", "email", "org");
    Optional<String> authenticate = instance.authenticate("userName", "password");
    assertThat(authenticate, is(present()));
    Files.delete(emptyLoginsFile);
}
Also used : Path(java.nio.file.Path) Login(nl.knaw.huygens.timbuctoo.security.dto.Login) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Login

use of nl.knaw.huygens.timbuctoo.security.dto.Login in project timbuctoo by HuygensING.

the class JsonBasedAuthenticator method createLogin.

@Override
public void createLogin(String userPid, String userName, String password, String givenName, String surname, String email, String organization) throws LoginCreationException {
    try {
        Login login = create(userPid, userName, password, givenName, surname, email, organization);
        loginAccess.addLogin(login);
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Encryption algorithm can not be found.", e);
        throw new LoginCreationException(e.getMessage());
    }
}
Also used : LoginCreationException(nl.knaw.huygens.timbuctoo.security.exceptions.LoginCreationException) Login(nl.knaw.huygens.timbuctoo.security.dto.Login) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with Login

use of nl.knaw.huygens.timbuctoo.security.dto.Login in project timbuctoo by HuygensING.

the class AzureAccessTest method testLogin.

@Test
public void testLogin() throws Exception {
    AzureLoginAccess instance = new AzureLoginAccess(tableClient);
    Login login = Login.create("userName", "userPid", new byte[] { 'a' }, new byte[] { 'a' }, "", "", "", "");
    instance.addLogin(login);
    Optional<Login> retrievedLogin = instance.getLogin("userName");
    assertThat(retrievedLogin, is(present()));
    assertThat(retrievedLogin.get(), is(login));
}
Also used : Login(nl.knaw.huygens.timbuctoo.security.dto.Login) Test(org.junit.Test)

Example 5 with Login

use of nl.knaw.huygens.timbuctoo.security.dto.Login in project timbuctoo by HuygensING.

the class JsonBasedAuthenticatorTest method createLoginIgnoresTheAdditionOfLoginOfAKnownUserPid.

@Test
public void createLoginIgnoresTheAdditionOfLoginOfAKnownUserPid() throws Exception {
    Login[] logins = new Login[0];
    Path emptyLoginsFile = FileHelpers.makeTempFilePath(true);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writeValue(emptyLoginsFile.toFile(), logins);
    JsonBasedAuthenticator instance = backedByFile(emptyLoginsFile);
    String userPid = "userPid";
    instance.createLogin(userPid, "userName", "password", "givenName", "surname", "email", "org");
    instance.createLogin(userPid, "userName1", "password1", "givenName1", "surname1", "email1", "org1");
    List<Login> loginList = objectMapper.readValue(emptyLoginsFile.toFile(), new TypeReference<List<Login>>() {
    });
    long count = loginList.stream().filter(login -> Objects.equals(login.getUserPid(), userPid)).count();
    assertThat(count, is(1L));
    Files.delete(emptyLoginsFile);
}
Also used : Path(java.nio.file.Path) FileHelpers(nl.knaw.huygens.timbuctoo.util.FileHelpers) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Test(org.junit.Test) JsonBasedAuthenticatorStubs.backedByFile(nl.knaw.huygens.timbuctoo.security.JsonBasedAuthenticatorStubs.backedByFile) Objects(java.util.Objects) LocalLoginUnavailableException(nl.knaw.huygens.timbuctoo.security.exceptions.LocalLoginUnavailableException) List(java.util.List) Rule(org.junit.Rule) JsonBasedAuthenticatorStubs.throwingWithAlgorithm(nl.knaw.huygens.timbuctoo.security.JsonBasedAuthenticatorStubs.throwingWithAlgorithm) Paths(java.nio.file.Paths) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Is.is(org.hamcrest.core.Is.is) Optional(java.util.Optional) OptionalPresentMatcher.present(nl.knaw.huygens.hamcrest.OptionalPresentMatcher.present) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Login(nl.knaw.huygens.timbuctoo.security.dto.Login) ExpectedException(org.junit.rules.ExpectedException) Path(java.nio.file.Path) LoginCreationException(nl.knaw.huygens.timbuctoo.security.exceptions.LoginCreationException) Before(org.junit.Before) List(java.util.List) Login(nl.knaw.huygens.timbuctoo.security.dto.Login) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

Login (nl.knaw.huygens.timbuctoo.security.dto.Login)6 LoginCreationException (nl.knaw.huygens.timbuctoo.security.exceptions.LoginCreationException)4 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Path (java.nio.file.Path)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 List (java.util.List)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 OptionalPresentMatcher.present (nl.knaw.huygens.hamcrest.OptionalPresentMatcher.present)2 JsonBasedAuthenticatorStubs.backedByFile (nl.knaw.huygens.timbuctoo.security.JsonBasedAuthenticatorStubs.backedByFile)2 JsonBasedAuthenticatorStubs.throwingWithAlgorithm (nl.knaw.huygens.timbuctoo.security.JsonBasedAuthenticatorStubs.throwingWithAlgorithm)2 LocalLoginUnavailableException (nl.knaw.huygens.timbuctoo.security.exceptions.LocalLoginUnavailableException)2 FileHelpers (nl.knaw.huygens.timbuctoo.util.FileHelpers)2 CoreMatchers.not (org.hamcrest.CoreMatchers.not)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Is.is (org.hamcrest.core.Is.is)2