Search in sources :

Example 36 with AuthenticationInfo

use of org.apache.sling.auth.core.spi.AuthenticationInfo in project sling by apache.

the class XingLoginAuthenticationHandler method extractCredentials.

/**
     * we need the <i>hash</i> from the XING cookie (<code>xing_p_lw_s_[...]</code>) and
     * the <i>user data</i> and <i>id</i> from our own cookies (<code>sling_auth_xing_[...]</code>)
     *
     * @param request
     * @param response
     * @return
     */
@Override
public AuthenticationInfo extractCredentials(final HttpServletRequest request, final HttpServletResponse response) {
    logger.debug("extract credentials");
    String hash = null;
    String user = null;
    String userId = null;
    final Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (final Cookie cookie : cookies) {
            final String cookieName = cookie.getName();
            if (cookieName.equals(xingCookie)) {
                hash = readCookieValue(cookie);
                logger.debug("“Login with XING” cookie found: {}", hash);
            } else if (cookieName.equals(userCookie)) {
                user = readCookieValue(cookie);
            } else if (cookieName.equals(userIdCookie)) {
                userId = readCookieValue(cookie);
            }
        }
    }
    if (!StringUtils.isEmpty(hash) && !StringUtils.isEmpty(userId) && !StringUtils.isEmpty(user)) {
        logger.debug("valid cookies with hash and user data and id found");
        final AuthenticationInfo authenticationInfo = new AuthenticationInfo(XingLogin.AUTH_TYPE, userId);
        authenticationInfo.put(XingLogin.AUTHENTICATION_CREDENTIALS_HASH_KEY, hash);
        authenticationInfo.put(XingLogin.AUTHENTICATION_CREDENTIALS_USERDATA_KEY, user);
        return authenticationInfo;
    } else {
        logger.debug("unable to extract credentials from request");
        return null;
    }
}
Also used : Cookie(javax.servlet.http.Cookie) AuthenticationInfo(org.apache.sling.auth.core.spi.AuthenticationInfo)

Aggregations

AuthenticationInfo (org.apache.sling.auth.core.spi.AuthenticationInfo)36 Test (org.junit.Test)25 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 SimpleCredentials (javax.jcr.SimpleCredentials)4 IOException (java.io.IOException)3 Collection (java.util.Collection)2 Credentials (javax.jcr.Credentials)2 LoginException (org.apache.sling.api.resource.LoginException)2 Method (java.lang.reflect.Method)1 AccountLockedException (javax.security.auth.login.AccountLockedException)1 AccountNotFoundException (javax.security.auth.login.AccountNotFoundException)1 CredentialExpiredException (javax.security.auth.login.CredentialExpiredException)1 Cookie (javax.servlet.http.Cookie)1 HttpSession (javax.servlet.http.HttpSession)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 FormCredentials (org.apache.sling.auth.form.impl.jaas.FormCredentials)1 XingUser (org.apache.sling.auth.xing.api.XingUser)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Token (org.scribe.model.Token)1