📄 openidinfocardextension.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.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.openid4java.OpenIDException;
import org.openid4java.infocard.InfocardException;
import org.openid4java.infocard.OpenIDToken;
import org.openid4java.message.AuthSuccess;
import org.openid4java.message.Message;
import org.openid4java.message.MessageExtension;
import org.openid4java.message.ParameterList;
import org.openid4java.message.ax.AxMessage;
import org.openid4java.message.ax.FetchResponse;
import org.wso2.solutions.identity.IdentityConstants;
import org.wso2.solutions.identity.relyingparty.RelyingPartyException;
import org.wso2.solutions.identity.relyingparty.TokenVerifierConstants;
import org.wso2.solutions.identity.relyingparty.openid.OpenIDConsumer;
public class OpenIDInfoCardExtension {
private static Map<String, String> axMapping = new HashMap<String, String>();
static {
axMapping.put(
IdentityConstants.OpenId.ExchangeAttributes.POSTAL_CODE_NS,
IdentityConstants.OpenId.ExchangeAttributes.POSTAL_CODE);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.NICK_NAME_NS,
IdentityConstants.OpenId.ExchangeAttributes.NICK_NAME);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.LANGUAGE_NS,
IdentityConstants.OpenId.ExchangeAttributes.LANGUAGE);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.GENDER_NS,
IdentityConstants.OpenId.ExchangeAttributes.GENDER);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.COUNTRY_NS,
IdentityConstants.OpenId.ExchangeAttributes.COUNTRY);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.DOB_NS,
IdentityConstants.OpenId.ExchangeAttributes.DOB);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.FULL_NAME_NS,
IdentityConstants.OpenId.ExchangeAttributes.FULL_NAME);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.TIMEZONE_NS,
IdentityConstants.OpenId.ExchangeAttributes.TIMEZONE);
axMapping.put(IdentityConstants.OpenId.ExchangeAttributes.EMAIL_NS,
IdentityConstants.OpenId.ExchangeAttributes.EMAIL);
}
/**
* Set request attributes for OpenID attribute exchange
* @param response FetchResponse
* @param request HttpServletRequest
*/
public void setSessionAttributes(HttpServletRequest request)
throws RelyingPartyException {
String xmlToken = null;
HttpSession session = null;
xmlToken = request.getParameter(IdentityConstants.XML_TOKEN);
session = request.getSession();
if (xmlToken != null) {
// Received an xmlToken from the identity selector.
ParameterList openidResponse = null;
openidResponse = getOpenIDResponse(request);
try {
parseOpenIDInfoCardToken(request, session, openidResponse);
} catch (OpenIDException e) {
// Present error to the user.
throw new RelyingPartyException(e.getMessage(), e);
}
} else {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.INVALID_XMLTOKEN);
}
request.setAttribute(TokenVerifierConstants.SERVLET_ATTR_STATE,
TokenVerifierConstants.STATE_SUCCESS);
}
/**
* Parses OpenID response.
* @param request HttpServletRequest
* @param session HttpSession
* @param openidResp OpenID response
* @throws OpenIDException
* @throws RelyingPartyException
*/
protected void parseOpenIDInfoCardToken(HttpServletRequest request,
HttpSession session, ParameterList openidResp)
throws OpenIDException, RelyingPartyException {
Message authResponse = null;
authResponse = OpenIDConsumer.getInstance().verifyOpenID(request,
openidResp);
request.setAttribute(IdentityConstants.OpenId.OPENID_IDENTIFIER,
openidResp.getParameter(IdentityConstants.OpenId.ATTR_IDENTITY)
.getValue());
if (authResponse instanceof AuthSuccess) {
AuthSuccess authSuccess = null;
MessageExtension extension = null;
FetchResponse fetchResp = null;
authSuccess = (AuthSuccess) authResponse;
if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
extension = authSuccess.getExtension(AxMessage.OPENID_NS_AX);
if (extension != null && extension instanceof FetchResponse) {
fetchResp = (FetchResponse) extension;
setSessionAttributes(fetchResp, request);
}
}
}
}
/**
* Extract parameters from the OpenID information card
* @param request HttpServletRequest
* @return Extracted a parameter list
* @throws InfocardException
*/
private ParameterList getOpenIDResponse(HttpServletRequest request)
throws RelyingPartyException {
OpenIDToken token = null;
String xmlToken = null;
xmlToken = request.getParameter(IdentityConstants.XML_TOKEN);
request.getSession().setAttribute(IdentityConstants.OpenId.ASSERTION,
xmlToken);
try {
token = OpenIDToken.createFromXmlToken(xmlToken);
} catch (InfocardException e) {
// Present error to the user.
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.OPENID_TOKEN_EXTRACTION_FAILED,
e);
}
return token.getOpenIDParams();
}
/**
* Set request attributes for OpenID attribute exchange
* @param response FetchResponse
* @param request HttpServletRequest
*/
private void setSessionAttributes(FetchResponse response,
HttpServletRequest request) {
List aliases = null;
List values = null;
String key = null;
aliases = response.getAttributeAliases();
for (Object alias : aliases) {
values = response.getAttributeValues((String) alias);
if (axMapping.containsKey((String) alias)) {
key = (String) axMapping.get(alias);
} else {
key = (String) alias;
}
request.setAttribute(key, (String) values.get(0));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -