use of org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPUtils method getMobileNumberForUsername.
/**
* Get the mobile number for Username.
*
* @param username the username
* @return mobile number
* @throws SMSOTPException
*/
public static String getMobileNumberForUsername(String username) throws SMSOTPException, AuthenticationFailedException {
UserRealm userRealm;
String mobile;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(username);
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
userRealm = getUserRealm(tenantDomain);
if (userRealm != null) {
mobile = userRealm.getUserStoreManager().getUserClaimValue(tenantAwareUsername, SMSOTPConstants.MOBILE_CLAIM, null);
} else {
throw new SMSOTPException("Cannot find the user realm for the given tenant domain : " + tenantDomain);
}
} catch (UserStoreException e) {
throw new SMSOTPException("Cannot find the user " + username + " to get the mobile number ", e);
}
return mobile;
}
use of org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPUtils method verifyUserExists.
/**
* Verify whether user Exist in the user store or not.
*
* @param username the Username
* @throws SMSOTPException
*/
public static void verifyUserExists(String username, String tenantDomain) throws SMSOTPException, AuthenticationFailedException {
UserRealm userRealm;
boolean isUserExist = false;
try {
userRealm = SMSOTPUtils.getUserRealm(tenantDomain);
if (userRealm == null) {
throw new SMSOTPException("Super tenant realm not loaded.");
}
UserStoreManager userStoreManager = userRealm.getUserStoreManager();
if (userStoreManager.isExistingUser(username)) {
isUserExist = true;
}
} catch (UserStoreException e) {
throw new SMSOTPException("Error while validating the user.", e);
}
if (!isUserExist) {
if (log.isDebugEnabled()) {
log.debug("User does not exist in the User Store");
}
throw new SMSOTPException("User does not exist in the User Store.");
}
}
use of org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPAuthenticator method checkWithBackUpCodes.
/**
* If user forgets the mobile, then user can use the back up codes to authenticate the user.
*
* @param context the AuthenticationContext
* @param userToken the userToken
* @param authenticatedUser the name of authenticatedUser
* @throws AuthenticationFailedException
*/
private void checkWithBackUpCodes(AuthenticationContext context, String userToken, AuthenticatedUser authenticatedUser) throws AuthenticationFailedException {
String savedOTPString = null;
String username = context.getProperty(SMSOTPConstants.USER_NAME).toString();
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
UserRealm userRealm = getUserRealm(username);
try {
if (userRealm != null) {
savedOTPString = userRealm.getUserStoreManager().getUserClaimValue(tenantAwareUsername, SMSOTPConstants.SAVED_OTP_LIST, null);
}
if (StringUtils.isEmpty(savedOTPString)) {
if (log.isDebugEnabled()) {
log.debug("The claim " + SMSOTPConstants.SAVED_OTP_LIST + " does not contain any values");
}
throw new AuthenticationFailedException("The claim " + SMSOTPConstants.SAVED_OTP_LIST + " does not contain any values");
} else if (savedOTPString.contains(userToken)) {
if (log.isDebugEnabled()) {
log.debug("Found saved backup SMS OTP for user :" + authenticatedUser);
}
context.setSubject(authenticatedUser);
savedOTPString = savedOTPString.replaceAll(userToken, "").replaceAll(",,", ",");
userRealm.getUserStoreManager().setUserClaimValue(tenantAwareUsername, SMSOTPConstants.SAVED_OTP_LIST, savedOTPString, null);
} else {
if (log.isDebugEnabled()) {
log.debug("User entered OTP :" + userToken + " does not match with any of the saved backup codes");
}
throw new AuthenticationFailedException("Verification Error due to Code " + userToken + " mismatch.");
}
} catch (UserStoreException e) {
throw new AuthenticationFailedException("Cannot find the user claim for OTP list for user : " + authenticatedUser, e);
}
}
use of org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPAuthenticator method processAuthenticationResponse.
/**
* Process the response of the SMSOTP end-point.
*
* @param request the HttpServletRequest
* @param response the HttpServletResponse
* @param context the AuthenticationContext
* @throws AuthenticationFailedException
*/
@Override
protected void processAuthenticationResponse(HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) throws AuthenticationFailedException {
String userToken = request.getParameter(SMSOTPConstants.CODE);
String contextToken = (String) context.getProperty(SMSOTPConstants.OTP_TOKEN);
AuthenticatedUser authenticatedUser = (AuthenticatedUser) context.getProperty(SMSOTPConstants.AUTHENTICATED_USER);
if (StringUtils.isEmpty(request.getParameter(SMSOTPConstants.CODE))) {
throw new InvalidCredentialsException("Code cannot not be null");
}
if (Boolean.parseBoolean(request.getParameter(SMSOTPConstants.RESEND))) {
if (log.isDebugEnabled()) {
log.debug("Retrying to resend the OTP");
}
throw new InvalidCredentialsException("Retrying to resend the OTP");
}
if (userToken.equals(contextToken)) {
context.setSubject(authenticatedUser);
} else if (SMSOTPUtils.getBackupCode(context, getName()).equals("true")) {
checkWithBackUpCodes(context, userToken, authenticatedUser);
} else {
context.setProperty(SMSOTPConstants.CODE_MISMATCH, true);
throw new AuthenticationFailedException("Code mismatch");
}
}
use of org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException in project identity-outbound-auth-sms-otp by wso2-extensions.
the class SMSOTPUtils method updateUserAttribute.
/**
* Update the mobile number (user attribute) in user's profile.
*
* @param username the Username
* @param attribute the Attribute
* @throws SMSOTPException
*/
public static void updateUserAttribute(String username, Map<String, String> attribute, String tenantDomain) throws SMSOTPException {
try {
// updating user attributes is independent from tenant association.not tenant association check needed here.
UserRealm userRealm;
// user is always in the super tenant.
userRealm = SMSOTPUtils.getUserRealm(tenantDomain);
if (userRealm == null) {
throw new SMSOTPException("The specified tenant domain " + tenantDomain + " does not exist.");
}
// check whether user already exists in the system.
SMSOTPUtils.verifyUserExists(username, tenantDomain);
UserStoreManager userStoreManager = userRealm.getUserStoreManager();
userStoreManager.setUserClaimValues(username, attribute, null);
} catch (UserStoreException | AuthenticationFailedException e) {
throw new SMSOTPException("Exception occurred while connecting to User Store: Authentication is failed. ", e);
}
}
Aggregations