Search in sources :

Example 6 with IdentityCookieConfig

use of org.wso2.carbon.identity.core.model.IdentityCookieConfig in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testSetCookieExistCookieConfig.

@Test
public void testSetCookieExistCookieConfig() {
    mockCookieTest();
    IdentityCookieConfig cookieConfig = new IdentityCookieConfig(FrameworkConstants.COMMONAUTH_COOKIE);
    IdentityUtil.getIdentityCookiesConfigurationHolder().put(FrameworkConstants.COMMONAUTH_COOKIE, cookieConfig);
    int age = 3600;
    FrameworkUtils.setCookie(request, response, FrameworkConstants.COMMONAUTH_COOKIE, "commonAuthIdValue", age);
    verify(response, times(1)).addCookie(cookieCaptor.capture());
    List<Cookie> capturedCookies = cookieCaptor.getAllValues();
    Cookie storedCookie = capturedCookies.get(0);
    assertEquals(storedCookie.getName(), FrameworkConstants.COMMONAUTH_COOKIE);
    assertEquals(storedCookie.getPath(), ROOT_DOMAIN);
    assertEquals(storedCookie.getMaxAge(), age);
}
Also used : Cookie(javax.servlet.http.Cookie) SameSiteCookie(org.wso2.carbon.core.SameSiteCookie) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 7 with IdentityCookieConfig

use of org.wso2.carbon.identity.core.model.IdentityCookieConfig in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testRemoveCookieExistCookieConfig.

@Test
public void testRemoveCookieExistCookieConfig() {
    mockCookieTest();
    IdentityCookieConfig cookieConfig = new IdentityCookieConfig(FrameworkConstants.COMMONAUTH_COOKIE);
    IdentityUtil.getIdentityCookiesConfigurationHolder().put(FrameworkConstants.COMMONAUTH_COOKIE, cookieConfig);
    FrameworkUtils.removeCookie(request, response, FrameworkConstants.COMMONAUTH_COOKIE, SameSiteCookie.STRICT, ROOT_DOMAIN);
    verify(response, times(1)).addCookie(cookieCaptor.capture());
    List<Cookie> capturedCookies = cookieCaptor.getAllValues();
    Cookie removedCookie = capturedCookies.get(0);
    assertEquals(removedCookie.getName(), FrameworkConstants.COMMONAUTH_COOKIE);
    assertEquals(removedCookie.getPath(), ROOT_DOMAIN);
    assertEquals(removedCookie.getMaxAge(), 0);
}
Also used : Cookie(javax.servlet.http.Cookie) SameSiteCookie(org.wso2.carbon.core.SameSiteCookie) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 8 with IdentityCookieConfig

use of org.wso2.carbon.identity.core.model.IdentityCookieConfig in project carbon-identity-framework by wso2.

the class FrameworkUtils method removeCookie.

/**
 * Removes a cookie which is already stored.
 *
 * @param req        Incoming HttpServletRequest.
 * @param resp       HttpServletResponse which should be stored.
 * @param cookieName Name of the cookie which should be removed.
 */
public static void removeCookie(HttpServletRequest req, HttpServletResponse resp, String cookieName) {
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                CookieBuilder cookieBuilder = new CookieBuilder(cookieName, cookie.getValue());
                IdentityCookieConfig cookieConfig = IdentityUtil.getIdentityCookieConfig(cookieName);
                if (cookieConfig != null) {
                    updateCookieConfig(cookieBuilder, cookieConfig, 0, ROOT_DOMAIN);
                } else {
                    cookieBuilder.setHttpOnly(true);
                    cookieBuilder.setSecure(true);
                    cookieBuilder.setPath(ROOT_DOMAIN);
                }
                cookieBuilder.setMaxAge(0);
                resp.addCookie(cookieBuilder.build());
                break;
            }
        }
    }
}
Also used : SameSiteCookie(org.wso2.carbon.core.SameSiteCookie) Cookie(javax.servlet.http.Cookie) CookieBuilder(org.wso2.carbon.identity.core.model.CookieBuilder) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig)

Example 9 with IdentityCookieConfig

use of org.wso2.carbon.identity.core.model.IdentityCookieConfig in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testSetCookieWithSameSiteExistCookieConfig.

@Test
public void testSetCookieWithSameSiteExistCookieConfig() {
    mockCookieTest();
    IdentityCookieConfig cookieConfig = new IdentityCookieConfig(FrameworkConstants.COMMONAUTH_COOKIE);
    IdentityUtil.getIdentityCookiesConfigurationHolder().put(FrameworkConstants.COMMONAUTH_COOKIE, cookieConfig);
    int age = 3600;
    FrameworkUtils.setCookie(request, response, FrameworkConstants.COMMONAUTH_COOKIE, "commonAuthIdValue", age, SameSiteCookie.STRICT, "Dummy-Path");
    verify(response, times(1)).addCookie(cookieCaptor.capture());
    List<Cookie> capturedCookies = cookieCaptor.getAllValues();
    Cookie storedCookie = capturedCookies.get(0);
    assertEquals(storedCookie.getName(), FrameworkConstants.COMMONAUTH_COOKIE);
    assertEquals(storedCookie.getPath(), "Dummy-Path");
    assertEquals(storedCookie.getMaxAge(), age);
}
Also used : Cookie(javax.servlet.http.Cookie) SameSiteCookie(org.wso2.carbon.core.SameSiteCookie) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 10 with IdentityCookieConfig

use of org.wso2.carbon.identity.core.model.IdentityCookieConfig in project carbon-identity-framework by wso2.

the class IdentityUtilTest method testGetIdentityCookiesConfigurationHolder.

@Test
public void testGetIdentityCookiesConfigurationHolder() throws Exception {
    Map<String, IdentityCookieConfig> mockIdentityCookiesConfigurationHolder = new HashMap<>();
    mockIdentityCookiesConfigurationHolder.put("cookie", new IdentityCookieConfig("cookieName"));
    Whitebox.setInternalState(IdentityUtil.class, "identityCookiesConfigurationHolder", mockIdentityCookiesConfigurationHolder);
    assertEquals(IdentityUtil.getIdentityCookiesConfigurationHolder(), mockIdentityCookiesConfigurationHolder, "Returned cookie holder doesn't match the given.");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

IdentityCookieConfig (org.wso2.carbon.identity.core.model.IdentityCookieConfig)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 SameSiteCookie (org.wso2.carbon.core.SameSiteCookie)6 Cookie (javax.servlet.http.Cookie)5 CookieBuilder (org.wso2.carbon.identity.core.model.CookieBuilder)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 BeforeTest (org.testng.annotations.BeforeTest)3 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)3 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 IdentityCacheConfig (org.wso2.carbon.identity.core.model.IdentityCacheConfig)1 IdentityCacheConfigKey (org.wso2.carbon.identity.core.model.IdentityCacheConfigKey)1 IdentityEventListenerConfig (org.wso2.carbon.identity.core.model.IdentityEventListenerConfig)1 IdentityEventListenerConfigKey (org.wso2.carbon.identity.core.model.IdentityEventListenerConfigKey)1