GrapesAuthenticator implements Authenticator<BasicCredentials, DbCredential> { @Override public Optional<DbCredential> authenticate(BasicCredentials credentials) throws AuthenticationException { if(credentials == null || credentials.getUsername() == null || credentials.getPassword() == null){ LOG.error("Missing credentials for the authentication"); return Optional.absent(); } final String encryptedPwd = encrypt(credentials.getPassword()); final DbCredential dbCredential = repoHandler.getCredential(credentials.getUsername()); if(dbCredential != null && encryptedPwd.equals(dbCredential.getPassword())){ return Optional.of(dbCredential); } return Optional.absent(); } GrapesAuthenticator(final RepositoryHandler repoHandler); @Override Optional<DbCredential> authenticate(BasicCredentials credentials); static String encrypt(final String password);  }
@Test public void checkAutentication() throws AuthenticationException, UnknownHostException{ final RepositoryHandler repositoryHandler = GrapesTestUtils.getRepoHandlerMock(); final GrapesAuthenticator authenticator = new GrapesAuthenticator(repositoryHandler); Exception exception = null; Optional<DbCredential> result = null; try { result = authenticator.authenticate(new BasicCredentials(GrapesTestUtils.USER_4TEST, GrapesTestUtils.PASSWORD_4TEST)); } catch (Exception e) { exception = e; } assertNull(exception); assertNotNull(result); assertTrue(result.isPresent()); final List<AvailableRoles> roles = result.get().getRoles(); assertNotNull(roles); assertTrue(roles.contains(AvailableRoles.ARTIFACT_CHECKER)); assertTrue(roles.contains(AvailableRoles.DATA_DELETER)); assertTrue(roles.contains(AvailableRoles.DATA_UPDATER)); assertTrue(roles.contains(AvailableRoles.DEPENDENCY_NOTIFIER)); assertTrue(roles.contains(AvailableRoles.LICENSE_CHECKER)); }
@Test public void checkWrongAutentication() throws AuthenticationException, UnknownHostException{ final RepositoryHandler repositoryHandler = GrapesTestUtils.getRepoHandlerMock(); final GrapesAuthenticator authenticator = new GrapesAuthenticator(repositoryHandler); Exception exception = null; Optional<DbCredential> result = null; try { result = authenticator.authenticate(new BasicCredentials(GrapesTestUtils.USER_4TEST, "wrongPassword")); } catch (Exception e) { exception = e; } assertNull(exception); assertNotNull(result); assertFalse(result.isPresent()); exception = null; result = null; try { result = authenticator.authenticate(new BasicCredentials("userWrong", GrapesTestUtils.PASSWORD_4TEST)); } catch (Exception e) { exception = e; } assertNull(exception); assertNotNull(result); assertFalse(result.isPresent()); exception = null; result = null; try { result = authenticator.authenticate(null); } catch (Exception e) { exception = e; } assertNull(exception); assertNotNull(result); assertFalse(result.isPresent()); }