@Test public void testGetters() { RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("key", "Test", ROLES_12); assertThat(token.getKeyHash()).isEqualTo("key".hashCode()); assertThat(token.getPrincipal()).isEqualTo("Test"); assertThat(token.getCredentials()).isEqualTo(""); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_ONE"); assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_TWO"); assertThat(token.isAuthenticated()).isTrue(); }
@Test public void testSetAuthenticatedIgnored() { RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("key", "Test", ROLES_12); assertThat(token.isAuthenticated()).isTrue(); token.setAuthenticated(false); assertThat(!token.isAuthenticated()).isTrue(); } }
@Test public void testNotEqualsDueToKey() { RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12); RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken( "DIFFERENT_KEY", "Test", ROLES_12); assertThat(token1.equals(token2)).isFalse(); }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof RememberMeAuthenticationToken) { RememberMeAuthenticationToken test = (RememberMeAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
@Test public void testEqualsWhenEqual() { RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12); RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken("key", "Test", ROLES_12); assertThat(token2).isEqualTo(token1); }
/** * Private Constructor to help in Jackson deserialization. * * @param keyHash hashCode of above given key. * @param principal the principal (typically a <code>UserDetails</code>) * @param authorities the authorities granted to the principal * @since 4.2 */ private RememberMeAuthenticationToken(Integer keyHash, Object principal, Collection<? extends GrantedAuthority> authorities) { super(authorities); this.keyHash = keyHash; this.principal = principal; setAuthenticated(true); }
@Override protected Authentication createSuccessfulAuthentication( HttpServletRequest request, UserDetails user) { if (user instanceof RememberMeUserDetails) user = ((RememberMeUserDetails) user).getWrappedObject(); Collection<GrantedAuthority> roles = new HashSet<GrantedAuthority>(); if (user.getAuthorities().contains(GeoServerRole.AUTHENTICATED_ROLE)) { roles.addAll(user.getAuthorities()); } else { roles = new HashSet<GrantedAuthority>(); roles.addAll(user.getAuthorities()); roles.add(GeoServerRole.AUTHENTICATED_ROLE); } RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(getKey(), user, roles); auth.setDetails(getAuthenticationDetailsSource().buildDetails(request)); return auth; } }
@Test public void serializeRememberMeAuthenticationWithUserTokenAfterEraseCredential() throws JsonProcessingException, JSONException { User user = createDefaultUser(); RememberMeAuthenticationToken token = new RememberMeAuthenticationToken(REMEMBERME_KEY, user, user.getAuthorities()); token.eraseCredentials(); String actualJson = mapper.writeValueAsString(token); JSONAssert.assertEquals(REMEMBERME_AUTH_JSON.replace(UserDeserializerTests.USER_PASSWORD, "null"), actualJson, true); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; final Pac4jRememberMeAuthenticationToken that = (Pac4jRememberMeAuthenticationToken) o; return profiles != null ? profiles.equals(that.profiles) : that.profiles == null; }
@Override public boolean equals(Object obj) { if (!super.equals(obj)) { return false; } if (obj instanceof RememberMeAuthenticationToken) { RememberMeAuthenticationToken test = (RememberMeAuthenticationToken) obj; if (this.getKeyHash() != test.getKeyHash()) { return false; } return true; } return false; }
private Authentication createRememberMe() { return new RememberMeAuthenticationToken("ignored", "ignored", AuthorityUtils.createAuthorityList("ignored")); }
/** * Private Constructor to help in Jackson deserialization. * * @param keyHash hashCode of above given key. * @param principal the principal (typically a <code>UserDetails</code>) * @param authorities the authorities granted to the principal * @since 4.2 */ private RememberMeAuthenticationToken(Integer keyHash, Object principal, Collection<? extends GrantedAuthority> authorities) { super(authorities); this.keyHash = keyHash; this.principal = principal; setAuthenticated(true); }
/** * Creates the final <tt>Authentication</tt> object returned from the * <tt>autoLogin</tt> method. * <p> * By default it will create a <tt>RememberMeAuthenticationToken</tt> instance. * * @param request the original request. The configured * <tt>AuthenticationDetailsSource</tt> will use this to build the details property of * the returned object. * @param user the <tt>UserDetails</tt> loaded from the <tt>UserDetailsService</tt>. * This will be stored as the principal. * * @return the <tt>Authentication</tt> for the remember-me authenticated user */ protected Authentication createSuccessfulAuthentication(HttpServletRequest request, UserDetails user) { RememberMeAuthenticationToken auth = new RememberMeAuthenticationToken(key, user, authoritiesMapper.mapAuthorities(user.getAuthorities())); auth.setDetails(authenticationDetailsSource.buildDetails(request)); return auth; }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((RememberMeAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("RememberMeAuthenticationProvider.incorrectKey", "The presented RememberMeAuthenticationToken does not contain the expected key")); } return authentication; }
@Test(expected = IllegalArgumentException.class) public void testWithNullPrincipal() throws JsonProcessingException, JSONException { new RememberMeAuthenticationToken("key", null, Collections.<GrantedAuthority>emptyList()); }
/** * Constructor. * * @param key to identify if this object made by an authorised client * @param principal the principal (typically a <code>UserDetails</code>) * @param authorities the authorities granted to the principal * @throws IllegalArgumentException if a <code>null</code> was passed */ public RememberMeAuthenticationToken(String key, Object principal, Collection<? extends GrantedAuthority> authorities) { super(authorities); if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal)) { throw new IllegalArgumentException( "Cannot pass null or empty values to constructor"); } this.keyHash = key.hashCode(); this.principal = principal; setAuthenticated(true); }
@Test public void testNotEqualsDueToAbstractParentEqualsCheck() { RememberMeAuthenticationToken token1 = new RememberMeAuthenticationToken("key", "Test", ROLES_12); RememberMeAuthenticationToken token2 = new RememberMeAuthenticationToken("key", "DIFFERENT_PRINCIPAL", ROLES_12); assertThat(token1.equals(token2)).isFalse(); }
public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!supports(authentication.getClass())) { return null; } if (this.key.hashCode() != ((RememberMeAuthenticationToken) authentication) .getKeyHash()) { throw new BadCredentialsException( messages.getMessage("RememberMeAuthenticationProvider.incorrectKey", "The presented RememberMeAuthenticationToken does not contain the expected key")); } return authentication; }
@Test(expected = IllegalArgumentException.class) public void testWithNullKey() throws JsonProcessingException, JSONException { new RememberMeAuthenticationToken(null, "principal", Collections.<GrantedAuthority>emptyList()); }
/** * Constructor. * * @param key to identify if this object made by an authorised client * @param principal the principal (typically a <code>UserDetails</code>) * @param authorities the authorities granted to the principal * @throws IllegalArgumentException if a <code>null</code> was passed */ public RememberMeAuthenticationToken(String key, Object principal, Collection<? extends GrantedAuthority> authorities) { super(authorities); if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal)) { throw new IllegalArgumentException( "Cannot pass null or empty values to constructor"); } this.keyHash = key.hashCode(); this.principal = principal; setAuthenticated(true); }