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

📄 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.relyingparty.openid.extensions;

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

import javax.servlet.http.HttpServletRequest;

import org.openid4java.message.AuthSuccess;
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.relyingparty.RelyingPartyException;
import org.wso2.solutions.identity.relyingparty.openid.OpenIDAuthenticationRequest;

public class OpenIDSimpleReg implements OpenIDExtension {

    private AuthSuccess authSuccess;

    /**
     * Default constructor
     */
    public OpenIDSimpleReg() {

    }

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

    /**
     * Creates an instance of MessageExtension for the OpenID authentication
     * request
     * @param request OpenID authentication request
     * @return An instance of MessageExtension
     * @throws RelyingPartyException
     */
    public MessageExtension getMessageExtension(
            OpenIDAuthenticationRequest request) throws RelyingPartyException {

        SRegRequest sregReq = null;

        sregReq = SRegRequest.createFetchRequest();

        if (request.getRequiredClaims() != null
                && request.getRequiredClaims().size() > 0) {
            for (Object requiredClaim : request.getRequiredClaims()) {
                if (requiredClaim instanceof String) {
                    sregReq.addAttribute((String) requiredClaim, true);
                }
            }
        }

        if (request.getOptionalClaims() != null
                && request.getOptionalClaims().size() > 0) {
            for (Object optionalClaim : request.getOptionalClaims()) {
                if (optionalClaim instanceof String) {
                    sregReq.addAttribute((String) optionalClaim, false);
                }
            }
        }

        return sregReq;
    }

    /**
     * Set request attributes for OpenID simple registration
     * @param request HttpServletRequest
     */
    public void setSessionAttributes(HttpServletRequest request)
            throws RelyingPartyException {

        try {

            SRegResponse sregResp = null;
            Iterator iterator = null;
            Map attributes = null;
            Entry entry = null;

            if (authSuccess.hasExtension(SRegResponse.OPENID_NS_SREG)) {
                sregResp = (SRegResponse) authSuccess
                        .getExtension(SRegResponse.OPENID_NS_SREG);
            } else if (authSuccess
                    .hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG)) {
                sregResp = (SRegResponse) authSuccess
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG);

            }else if (authSuccess
                    .hasExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1)) {
                sregResp = (SRegResponse) authSuccess
                        .getExtension(IdentityConstants.OpenId.SimpleRegAttributes.NS_SREG_1);
            }

            if (sregResp != null) {
                attributes = sregResp.getAttributes();
                iterator = attributes.entrySet().iterator();
                while (iterator.hasNext()) {
                    entry = (Entry) iterator.next();
                    request.setAttribute((String) entry.getKey(), entry
                            .getValue());
                }
            }

        } catch (MessageException e) {
            throw new RelyingPartyException(
                    IdentityConstants.ErrorCodes.OPENID_AUTHENTICATION_FAILED,
                    e);
        }
    }

    /**
     * If no attribute set by the user for simple registration request, by
     * default we set all the attributes.
     * @param request Simple registration request
     */
    protected void setDefaultRequestParams(SRegRequest request) {
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.NICK_NAME, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.FULL_NAME, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.EMAIL, true);
        request.addAttribute(IdentityConstants.OpenId.SimpleRegAttributes.DOB,
                true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.GENDER, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.POSTAL_CODE, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.COUNTRY, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.LANGUAGE, true);
        request.addAttribute(
                IdentityConstants.OpenId.SimpleRegAttributes.TIMEZONE, true);
    }

}

⌨️ 快捷键说明

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