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

📄 openidsimplereg.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.sreg.SRegRequest;
import org.openid4java.message.sreg.SRegResponse;
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.persistence.dataobject.ClaimDO;

public class OpenIDSimpleReg extends OpenIDExtension {

    private OpenIDAuthenticationRequest request;
    private static Log log = LogFactory.getLog(OpenIDSimpleReg.class);

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

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

        AuthRequest authRequest = null;
        MessageExtension extension = null;

        try {

            authRequest = request.getAuthRequest();

            if (authRequest.hasExtension(SRegRequest.OPENID_NS_SREG))
                extension = authRequest
                        .getExtension(SRegRequest.OPENID_NS_SREG);
            else if(authRequest.hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG))
                extension = authRequest
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG);
            else if(authRequest.hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1))
                extension = authRequest
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1);

            if (extension instanceof SRegRequest) {

                SRegRequest sregReq = null;
                List required = null;
                List optional = null;

                sregReq = (SRegRequest) extension;

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

                if (optional != null && !optional.isEmpty()) {
                    for (Object attr : optional) {
                        if (!required.contains(attr)) {
                            required.add(attr);
                        }
                    }
                }

                Map<String, ClaimDO> claims = null;
                ClaimDO[] supportedClaims = null;
                ClaimsAdmin claimsAdmin = 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);
                }

                mapToAttriId(required, 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 extension = null;
        AuthRequest authRequest = null;
        SRegResponse response = null;

        try {
            authRequest = request.getAuthRequest();
            
            if (authRequest.hasExtension(SRegRequest.OPENID_NS_SREG))
                extension = authRequest
                        .getExtension(SRegRequest.OPENID_NS_SREG);
            else if(authRequest.hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG))
                extension = authRequest
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG);
            else if(authRequest.hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1))
                extension = authRequest
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1);

            if (log.isDebugEnabled()) {
                if (extension == null)
                    log.info("SReg extension is null");
                else
                    log.info("SReg extension: " + extension.getTypeUri());
            }

            if (extension instanceof SRegRequest) {
                SRegRequest sregReq = null;
                List required = null;
                List optional = null;
                Map userDataSReg = null;
                Map<String, OpenIDClaim> claimValues = null;

                sregReq = (SRegRequest) extension;

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

                if (optional != null && !optional.isEmpty()) {
                    for (Object attr : optional) {
                        if (!required.contains(attr)) {
                            required.add(attr);
                        }
                    }
                }

                if (log.isDebugEnabled())
                    log.info("Required attributes for SReg request: "
                            + required.toString());

                userDataSReg = new HashMap();
                response = SRegResponse.createSRegResponse(sregReq,
                        userDataSReg);
                claimValues = populateAttributeValues(required, userId,
                        profileName);
                setSimpleAttributeRegistrationValues(response, claimValues);
            }

            return response;

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

    /**
     * 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 response Simple Registration response.
     * @param claimValues Claim values.
     * @throws MessageException
     */
    protected void setSimpleAttributeRegistrationValues(SRegResponse response,
            Map<String, OpenIDClaim> claimValues) throws MessageException {

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

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

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

    /**
     * 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(
            List<String> requiredClaims, String userId, String profileName)
            throws IdentityProviderException {

        Map<String, ClaimDO> claims = null;
        ClaimDO[] supportedClaims = null;
        ClaimsAdmin claimsAdmin = 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);
        }

        return populateAttributeValues(requiredClaims, userId, claims, null,
                profileName);
    }
}

⌨️ 快捷键说明

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