Example usage for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken

List of usage examples for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken

Introduction

In this page you can find the example usage for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken.

Prototype

public TestingAuthenticationToken(Object principal, Object credentials, List<GrantedAuthority> authorities) 

Source Link

Usage

From source file:org.cloudfoundry.identity.uaa.login.UsernamePasswordExtractingAuthenticationManagerTests.java

@Test
public void testAuthenticate() {
    Authentication expected = new TestingAuthenticationToken("bar", "foo",
            AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
    Mockito.when(delegate.authenticate(Mockito.any(UsernamePasswordAuthenticationToken.class)))
            .thenReturn(expected);//from   ww w . ja v a2  s  . c  o m
    Authentication output = manager.authenticate(new TestingAuthenticationToken("foo", "bar"));
    assertSame(expected, output);
}

From source file:org.springmodules.jcr.jackrabbit.SpringSecurityTests.java

protected void onSetUpBeforeTransaction() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(
            new TestingAuthenticationToken(new Object(), new Object(), new GrantedAuthority[] {}));

}

From source file:mx.edu.um.mateo.general.test.BaseTest.java

public Authentication authenticate(UserDetails principal, String credentials,
        List<GrantedAuthority> authorities) {
    Authentication authentication = new TestingAuthenticationToken(principal, credentials, authorities);
    authentication.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return authentication;
}

From source file:com.epam.ta.reportportal.auth.AuthConstants.java

/**
 * Constructrs authentification using provided credentials and authorities
 * //  w  w  w  .j av  a2  s. co m
 * @param user
 * @param password
 * @param authenticated
 * @param authorities
 * @return
 */
public static Authentication newAuthentication(String user, String password, final boolean authenticated,
        GrantedAuthority... authorities) {
    return new TestingAuthenticationToken(user, password,
            ImmutableList.<org.springframework.security.core.GrantedAuthority>builder().add(authorities)
                    .build()) {
        private static final long serialVersionUID = 1L;

        {
            setAuthenticated(authenticated);
        }
    };
}

From source file:at.ac.univie.isc.asio.security.SpringSecurityAuthorizerTest.java

private TestingAuthenticationToken auth(final Permission... permissions) {
    final String[] authorities = new String[permissions.length];
    for (int i = 0; i < permissions.length; i++) {
        authorities[i] = permissions[i].toString();
    }//from  w w w.  ja v  a 2 s  . co  m
    return new TestingAuthenticationToken("test-user", "test-password", authorities);
}

From source file:at.ac.univie.isc.asio.security.HttpMethodRestrictionFilterTest.java

@Test
public void should_remove__PERMISSION_UPDATE__from__GET__request() throws Exception {
    final TestingAuthenticationToken token = new TestingAuthenticationToken("user", "secret",
            Arrays.<GrantedAuthority>asList(Permission.INVOKE_QUERY, Permission.INVOKE_UPDATE));
    setAuthentication(token);//from   w  w w . j a v  a 2 s. c  om
    request.setMethod(HttpMethod.GET.name());
    subject.doFilter(request, response, chain);
    assertThat(getAuthentication().getAuthorities(),
            Matchers.<GrantedAuthority>contains(Permission.INVOKE_QUERY));
}

From source file:at.ac.univie.isc.asio.security.HttpMethodRestrictionFilterTest.java

@Test
public void should_keep_other_token_properties() throws Exception {
    final TestingAuthenticationToken token = new TestingAuthenticationToken("user", "secret",
            Collections.<GrantedAuthority>singletonList(Permission.INVOKE_UPDATE));
    token.setDetails("details");
    setAuthentication(token);/*w  w  w . jav a 2s  .  co m*/
    request.setMethod(HttpMethod.GET.name());
    subject.doFilter(request, response, chain);
    final Authentication filtered = getAuthentication();
    assertThat(filtered.getPrincipal(), equalTo(token.getPrincipal()));
    assertThat(filtered.getCredentials(), equalTo(token.getCredentials()));
    assertThat(filtered.getDetails(), equalTo(token.getDetails()));
}

From source file:org.apache.wicket.security.examples.springsecurity.security.MockLoginPage.java

/**
 * @param username//w w w. j  a  va2  s.c  om
 * @return true if the login was successful, false otherwise
 */
public boolean login(String username) {
    try {
        LoginContext context;

        context = new SpringSecureLoginContext(
                new TestingAuthenticationToken(username, username, getAuthorities(username, username)));
        ((WaspSession) Session.get()).login(context);
        if (!continueToOriginalDestination())
            setResponsePage(Application.get().getHomePage());
        return true;
    } catch (LoginException e) {
        log.error(e.getMessage(), e);
    }
    return false;
}

From source file:org.jasig.springframework.security.portlet.context.PortletSecurityContextPersistenceFilterTests.java

@Test
public void loadedContextContextIsCopiedToSecurityContextHolderAndUpdatedContextIsStored() throws Exception {
    final MockRenderRequest request = new MockRenderRequest();
    final MockRenderResponse response = new MockRenderResponse();
    final PortletSecurityContextRepository repo = mock(PortletSecurityContextRepository.class);
    PortletSecurityContextPersistenceFilter filter = new PortletSecurityContextPersistenceFilter(repo);
    final TestingAuthenticationToken beforeAuth = new TestingAuthenticationToken("someoneelse", "passwd",
            "ROLE_B");
    final SecurityContext scBefore = new SecurityContextImpl();
    final SecurityContext scExpectedAfter = new SecurityContextImpl();
    scExpectedAfter.setAuthentication(testToken);
    scBefore.setAuthentication(beforeAuth);
    when(repo.loadContext(any(PortletRequestResponseHolder.class))).thenReturn(scBefore);

    final FilterChain chain = mock(FilterChain.class);
    doAnswer(new Answer<Object>() {
        @Override/*from w w w .jav a  2s . c o m*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertEquals(beforeAuth, SecurityContextHolder.getContext().getAuthentication());
            // Change the context here
            SecurityContextHolder.setContext(scExpectedAfter);
            return null;
        }
    }).when(chain).doFilter(any(RenderRequest.class), any(RenderResponse.class));

    filter.doFilter(request, response, chain);

    verify(repo).saveContext(eq(scExpectedAfter), any(PortletRequestResponseHolder.class));
}

From source file:org.dhara.CustomUserServiceTest.java

@Test
public void testGetAuthenticatedUser() throws Exception {
    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    UserDetails userDetails = new UserImpl("1", "canonical");
    Authentication authentication = new TestingAuthenticationToken(userDetails, "canonical",
            grantedAuthorities);//from www. j  a  v  a  2 s.  c  om
    SecurityContextHolder.getContext().setAuthentication(authentication);

    User returnedUser = userService.getAuthenticatedUser();
    assertEquals(returnedUser, userDetails);
}