📄 openidattributeexchange.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.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.openid4java.message.AuthSuccess;
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.openid.OpenIDClaimMapper;
import org.wso2.solutions.identity.relyingparty.RelyingPartyException;
import org.wso2.solutions.identity.relyingparty.openid.OpenIDAuthenticationRequest;
import org.wso2.solutions.identity.relyingparty.openid.OpenIDAxAttribute;
public class OpenIDAttributeExchange implements OpenIDExtension {
private AuthSuccess authSuccess;
private static Map<String, String> axMapping;
/**
* Default constructor
*/
public OpenIDAttributeExchange() {
}
/**
* Constructed during building the response
* @param authSuccess An instance of AuthSuccess
*/
public OpenIDAttributeExchange(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 {
FetchRequest fetchReq = null;
OpenIDAxAttribute attr = null;
try {
fetchReq = FetchRequest.createFetchRequest();
if (request.getRequiredClaims() != null
&& request.getRequiredClaims().size() > 0) {
for (Object requiredClaim : request.getRequiredClaims()) {
if (requiredClaim instanceof OpenIDAxAttribute) {
attr = (OpenIDAxAttribute) requiredClaim;
fetchReq.addAttribute(attr.getAttributeName(), attr
.getNamespace(), true);
}
}
}
if (request.getOptionalClaims() != null
&& request.getOptionalClaims().size() > 0) {
for (Object optionalClaim : request.getOptionalClaims()) {
if (optionalClaim instanceof OpenIDAxAttribute) {
attr = (OpenIDAxAttribute) optionalClaim;
fetchReq.addAttribute(attr.getAttributeName(), attr
.getNamespace(), false);
}
}
}
} catch (MessageException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.OPENID_AUTHENTICATION_FAILED,
e);
}
return fetchReq;
}
/**
* Set request attributes for OpenID attribute exchange
* @param request HttpServletRequest
*/
public void setSessionAttributes(HttpServletRequest request)
throws RelyingPartyException {
List aliases = null;
List values = null;
FetchResponse fetchReponse = null;
Map attributeTypes = null;
try {
if (authSuccess.hasExtension(FetchResponse.OPENID_NS_AX)) {
fetchReponse = (FetchResponse) authSuccess
.getExtension(FetchResponse.OPENID_NS_AX);
aliases = fetchReponse.getAttributeAliases();
attributeTypes = fetchReponse.getAttributeTypes();
for (Object alias : aliases) {
values = fetchReponse.getAttributeValues((String) alias);
if (values != null && !values.isEmpty()) {
request.setAttribute(getAlias((String) attributeTypes
.get(alias)), (String) values.get(0));
}
}
} else if (authSuccess
.hasExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX)) {
fetchReponse = (FetchResponse) authSuccess
.getExtension(IdentityConstants.OpenId.ExchangeAttributes.NS_AX);
aliases = fetchReponse.getAttributeAliases();
attributeTypes = fetchReponse.getAttributeTypes();
for (Object alias : aliases) {
values = fetchReponse.getAttributeValues((String) alias);
if (values != null && !values.isEmpty()) {
request.setAttribute(getAlias((String) attributeTypes
.get(alias)), (String) values.get(0));
}
}
}
} catch (MessageException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.OPENID_AUTHENTICATION_FAILED,
e);
}
}
/**
* This provides a mapping between http://schema.openid.net/ and
* http://axschema.org
* @param val schema name-space URL
* @return mapped value
*/
protected String getAlias(String val) throws RelyingPartyException {
try {
if (axMapping == null) {
axMapping = OpenIDClaimMapper.getInstance().getSregMapping();
}
} catch (Exception e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.OPENID_AUTHENTICATION_FAILED,
e);
}
if (axMapping.containsKey(val)) {
return axMapping.get(val);
}
return val;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -