@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if (VALID_USERS.containsKey(credentials.getUsername()) && "secret".equals(credentials.getPassword())) { return Optional.of(new User(credentials.getUsername(), VALID_USERS.get(credentials.getUsername()))); } return Optional.empty(); } }
import com.google.common.base.Optional; import io.dropwizard.auth.AuthenticationException; import io.dropwizard.auth.Authenticator; import io.dropwizard.auth.basic.BasicCredentials; public class SimpleAuthenticator implements Authenticator<BasicCredentials, SimplePrincipal> { @Override public Optional<SimplePrincipal> authenticate(BasicCredentials credentials) throws AuthenticationException { // Note: this is horrible authentication. Normally we'd use some // service to identify the password from the user name. if (!"pass".equals(credentials.getPassword())) { throw new AuthenticationException("Boo Hooo!"); } // from some user service get the roles for this user // I am explicitly setting it just for simplicity SimplePrincipal prince = new SimplePrincipal(credentials.getUsername()); prince.getRoles().add(Roles.ADMIN); return Optional.fromNullable(prince); } }
@Override public Optional<Account> authenticate(BasicCredentials basicCredentials) throws AuthenticationException { try { AuthorizationHeader authorizationHeader = AuthorizationHeader.fromUserAndPassword(basicCredentials.getUsername(), basicCredentials.getPassword()); Optional<Account> account = accountsManager.get(authorizationHeader.getNumber()); if (!account.isPresent()) { return Optional.empty(); } Optional<Device> device = account.get().getDevice(authorizationHeader.getDeviceId()); if (!device.isPresent()) { return Optional.empty(); } if (!device.get().isMaster() && device.get().isIdleInactive()) { return Optional.empty(); } if (device.get().getAuthenticationCredentials().verify(basicCredentials.getPassword())) { authenticationSucceededMeter.mark(); account.get().setAuthenticatedDevice(device.get()); updateLastSeen(account.get(), device.get()); return account; } authenticationFailedMeter.mark(); return Optional.empty(); } catch (InvalidAuthorizationHeaderException iahe) { return Optional.empty(); } }
/** * Creates a new BasicCredentials with the given username and password. * * @param credentials instance of BasicCredentials to wrap */ public SmartCosmosCredentials(BasicCredentials credentials) { super(credentials.getUsername(), credentials.getPassword()); }
public class BasicAuthenticator implements Authenticator<BasicCredentials, Subject> { @Override public Optional<Subject> authenticate(BasicCredentials credentials) throws AuthenticationException { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword(), false)); return Optional.of(subject); } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) { } catch (AuthenticationException ae) { } return Optional.absent(); } }
@Override public Optional<HelloAccount> authenticate(BasicCredentials credentials) throws AuthenticationException { if (credentials.getUsername().equals("moxie") && credentials.getPassword().equals("insecure")) { return Optional.of(new HelloAccount("moxie")); } return Optional.empty(); } }
public class AuthenticationService implements Authenticator<BasicCredentials, User>, Authorizer<User> { private final UserDAO userDAO; public AuthenticationService(UserDAO userDAO) { this.userDAO = userDAO; } public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { User user = userDAO.getByEmail(credentials.getUsername()); if (user != null && user.getPassword().equals(credentials.getPassword())) { return Optional.of(user); } return Optional.absent(); } public boolean authorize(User user, String role) { return user.hasRole(role); } }
@Override public Optional<Principal> authenticate(BasicCredentials credentials) throws AuthenticationException { String username = credentials.getUsername(); String password = credentials.getPassword(); String pass = users.get(username); if (pass != null && pass.equals(password)) { return Optional.of(new User(username)); } return Optional.empty(); }
@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if ("secret".equals(credentials.getPassword())) { return Optional.of(new User(credentials.getUsername())); } return Optional.empty(); } }
@Override public java.util.Optional<UserConfiguration> authenticate(BasicCredentials credentials) throws AuthenticationException { if (userConfiguration != null && userConfiguration.getUsername().equals(credentials.getUsername()) && userConfiguration.getPassword().equals(credentials.getPassword())) { return Optional.of(userConfiguration); } LOGGER.warn("Unauthorized access attempt: " + credentials.toString()); return Optional.empty(); } }
@Override public Optional<Server> authenticate(BasicCredentials credentials) throws AuthenticationException { for (Server server : servers) { if (MessageDigest.isEqual(server.getName().getBytes(), credentials.getUsername().getBytes()) && MessageDigest.isEqual(server.getPassword().getBytes(), credentials.getPassword().getBytes())) { return Optional.of(server); } } return Optional.absent(); } }
@Override public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException { if ("secret".equals(credentials.getPassword())) { return Optional.of(new User(credentials.getUsername())); } throw new AuthenticationException("Invalid credentials"); } }
private static boolean matchesTheCredentialsSuppliedByTheClient(UserCredentials userCredentials, BasicCredentials basicCredentials) { return userCredentials.getAuthField().equals( crypt(basicCredentials.getPassword(), userCredentials.getAuthField())); }
@Override public java.util.Optional<Principal> authenticate(BasicCredentials credentials) throws AuthenticationException { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword(), false)); User user = new User(subject); return Optional.of(user); } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) { logger.log(Level.WARNING, e.getMessage(), e); } catch (org.apache.shiro.authc.AuthenticationException ae) { logger.log(Level.WARNING, ae.getMessage(), ae); } return Optional.empty(); }
public Optional<User> authenticateAndReturnPermittedGroups(BasicCredentials credentials) throws io.dropwizard.auth.AuthenticationException { final String sanitizedUsername = sanitizeEntity(credentials.getUsername()); try { try (AutoclosingLdapContext context = buildContext(sanitizedUsername, credentials.getPassword())) { Set<String> groupMemberships = getGroupMembershipsIntersectingWithRestrictedGroups(context, sanitizedUsername); if (!groupMemberships.isEmpty()) { return Optional.of(new User(sanitizedUsername, groupMemberships)); } } } catch (AuthenticationException ae) { LOG.debug("{} failed to authenticate. {}", sanitizedUsername, ae); } catch (IOException | NamingException err) { throw new io.dropwizard.auth.AuthenticationException(String.format("LDAP Authentication failure (username: %s)", sanitizedUsername), err); } return Optional.empty(); }
public boolean authenticate(BasicCredentials credentials) throws io.dropwizard.auth.AuthenticationException { final String sanitizedUsername = sanitizeEntity(credentials.getUsername()); try { try (AutoclosingLdapContext context = buildContext(sanitizedUsername, credentials.getPassword())) { return filterByGroup(context, sanitizedUsername); } } catch (AuthenticationException ae) { LOG.debug("{} failed to authenticate. {}", sanitizedUsername, ae); } catch (IOException | NamingException err) { throw new io.dropwizard.auth.AuthenticationException(String.format("LDAP Authentication failure (username: %s)", sanitizedUsername), err); } return false; }
new BasicCredentials(credentials.getUsername(), credentials.getPassword()));