📄 userutil.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.user.ui.util;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import org.wso2.solutions.identity.IdentityConstants;
import org.wso2.solutions.identity.IdentityProviderException;
import org.wso2.solutions.identity.UserStore;
import org.wso2.solutions.identity.admin.RegisteredInfoCardInfoAdmin;
import org.wso2.solutions.identity.admin.ReportAdmin;
import org.wso2.solutions.identity.persistence.IPPersistenceManager;
import org.wso2.solutions.identity.persistence.dataobject.ActionDO;
import org.wso2.solutions.identity.persistence.dataobject.RegisteredInfoCardInfoDO;
import org.wso2.solutions.identity.relyingparty.RelyingPartyException;
import org.wso2.solutions.identity.relyingparty.TokenVerifierConstants;
import org.wso2.solutions.identity.user.ui.UIConstants;
import com.opensymphony.xwork2.ActionContext;
public class UserUtil {
/**
* Get the user name corresponding to a given OpenID
* @param openID OpenID used to log in
* @return Corresponding user name
* @throws RelyingPartyException
*/
public static String getUserName(String openID)
throws RelyingPartyException {
UserStore userStore = null;
List<String> users = null;
try {
userStore = UserStore.getInstance();
users = userStore.getAllUserNames();
} catch (IdentityProviderException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.USERNAME_RETRIEVAL_FAILED);
}
if (users == null)
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.NO_USERS_FOUND);
if (openID == null || openID.trim().length() == 0)
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.NULL_OPEN_ID);
Map mapValues = null;
Iterator<String> iterator = null;
iterator = users.iterator();
while (iterator.hasNext()) {
String user = iterator.next();
try {
mapValues = userStore.getClaimValues(user, null);
} catch (IdentityProviderException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.CLAIM_RETRIEVAL_FAILED);
}
if (mapValues != null && !mapValues.isEmpty()) {
// User has defined claims!
String claimId = (String) mapValues
.get(IdentityConstants.CLAIM_OPENID);
if (claimId != null && claimId.equals(openID)) {
return user;
}
}
}
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.NO_USERS_FOUND);
}
/**
* Verify user name/password authentication.
* @param username User name
* @param password Password
* @return true if user successfully authenticated
*/
public static boolean doLogin(String username, String password) {
try {
UserStore userStore = UserStore.getInstance();
return userStore.authenticate(username, password);
} catch (Exception e) {
return false;
}
}
/**
* Verify user information card authentication.
* @param ActionContext
* @return true if user successfully authenticated
* @throws RelyingPartyException
*/
public static boolean verifyInfoCardLogin(ActionContext context,
String openID) throws RelyingPartyException {
HttpServletRequest request = (HttpServletRequest) context
.get(StrutsStatics.HTTP_REQUEST);
String state = (String) request
.getAttribute(TokenVerifierConstants.SERVLET_ATTR_STATE);
if (state == null
|| !TokenVerifierConstants.STATE_SUCCESS.equals(state))
return false;
String ppid = (String) request
.getAttribute(IdentityConstants.CLAIM_PPID);
String issuerInfo = (String) request
.getAttribute(TokenVerifierConstants.ISSUER_INFO);
RegisteredInfoCardInfoAdmin admin = new RegisteredInfoCardInfoAdmin();
RegisteredInfoCardInfoDO info;
try {
info = admin.getInfo(ppid);
} catch (IdentityProviderException e) {
return false;
}
if (info != null && info.getIssuerInfo().equals(issuerInfo)) {
try {
if (openID.equals(getOpenID(ppid))) {
ReportAdmin.record(info.getUserId(),
ActionDO.ACTION_USER_LOG_IN_CARD, "PPID=" + ppid);
context.getSession().put(UIConstants.PPID, ppid);
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
} else {
return false;
}
}
/**
* Get OpenID corresponding to a given PPID
* @param ppid PPID of the self-issued information card
* @return OpenID corresponding to the given PPID
* @throws RelyingPartyException
*/
public static String getOpenID(String ppid) throws RelyingPartyException {
UserStore userStore = null;
List<String> users = null;
try {
userStore = UserStore.getInstance();
users = userStore.getAllUserNames();
} catch (IdentityProviderException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.USERNAME_RETRIEVAL_FAILED);
}
if (users == null)
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.NO_USERS_FOUND);
if (ppid == null || ppid.trim().length() == 0)
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.INVALID_PPID);
Map mapValues = null;
Iterator<String> iterator = null;
iterator = users.iterator();
while (iterator.hasNext()) {
String user = iterator.next();
try {
mapValues = userStore.getClaimValues(user, null);
} catch (IdentityProviderException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.CLAIM_RETRIEVAL_FAILED);
}
if (mapValues != null && !mapValues.isEmpty()) {
// User has defined claims!
String claimId = (String) mapValues
.get(IdentityConstants.CLAIM_OPENID);
String currentppid = (String) mapValues
.get(IdentityConstants.CLAIM_PPID);
if (currentppid == null || !currentppid.equals(ppid)) {
// This is a user signed up with user-name/password or a
// self-issued information card and later registered an
// information card.
IPPersistenceManager db;
try {
db = IPPersistenceManager.getPersistanceManager();
} catch (IdentityProviderException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.DB_CONNECTION_FAILURE);
}
RegisteredInfoCardInfoDO[] infocards = null;
infocards = db.getAllRegistedInfoCardInfoForUser(user);
for (RegisteredInfoCardInfoDO infocard : infocards) {
if (ppid.equals(infocard.getPpid()))
return claimId;
}
}
if (ppid.equals(currentppid))
return claimId;
}
}
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.NO_OPENID_FOUND);
}
/**
* Normalize the provided relying party URL
* @param rpUrl Relying party URL to be normalized
* @return Normalized relying party URL
* @throws RelyingPartyException
*/
public static String getRelyingPartyUrl(String rpUrl)
throws RelyingPartyException {
URI uri = null;
URL url = null;
try {
uri = new URI(rpUrl);
} catch (URISyntaxException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.INVALID_OPENID_RETURNTO);
}
try {
url = uri.normalize().toURL();
url = new URL(url.getProtocol().toLowerCase(), url.getHost()
.toLowerCase(), url.getPort(), url.getPath());
return url.toString();
} catch (MalformedURLException e) {
throw new RelyingPartyException(
IdentityConstants.ErrorCodes.INVALID_OPENID_RETURNTO);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -