@Test public void invalidAuthenticationCredentials() { expectedEx.expect(AuthenticationException.class); expectedEx.expectMessage("The username/password was invalid"); realm.doGetAuthenticationInfo(new UsernamePasswordToken("unknownUser", "password")); }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; }
DatastoreRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; } }
DatastoreRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; } @Inject  DatastoreRealm(UserService userService, RoleService roleService,
            CredentialsMatcher credentialsMatcher); }
DatastoreRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; } @Inject  DatastoreRealm(UserService userService, RoleService roleService,
            CredentialsMatcher credentialsMatcher); @Override CredentialsMatcher getCredentialsMatcher(); void destroy(); }
DatastoreRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { throw new AuthenticationException( "The provided token is not a UsernamePasswordToken"); } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String username = usernamePasswordToken.getUsername(); if (username == null) { throw new AuthenticationException( "The provided token does not contain a username"); } User user = getUserByUsernameOrEmail(username); if (user == null) { LOG.warn("User with the specified username does not exist: " + username); throw new AuthenticationException("The username/password was invalid"); } if (!user.getEnabled()) { throw new AuthenticationException("The user account is disabled"); } SimpleAccount info = new SimpleAccount(username, user.getPassword(), new SimpleByteSource(user.getPasswordSalt()), getName()); SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(user.getId(), getName()); principals.add(user.getUsername(), getName()); principals.add(user.getEmail(), getName()); info.setPrincipals(principals); return info; } @Inject  DatastoreRealm(UserService userService, RoleService roleService,
            CredentialsMatcher credentialsMatcher); @Override CredentialsMatcher getCredentialsMatcher(); void destroy();  }