Java Code Examples for org.wso2.carbon.identity.core.util.IdentityUtil#getHMAC()

The following examples show how to use org.wso2.carbon.identity.core.util.IdentityUtil#getHMAC() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIDProviderService.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public OpenIDRememberMeDTO handleRememberMe(String openID, String ipaddress) throws Exception {

        String userName = OpenIDUtil.getUserName(openID);
        String hmac = null;
        OpenIDRememberMeDTO dto = new OpenIDRememberMeDTO();
        dto.setAuthenticated(false);
        String cookie = null;

        OpenIDRememberMeDO rememberMe = new OpenIDRememberMeDO();
        rememberMe.setOpenID(openID);
        rememberMe.setUserName(userName);

        OpenIDRememberMeTokenManager tokenManager = new OpenIDRememberMeTokenManager();

        if (ipaddress != null) {
            cookie = IdentityUtil.generateUUID();
            hmac = IdentityUtil.getHMAC(ipaddress, cookie);
            rememberMe.setToken(hmac);
            tokenManager.updateToken(rememberMe);
            dto.setNewCookieValue(cookie);
            dto.setAuthenticated(true);

            MessageContext msgContext = MessageContext.getCurrentMessageContext();

            if (msgContext != null) {
                HttpServletRequest request =
                        (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
                HttpSession httpSession = request.getSession(false);

                if (httpSession != null) {
                    httpSession.setAttribute(OpenIDServerConstants.OPENID_LOGGEDIN_USER, userName);
                }
            }
        }

        return dto;
    }
 
Example 2
Source File: OpenIDProviderService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Authenticates with the remember me token given in previous authentication.
 *
 * @param openID
 * @param password
 * @param ipaddress
 * @param cookie
 * @return
 * @throws Exception
 */
public OpenIDRememberMeDTO authenticateWithOpenIDRememberMe(String openID, String password,
                                                            String ipaddress, String cookie)
        throws Exception {
    String userName = OpenIDUtil.getUserName(openID);
    boolean isAutheticated = false;
    String hmac = null;
    OpenIDRememberMeDTO dto = new OpenIDRememberMeDTO();
    dto.setAuthenticated(false);

    if (password != null && password.trim().length() > 0) {
        isAutheticated = authenticateWithOpenID(openID, password);
        if (!isAutheticated) {
            return dto;
        }
    } else {
        if (cookie == null || "null".equals(cookie) || ipaddress == null) {
            return dto;
        }
    }

    OpenIDRememberMeDO rememberMe = new OpenIDRememberMeDO();
    rememberMe.setOpenID(openID);
    rememberMe.setUserName(userName);

    OpenIDRememberMeTokenManager tokenManager = new OpenIDRememberMeTokenManager();
    String token = null;

    if (ipaddress != null) {
        if (cookie != null && !"null".equals(cookie)) {
            hmac = IdentityUtil.getHMAC(ipaddress, cookie);
            token = tokenManager.getToken(rememberMe);
            // if the authentication failed and no valid rememberMe cookie found, then failed.
            if (!isAutheticated && (token == null || !token.equals(hmac))) {
                return dto;
            }
        } else if (!isAutheticated) {
            return dto;
        }

        cookie = IdentityUtil.generateUUID();
        hmac = IdentityUtil.getHMAC(ipaddress, cookie);
        rememberMe.setToken(hmac);
        tokenManager.updateToken(rememberMe);
        dto.setNewCookieValue(cookie);
        dto.setAuthenticated(true);

        MessageContext msgContext = MessageContext.getCurrentMessageContext();

        if (msgContext != null) {
            HttpServletRequest request =
                    (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
            HttpSession httpSession = request.getSession(false);

            if (httpSession != null) {
                httpSession.setAttribute(OpenIDServerConstants.OPENID_LOGGEDIN_USER, userName);
            }
        }

        return dto;
    }

    return dto;
}
 
Example 3
Source File: OpenIDProviderService.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
public OpenIDRememberMeDTO authenticateWithRememberMeCookie(String openID, String ipaddress, String cookie)
        throws Exception {
    String userName = OpenIDUtil.getUserName(openID);
    boolean isAutheticated = false;
    String hmac = null;
    OpenIDRememberMeDTO dto = new OpenIDRememberMeDTO();
    dto.setAuthenticated(false);

    if (cookie == null || "null".equals(cookie) || ipaddress == null) {
        return dto;
    }

    OpenIDRememberMeDO rememberMe = new OpenIDRememberMeDO();
    rememberMe.setOpenID(openID);
    rememberMe.setUserName(userName);

    OpenIDRememberMeTokenManager tokenManager = new OpenIDRememberMeTokenManager();
    String token = null;

    hmac = IdentityUtil.getHMAC(ipaddress, cookie);
    token = tokenManager.getToken(rememberMe);

    // if the authentication failed and no valid rememberMe cookie found, then failed.
    if (!isAutheticated && (token == null || !token.equals(hmac))) {
        return dto;
    }

    cookie = IdentityUtil.generateUUID();
    hmac = IdentityUtil.getHMAC(ipaddress, cookie);
    rememberMe.setToken(hmac);
    tokenManager.updateToken(rememberMe);
    dto.setNewCookieValue(cookie);
    dto.setAuthenticated(true);

    MessageContext msgContext = MessageContext.getCurrentMessageContext();

    if (msgContext != null) {
        HttpServletRequest request =
                (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        HttpSession httpSession = request.getSession(false);

        if (httpSession != null) {
            httpSession.setAttribute(OpenIDServerConstants.OPENID_LOGGEDIN_USER, userName);
        }
    }

    return dto;
}