PasswordAuthenticator implements IAuthenticator { @Override public UserIdentity authenticate(final Authenticator authenticator, final MultivaluedMap<String, String> parameters, final URI callback) { if (authenticator == null || parameters == null) { throw new InvalidRequestException(); } String login = ParamUtil.getOne(parameters, "username"); String password = ParamUtil.getOne(parameters, "password"); Criteria c = session.createCriteria(UserIdentity.class); c.add(Restrictions.eq("remoteId", login)); c.add(Restrictions.eq("type", authenticator.getType())); c.createAlias("user", "u"); c.add(Restrictions.eq("u.application", authenticator.getClient().getApplication())); c.setFirstResult(0); c.setMaxResults(1); List<UserIdentity> results = c.list(); if (c.list().size() == 0) { return null; } UserIdentity identity = results.get(0); Boolean isValid = PasswordUtil.isValid(password, identity.getSalt(), identity.getPassword()); if (!isValid) { return null; } return identity; } @Inject  PasswordAuthenticator(final Session session); @Override Response delegate(final Authenticator configuration,
                             final URI callback); @Override UserIdentity authenticate(final Authenticator authenticator,
                                     final MultivaluedMap<String, String>
                                             parameters,
                                     final URI callback);  }
@Test public void testAuthenticateValid() throws Exception { IAuthenticator a = new PasswordAuthenticator(getSession()); MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); params.add("username", "login"); params.add("password", "password"); UserIdentity i = a.authenticate(context.getAuthenticator(), params, null); assertEquals("login", i.getRemoteId()); }
@Test(expected = InvalidRequestException.class) public void testAuthenticateNullConfig() throws Exception { IAuthenticator a = new PasswordAuthenticator(getSession()); MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); a.authenticate(null, params, null); }
@Test(expected = InvalidRequestException.class) public void testAuthenticateNullParams() throws Exception { IAuthenticator a = new PasswordAuthenticator(getSession()); Authenticator config = new Authenticator(); a.authenticate(config, null, null); }
@Test public void testAuthenticateNoIdentity() throws Exception { IAuthenticator a = new PasswordAuthenticator(getSession()); MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); params.add("username", "wrongIdentity"); params.add("password", "password"); UserIdentity i = a.authenticate(context.getAuthenticator(), params, null); assertNull(i); }
@Test public void testAuthenticateWrongPassword() throws Exception { IAuthenticator a = new PasswordAuthenticator(getSession()); MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); params.add("username", "login"); params.add("password", "wrongpassword"); UserIdentity i = a.authenticate(context.getAuthenticator(), params, null); assertNull(i); }