Search in sources :

Example 6 with AccessToken

use of org.craftercms.profile.api.AccessToken in project profile by craftercms.

the class AccessTokenServiceIT method testCreateAccessToken.

@Test
public void testCreateAccessToken() throws Exception {
    AccessToken token = accessTokenService.createToken(getCrafterStudioAccessToken());
    try {
        assertNotNull(token);
        assertNotNull(token.getId());
        assertEquals(CRAFTER_STUDIO_APPLICATION, token.getApplication());
        assertEquals(true, token.isMaster());
        assertEquals(EXPIRES_ON, token.getExpiresOn());
        assertEquals(1, token.getTenantPermissions().size());
        assertEquals("*", token.getTenantPermissions().get(0).getTenant());
        assertEquals(SetUtils.asSet("*"), token.getTenantPermissions().get(0).getAllowedActions());
    } finally {
        accessTokenService.deleteToken(token.getId());
    }
}
Also used : AccessToken(org.craftercms.profile.api.AccessToken) Test(org.junit.Test)

Example 7 with AccessToken

use of org.craftercms.profile.api.AccessToken in project profile by craftercms.

the class AccessTokenServiceIT method testDeleteToken.

@Test
public void testDeleteToken() throws Exception {
    AccessToken token = accessTokenService.createToken(getCrafterStudioAccessToken());
    assertNotNull(token);
    accessTokenService.deleteToken(token.getId());
    token = accessTokenService.getToken(token.getId());
    assertNull(token);
}
Also used : AccessToken(org.craftercms.profile.api.AccessToken) Test(org.junit.Test)

Example 8 with AccessToken

use of org.craftercms.profile.api.AccessToken in project profile by craftercms.

the class AccessTokenCheckingInterceptorTest method testPreHandle.

@Test
public void testPreHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter(ProfileConstants.PARAM_ACCESS_TOKEN_ID, NORMAL_TOKEN_ID);
    interceptor.preHandle(request, null, null);
    AccessToken token = AccessTokenUtils.getAccessToken(request);
    TenantPermission permission = new TenantPermission();
    permission.allow("*");
    assertNotNull(token);
    assertEquals(APPLICATION, token.getApplication());
    assertTrue(token.isMaster());
    assertEquals(Arrays.asList(permission), token.getTenantPermissions());
    verify(tokenRepository).findByStringId(NORMAL_TOKEN_ID);
}
Also used : TenantPermission(org.craftercms.profile.api.TenantPermission) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AccessToken(org.craftercms.profile.api.AccessToken) Test(org.junit.Test)

Example 9 with AccessToken

use of org.craftercms.profile.api.AccessToken in project profile by craftercms.

the class AccessTokenServiceIT method testGetToken.

@Test
public void testGetToken() throws Exception {
    AccessToken token = accessTokenService.getToken(ADMIN_CONSOLE_TOKEN_ID);
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
    Date expiresOn = format.parse("01/01/24");
    assertNotNull(token);
    assertNotNull(token.getId());
    assertEquals(ADMIN_CONSOLE_APPLICATION, token.getApplication());
    assertEquals(true, token.isMaster());
    assertEquals(expiresOn, token.getExpiresOn());
    assertEquals(1, token.getTenantPermissions().size());
    assertEquals("*", token.getTenantPermissions().get(0).getTenant());
    assertEquals(SetUtils.asSet("*"), token.getTenantPermissions().get(0).getAllowedActions());
}
Also used : AccessToken(org.craftercms.profile.api.AccessToken) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 10 with AccessToken

use of org.craftercms.profile.api.AccessToken in project profile by craftercms.

the class AccessTokenCheckingInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (includeRequest(request)) {
        AccessToken token = getAccessToken(request);
        Date now = new Date();
        if (token.getExpiresOn() == null || now.before(token.getExpiresOn())) {
            AccessTokenUtils.setAccessToken(request, token);
        } else {
            throw new AccessDeniedException.ExpiredAccessToken(token.getId(), token.getApplication(), token.getExpiresOn());
        }
    }
    return true;
}
Also used : AccessToken(org.craftercms.profile.api.AccessToken) Date(java.util.Date)

Aggregations

AccessToken (org.craftercms.profile.api.AccessToken)13 Test (org.junit.Test)6 TenantPermission (org.craftercms.profile.api.TenantPermission)5 Date (java.util.Date)4 SimpleDateFormat (java.text.SimpleDateFormat)1 ResourceNotFoundException (org.craftercms.profile.management.exceptions.ResourceNotFoundException)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1