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.syncope.core.notification.NotificationTest.java

@Before
public void setupSecurity() {
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (Entitlement entitlement : entitlementDAO.findAll()) {
        authorities.add(new SimpleGrantedAuthority(entitlement.getName()));
    }/*w ww .ja  va 2s.  c  om*/

    UserDetails userDetails = new User(adminUser, "FAKE_PASSWORD", true, true, true, true, authorities);
    Authentication authentication = new TestingAuthenticationToken(userDetails, "FAKE_PASSWORD", authorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

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

@Test
public void saveContextCallsSetAttributeIfContextIsModifiedDirectlyDuringRequest() throws Exception {
    PortletSessionSecurityContextRepository repo = new PortletSessionSecurityContextRepository();
    MockPortletRequest request = new MockPortletRequest();
    // Set up an existing authenticated context, mocking that it is in the session already
    SecurityContext ctx = SecurityContextHolder.getContext();
    ctx.setAuthentication(testToken);/*www  .  ja  v  a 2 s  . c  o  m*/
    PortletSession session = mock(PortletSession.class);
    when(session.getAttribute(SPRING_SECURITY_CONTEXT_KEY, PortletSession.APPLICATION_SCOPE)).thenReturn(ctx);
    request.setSession(session);
    PortletRequestResponseHolder holder = new PortletRequestResponseHolder(request, new MockPortletResponse());
    assertSame(ctx, repo.loadContext(holder));

    // Modify context contents. Same user, different role
    SecurityContextHolder.getContext()
            .setAuthentication(new TestingAuthenticationToken("someone", "passwd", "ROLE_B"));
    repo.saveContext(ctx, holder);

    // Must be called even though the value in the local VM is already the same
    verify(session).setAttribute(SPRING_SECURITY_CONTEXT_KEY, ctx, PortletSession.APPLICATION_SCOPE);
}

From source file:net.projectmonkey.spring.acl.hbase.repository.HBaseACLRepositoryTest.java

private void setUpAuthorisedUser() {
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(SOME_PRINCIPAL, "credentials",
            SOME_AUTHORITY);/*  w ww  . j a v  a2s. c o m*/
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:org.apache.syncope.core.logic.NotificationTest.java

@Before
public void setupSecurity() {
    List<GrantedAuthority> authorities = CollectionUtils.collect(Entitlement.values(),
            new Transformer<String, GrantedAuthority>() {

                @Override//from   w  ww.jav  a 2s .c  om
                public GrantedAuthority transform(final String entitlement) {
                    return new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM);
                }
            }, new ArrayList<GrantedAuthority>());

    UserDetails userDetails = new User(adminUser, "FAKE_PASSWORD", authorities);
    Authentication authentication = new TestingAuthenticationToken(userDetails, "FAKE_PASSWORD", authorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:org.encuestame.test.business.config.AbstractSpringSecurityContext.java

/**
 * set Authentication.//  w ww.  j a v  a 2 s . c om
 * @param username
 * @param password
 */
public void setAuthentication(final String username, final String password) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //Add permissions.
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_USER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_ADMIN.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_WRITE.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_OWNER.name()));
    TestingAuthenticationToken token = new TestingAuthenticationToken(username, password, authorities);
    token.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(token);
    //System.out.println("creando SecurityContextHolder "+SecurityContextHolder.getContext().getAuthentication());
    setUsernameLogged(this.springSecurityLoggedUserAccount.getUsername());
}

From source file:org.encuestame.test.business.config.AbstractSpringSecurityContext.java

/**
 *
 * @param userAccount/*from w  ww  .  j  av  a2s.  co  m*/
 */
public void login(UserAccount userAccount) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //Add permissions.
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_USER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_ADMIN.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_EDITOR.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_PUBLISHER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_WRITE.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_OWNER.name()));
    TestingAuthenticationToken token = new TestingAuthenticationToken(userAccount.getUsername(), "12345",
            authorities);
    token.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(token);
    //setUsernameLogged(this.springSecurityLoggedUserAccount.getUsername());
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginSuccessIgnoredIfParameterNotSetOrFalse() {
    TokenBasedRememberMeServices services = new TokenBasedRememberMeServices("key",
            new AbstractRememberMeServicesTests.MockUserDetailsService(null, false));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(DEFAULT_PARAMETER, "false");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNull();//from w w  w  .  j  ava2s  .  c o  m
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginSuccessNormalWithNonUserDetailsBasedPrincipalSetsExpectedCookie() {
    // SEC-822//from  ww  w. jav a  2  s.c o m
    services.setTokenValiditySeconds(500000000);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(TokenBasedRememberMeServices.DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    String expiryTime = services.decodeCookie(cookie.getValue())[1];
    long expectedExpiryTime = 1000L * 500000000;
    expectedExpiryTime += System.currentTimeMillis();
    assertThat(Long.parseLong(expiryTime) > expectedExpiryTime - 10000).isTrue();
    assertThat(cookie).isNotNull();
    assertThat(cookie.getMaxAge()).isEqualTo(services.getTokenValiditySeconds());
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
    assertThat(new Date().before(new Date(determineExpiryTimeFromBased64EncodedToken(cookie.getValue()))))
            .isTrue();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void loginSuccessNormalWithUserDetailsBasedPrincipalSetsExpectedCookie() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(TokenBasedRememberMeServices.DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNotNull();// w  ww . ja  va 2s  . c  o  m
    assertThat(cookie.getMaxAge()).isEqualTo(services.getTokenValiditySeconds());
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
    assertThat(new Date().before(new Date(determineExpiryTimeFromBased64EncodedToken(cookie.getValue()))))
            .isTrue();
}

From source file:org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServicesTests.java

@Test
public void negativeValidityPeriodIsSetOnCookieButExpiryTimeRemainsAtTwoWeeks() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(DEFAULT_PARAMETER, "true");

    MockHttpServletResponse response = new MockHttpServletResponse();
    services.setTokenValiditySeconds(-1);
    services.loginSuccess(request, response, new TestingAuthenticationToken("someone", "password", "ROLE_ABC"));

    Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
    assertThat(cookie).isNotNull();/*  w  ww  .j  a  v  a  2s  . c  o  m*/
    // Check the expiry time is within 50ms of two weeks from current time
    assertThat(determineExpiryTimeFromBased64EncodedToken(cookie.getValue())
            - System.currentTimeMillis() > TWO_WEEKS_S - 50).isTrue();
    assertThat(cookie.getMaxAge()).isEqualTo(-1);
    assertThat(Base64.isArrayByteBase64(cookie.getValue().getBytes())).isTrue();
}