Search in sources :

Example 1 with CookieBuilder

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

the class FrameworkUtils method setCookie.

public static void setCookie(HttpServletRequest req, HttpServletResponse resp, String cookieName, String id, Integer age, SameSiteCookie setSameSite, String path) {
    CookieBuilder cookieBuilder = new CookieBuilder(cookieName, id);
    IdentityCookieConfig cookieConfig = IdentityUtil.getIdentityCookieConfig(cookieName);
    if (cookieConfig != null) {
        updateCookieConfig(cookieBuilder, cookieConfig, age, path);
    } else {
        cookieBuilder.setSecure(true);
        cookieBuilder.setHttpOnly(true);
        cookieBuilder.setPath(StringUtils.isNotBlank(path) ? path : ROOT_DOMAIN);
        cookieBuilder.setSameSite(setSameSite);
        if (age != null) {
            cookieBuilder.setMaxAge(age);
        }
    }
    resp.addCookie(cookieBuilder.build());
}
Also used : CookieBuilder(org.wso2.carbon.identity.core.model.CookieBuilder) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig)

Example 2 with CookieBuilder

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

the class FrameworkUtils method setCookie.

/**
 * Stores a cookie to the response taking configurations from identity.xml file.
 *
 * @param req        Incoming HttpSerletRequest.
 * @param resp       Outgoing HttpServletResponse.
 * @param cookieName Name of the cookie to be stored.
 * @param id         Cookie id.
 * @param age        Max age of the cookie.
 */
public static void setCookie(HttpServletRequest req, HttpServletResponse resp, String cookieName, String id, Integer age) {
    CookieBuilder cookieBuilder = new CookieBuilder(cookieName, id);
    IdentityCookieConfig cookieConfig = IdentityUtil.getIdentityCookieConfig(cookieName);
    if (cookieConfig != null) {
        updateCookieConfig(cookieBuilder, cookieConfig, age, null);
    } else {
        cookieBuilder.setSecure(true);
        cookieBuilder.setHttpOnly(true);
        cookieBuilder.setPath(ROOT_DOMAIN);
        if (age != null) {
            cookieBuilder.setMaxAge(age);
        }
    }
    resp.addCookie(cookieBuilder.build());
}
Also used : CookieBuilder(org.wso2.carbon.identity.core.model.CookieBuilder) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig)

Example 3 with CookieBuilder

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

the class FrameworkUtils method removeCookie.

public static void removeCookie(HttpServletRequest req, HttpServletResponse resp, String cookieName, SameSiteCookie sameSiteCookie, String path) {
    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, path);
                } else {
                    cookieBuilder.setHttpOnly(true);
                    cookieBuilder.setSecure(true);
                    cookieBuilder.setPath(StringUtils.isNotBlank(path) ? path : ROOT_DOMAIN);
                    cookieBuilder.setSameSite(sameSiteCookie);
                }
                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 4 with CookieBuilder

use of org.wso2.carbon.identity.core.model.CookieBuilder 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)

Aggregations

CookieBuilder (org.wso2.carbon.identity.core.model.CookieBuilder)4 IdentityCookieConfig (org.wso2.carbon.identity.core.model.IdentityCookieConfig)4 Cookie (javax.servlet.http.Cookie)2 SameSiteCookie (org.wso2.carbon.core.SameSiteCookie)2