public Assertion validate(final String ticket, final String service) throws TicketValidationException { if (returnTicket) { return new AssertionImpl("rod"); } throw new BadCredentialsException("As requested from mock"); } }
private CasAuthenticationToken createCasAuthenticationToken() { User principal = new User("admin", "1234", Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))); Collection<? extends GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")); Assertion assertion = new AssertionImpl(new AttributePrincipalImpl("assertName"), START_DATE, END_DATE, START_DATE, Collections.<String, Object>emptyMap()); return new CasAuthenticationToken(KEY, principal, principal.getPassword(), authorities, new User("admin", "1234", authorities), assertion); } }
protected CasAuthenticationToken getToken() { List<String> proxyList = new ArrayList<>(); proxyList.add("https://localhost/newPortal/login/cas"); User user = new User("rod", "password", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO")); final Assertion assertion = new AssertionImpl("rod"); return new CasAuthenticationToken("key", user, "ST-0-ER94xMJmn6pha35CQRoZ", AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"), user, assertion); }
@Test public void testNotEqualsDueToDifferentAuthenticationClass() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken( "Test", "Password", ROLES); assertThat(!token1.equals(token2)).isTrue(); }
@Test public void testToString() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); String result = token.toString(); assertThat( result.lastIndexOf("Credentials (Service/Proxy Ticket):") != -1).isTrue(); } }
@Test public void testSetAuthenticated() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); assertThat(token.isAuthenticated()).isTrue(); token.setAuthenticated(false); assertThat(!token.isAuthenticated()).isTrue(); }
@Test public void testNotEqualsDueToAssertion() { final Assertion assertion = new AssertionImpl("test"); final Assertion assertion2 = new AssertionImpl("test"); CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion2); assertThat(!token1.equals(token2)).isTrue(); }
@Test public void testGetters() { // Build the proxy list returned in the ticket from CAS final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); assertThat(token.getKeyHash()).isEqualTo("key".hashCode()); assertThat(token.getPrincipal()).isEqualTo(makeUserDetails()); assertThat(token.getCredentials()).isEqualTo("Password"); assertThat(token.getAuthorities()).contains( new SimpleGrantedAuthority("ROLE_ONE")); assertThat(token.getAuthorities()).contains( new SimpleGrantedAuthority("ROLE_TWO")); assertThat(token.getAssertion()).isEqualTo(assertion); assertThat(token.getUserDetails().getUsername()).isEqualTo( makeUserDetails().getUsername()); }
@Test public void testEqualsWhenEqual() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); assertThat(token2).isEqualTo(token1); }
@Test public void authenticateAllNullService() throws Exception { String serviceUrl = "https://service/context"; ServiceAuthenticationDetails details = mock(ServiceAuthenticationDetails.class); when(details.getServiceUrl()).thenReturn(serviceUrl); TicketValidator validator = mock(TicketValidator.class); when(validator.validate(any(String.class), any(String.class))).thenReturn( new AssertionImpl("rod")); ServiceProperties serviceProperties = makeServiceProperties(); serviceProperties.setAuthenticateAllArtifacts(true); CasAuthenticationProvider cap = new CasAuthenticationProvider(); cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator()); cap.setKey("qwerty"); cap.setTicketValidator(validator); cap.setServiceProperties(serviceProperties); cap.afterPropertiesSet(); String ticket = "ST-456"; UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( CasAuthenticationFilter.CAS_STATELESS_IDENTIFIER, ticket); Authentication result = cap.authenticate(token); }
@Test public void testNotEqualsDueToAbstractParentEqualsCheck() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); CasAuthenticationToken token2 = new CasAuthenticationToken("key", makeUserDetails("OTHER_NAME"), "Password", ROLES, makeUserDetails(), assertion); assertThat(!token1.equals(token2)).isTrue(); }
@Test public void testNotEqualsDueToKey() { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationToken token1 = new CasAuthenticationToken("key", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); CasAuthenticationToken token2 = new CasAuthenticationToken("DIFFERENT_KEY", makeUserDetails(), "Password", ROLES, makeUserDetails(), assertion); assertThat(!token1.equals(token2)).isTrue(); }
TicketValidator validator = mock(TicketValidator.class); when(validator.validate(any(String.class), any(String.class))).thenReturn( new AssertionImpl("rod"));
@Test(expected = BadCredentialsException.class) public void invalidKeyIsDetected() throws Exception { final Assertion assertion = new AssertionImpl("test"); CasAuthenticationProvider cap = new CasAuthenticationProvider(); cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator()); cap.setKey("qwerty"); StatelessTicketCache cache = new MockStatelessTicketCache(); cap.setStatelessTicketCache(cache); cap.setTicketValidator(new MockTicketValidator(true)); cap.setServiceProperties(makeServiceProperties()); cap.afterPropertiesSet(); CasAuthenticationToken token = new CasAuthenticationToken("WRONG_KEY", makeUserDetails(), "credentials", AuthorityUtils.createAuthorityList("XX"), makeUserDetails(), assertion); cap.authenticate(token); }
@Test public void testConstructorRejectsNulls() { final Assertion assertion = new AssertionImpl("test"); try { new CasAuthenticationToken(null, makeUserDetails(), "Password", ROLES,
protected Assertion parseResponseFromServer(final String response) throws TicketValidationException { if (!response.startsWith("yes")) { throw new TicketValidationException("CAS Server could not validate ticket."); } try { final BufferedReader reader = new BufferedReader(new StringReader(response)); reader.readLine(); final String name = reader.readLine(); return new AssertionImpl(name); } catch (final IOException e) { throw new TicketValidationException("Unable to parse response.", e); } } }
protected Assertion parseResponseFromServer(final String response) throws TicketValidationException { if (!response.startsWith("yes")) { throw new TicketValidationException("CAS Server could not validate ticket."); } try { final BufferedReader reader = new BufferedReader(new StringReader( response)); reader.readLine(); final String name = reader.readLine(); return new AssertionImpl(name); } catch (final IOException e) { throw new TicketValidationException("Unable to parse response.", e); } } }
/** * Build cas assertion. * * @param principal the principal * @param registeredService the registered service * @param attributes the attributes * @return the assertion */ protected Assertion buildCasAssertion(final String principal, final RegisteredService registeredService, final Map<String, Object> attributes) { val p = new AttributePrincipalImpl(principal, attributes); return new AssertionImpl(p, DateTimeUtils.dateOf(ZonedDateTime.now()), null, DateTimeUtils.dateOf(ZonedDateTime.now()), attributes); }
public void testAssertionValidity() throws Exception { final Assertion assertion = new AssertionImpl(CONST_PRINCIPAL, new Date(), new Date(), new Date(), CONST_ATTRIBUTES); assertTrue(assertion.isValid()); }
return new AssertionImpl( new AttributePrincipalImpl(nameId, principalAttributes), assertionValidityStart,