⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openidattributeexchange.java

📁 开源的OpenId的一个java实现
💻 JAVA
字号:
/*
 * Copyright 2005-2008 WSO2, Inc. (http://wso2.com) Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package org.wso2.solutions.identity.openid.extensions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openid4java.message.AuthRequest;
import org.openid4java.message.MessageException;
import org.openid4java.message.MessageExtension;
import org.openid4java.message.ax.FetchRequest;
import org.openid4java.message.ax.FetchResponse;
import org.wso2.solutions.identity.IdentityConstants;
import org.wso2.solutions.identity.IdentityProviderException;
import org.wso2.solutions.identity.admin.ClaimsAdmin;
import org.wso2.solutions.identity.openid.OpenIDAuthenticationRequest;
import org.wso2.solutions.identity.openid.OpenIDClaim;
import org.wso2.solutions.identity.openid.OpenIDClaimMapper;
import org.wso2.solutions.identity.persistence.dataobject.ClaimDO;

public class OpenIDAttributeExchange extends OpenIDExtension {

    private OpenIDAuthenticationRequest request;

    private static Log log = LogFactory.getLog(OpenIDAttributeExchange.class);

    private static Map<String, String> axMapping;

    /**
     * Constructed during building the response
     * @param request An instance of OpenIDAuthenticationRequest
     */
    public OpenIDAttributeExchange(OpenIDAuthenticationRequest request) {
        this.request = request;
    }

    public void addRequiredAttributes(List<String> requiredAttributes)
            throws IdentityProviderException {

        MessageExtension extensions = null;
        AuthRequest authRequest = null;

        try {
            authRequest = request.getAuthRequest();

            if (authRequest.hasExtension(FetchRequest.OPENID_NS_AX))
                extensions = authRequest
                        .getExtension(FetchRequest.OPENID_NS_AX);
            else if (authRequest
                    .hasExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX))
                extensions = authRequest
                        .getExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX);

            if (extensions instanceof FetchRequest) {
                Map required = null;
                Map optional = null;
                FetchRequest fetchRequest = null;

                fetchRequest = (FetchRequest) extensions;

                // Get the required attributes as requested by the
                // RP.
                required = fetchRequest.getAttributes(true);
                optional = fetchRequest.getAttributes();

                if (optional != null && !optional.isEmpty()) {
                    Iterator iterator = optional.entrySet().iterator();
                    Entry entry = null;
                    while (iterator.hasNext()) {
                        entry = (Entry) iterator.next();
                        if (!required.containsKey(entry.getKey())) {
                            required.put(entry.getKey(), entry.getValue());
                        }
                    }
                }

                Map<String, ClaimDO> claims = null;
                ClaimDO[] supportedClaims = null;
                ClaimsAdmin claimsAdmin = null;
                Iterator<Entry<String, String>> iterator = null;
                Map<String, String> map = null;
                Entry<String, String> entry = null;

                claims = new HashMap<String, ClaimDO>();

                claimsAdmin = new ClaimsAdmin();
                supportedClaims = claimsAdmin.getAllMappedEnabledClaims();

                for (int i = 0; i < supportedClaims.length; i++) {
                    ClaimDO temp = supportedClaims[i];
                    if (temp.getOpenIDTag() != null) {
                        claims.put(temp.getOpenIDTag(), temp);
                    }
                }

                iterator = required.entrySet().iterator();
                map = new HashMap<String, String>();

                String val = null;
                String tag = null;

                while (iterator.hasNext()) {
                    entry = iterator.next();
                    val = getMappedAxSchema((String) entry.getValue());
                    tag = claimsAdmin.getMappedOpenIDTag(val);
                    if (tag != null) {
                        claims.get(tag).setUri((String) entry.getValue());
                        map.put(tag, (String) entry.getKey());
                    }
                }

                mapToAttriId(map.keySet(), requiredAttributes, claims);
            }

        } catch (MessageException ex) {
            throw new IdentityProviderException(
                    IdentityConstants.ErrorCodes.OPENID_RESP_GENERATION_FAILED,
                    ex);
        }
    }

    /**
     * Creates an instance of MessageExtension for the OpenID authentication
     * response
     * @param request OpenID authentication request
     * @return An instance of MessageExtension
     * @throws RelyingPartyException
     */
    public MessageExtension getMessageExtension(String userId,
            String profileName) throws IdentityProviderException {

        MessageExtension extensions = null;
        AuthRequest authRequest = null;
        FetchResponse fetchResponse = null;

        try {
            authRequest = request.getAuthRequest();
            if (authRequest.hasExtension(FetchRequest.OPENID_NS_AX))
                extensions = authRequest
                        .getExtension(FetchRequest.OPENID_NS_AX);
            else if (authRequest
                    .hasExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX))
                extensions = authRequest
                        .getExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX);

            if (extensions instanceof FetchRequest) {

                Map required = null;
                Map optional = null;
                FetchRequest fetchRequest = null;
                Map<String, OpenIDClaim> claimValues = null;

                fetchRequest = (FetchRequest) extensions;

                // Get the required attributes as requested by the
                // RP.
                required = fetchRequest.getAttributes(true);
                optional = fetchRequest.getAttributes();

                if (optional != null && !optional.isEmpty()) {
                    Iterator iterator = optional.entrySet().iterator();
                    Entry entry = null;
                    while (iterator.hasNext()) {
                        entry = (Entry) iterator.next();
                        if (!required.containsKey(entry.getKey())) {
                            required.put(entry.getKey(), entry.getValue());
                        }
                    }
                }

                fetchResponse = FetchResponse.createFetchResponse(fetchRequest,
                        new HashMap());
                claimValues = populateAttributeValues(required, userId,
                        profileName);
                setAttributeExchangeValues(fetchResponse, claimValues);
            }

            return fetchResponse;

        } catch (MessageException e) {
            throw new IdentityProviderException(
                    IdentityConstants.ErrorCodes.OPENID_RESP_GENERATION_FAILED,
                    e);
        }
    }

    /**
     * Populate the required claims with claim values.
     * @param requiredClaims Required claims as requested by the RP.
     * @param userId User ID.
     * @return A map, populated with required claim values.
     * @throws IdentityProviderException
     */
    protected Map<String, OpenIDClaim> populateAttributeValues(
            Map<String, String> requiredClaims, String userId,
            String profileName) throws IdentityProviderException {

        Map<String, ClaimDO> claims = null;
        ClaimDO[] supportedClaims = null;
        ClaimsAdmin claimsAdmin = null;
        Iterator<Entry<String, String>> iterator = null;
        Map<String, String> map = null;
        Entry<String, String> entry = null;

        claims = new HashMap<String, ClaimDO>();

        claimsAdmin = new ClaimsAdmin();
        supportedClaims = claimsAdmin.getAllMappedEnabledClaims();

        for (int i = 0; i < supportedClaims.length; i++) {
            ClaimDO temp = supportedClaims[i];
            if (temp.getOpenIDTag() != null) {
                claims.put(temp.getOpenIDTag(), temp);
            }
        }

        iterator = requiredClaims.entrySet().iterator();
        map = new HashMap<String, String>();

        String val = null;
        String tag = null;

        while (iterator.hasNext()) {
            entry = iterator.next();
            val = getMappedAxSchema((String) entry.getValue());
            tag = claimsAdmin.getMappedOpenIDTag(val);
            if (tag != null) {
                claims.get(tag).setUri((String) entry.getValue());
                map.put(tag, (String) entry.getKey());
            }
        }

        return populateAttributeValues(map.keySet(), userId, claims, map,
                profileName);
    }

    /**
     * This provides a mapping between http://schema.openid.net/ and
     * http://axschema.org
     * @param val schema name-space URL
     * @return mapped value
     * @throws IdentityProviderException
     */
    protected String getMappedAxSchema(String val)
            throws IdentityProviderException {

        if (axMapping == null) {
            try {
                axMapping = OpenIDClaimMapper.getInstance().getAxMapping();
            } catch (Exception e) {
                throw new IdentityProviderException(
                        IdentityConstants.ErrorCodes.OPENID_RESP_GENERATION_FAILED,
                        e);
            }
        }

        if (axMapping.containsKey(val)) {
            return axMapping.get(val);
        }

        return val;
    }

    /**
     * Populate the response with claim values. If we can't find the required
     * values with us, we simply avoid sending them. An Identity Provider MAY
     * return any subset of the following fields in response to the query.
     * @param claimValues Claim values.
     * @throws MessageException
     */
    protected void setAttributeExchangeValues(FetchResponse response,
            Map<String, OpenIDClaim> claimValues) throws MessageException {

        Iterator<Entry<String, OpenIDClaim>> iterator = null;
        Entry<String, OpenIDClaim> entry = null;
        OpenIDClaim claim = null;

        iterator = claimValues.entrySet().iterator();

        while (iterator.hasNext()) {
            entry = iterator.next();
            claim = (OpenIDClaim) entry.getValue();
            response.addAttribute(claim.getOpenIDTag(), claim.getTypeUri(),
                    claim.getClaimValue());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -