Generalize OTP creation

revert-70ac1926
tcdlpds@gmail.com 4 years ago
parent 8494f0bc58
commit 648cffaa17

@ -19,7 +19,7 @@ package org.wso2.carbon.device.mgt.common.otp.mgt.dto;
import java.sql.Timestamp; import java.sql.Timestamp;
public class OTPMailDTO { public class OneTimePinDTO {
int id; int id;
String otpToken; String otpToken;

@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.common.spi;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException; import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.OTPManagementException; import org.wso2.carbon.device.mgt.common.exceptions.OTPManagementException;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OTPMailDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPWrapper; import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPWrapper;
public interface OTPManagementService { public interface OTPManagementService {
@ -40,5 +40,5 @@ public interface OTPManagementService {
* @throws OTPManagementException if error occurred whle verifying validity of the OPT * @throws OTPManagementException if error occurred whle verifying validity of the OPT
* @throws BadRequestException if found an null value for OTP * @throws BadRequestException if found an null value for OTP
*/ */
OTPMailDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException; OneTimePinDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException;
} }

@ -17,26 +17,26 @@
package org.wso2.carbon.device.mgt.core.otp.mgt.dao; package org.wso2.carbon.device.mgt.core.otp.mgt.dao;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OTPMailDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException; import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
public interface OTPManagementDAO { public interface OTPManagementDAO {
/** /**
* Save OTP token data and tenant details of registering user * Save OTP token data and tenant details of registering user
* @param otpMailDTO OTPMailDTO * @param oneTimePinDTO OTPMailDTO
* @return Primary key of the newly adding data raw * @return Primary key of the newly adding data raw
* @throws OTPManagementDAOException if error occurred whule storing data * @throws OTPManagementDAOException if error occurred whule storing data
*/ */
int addOTPData(OTPMailDTO otpMailDTO) throws OTPManagementDAOException; int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException;
/** /**
* Get OTP data for requesting One Time Token * Get OTP data for requesting One Time Token
* @param oneTimeToken One Time Token * @param oneTimeToken One Time Token
* @return {@link OTPMailDTO} * @return {@link OneTimePinDTO}
* @throws OTPManagementDAOException if error ocured while getting OTP data for requesting one time token * @throws OTPManagementDAOException if error ocured while getting OTP data for requesting one time token
*/ */
OTPMailDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException; OneTimePinDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException;
/** /**
* Expire the OTP * Expire the OTP

@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.core.otp.mgt.dao.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException; import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OTPMailDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.AbstractDAOImpl; import org.wso2.carbon.device.mgt.core.otp.mgt.dao.AbstractDAOImpl;
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.OTPManagementDAO; import org.wso2.carbon.device.mgt.core.otp.mgt.dao.OTPManagementDAO;
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException; import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
@ -38,11 +38,11 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM
private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class); private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class);
@Override @Override
public int addOTPData(OTPMailDTO otpMailDTO) throws OTPManagementDAOException { public int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to create an OTP data entry"); log.debug("Request received in DAO Layer to create an OTP data entry");
log.debug("OTP Details : "); log.debug("OTP Details : ");
log.debug("OTP key : " + otpMailDTO.getOtpToken() + " Email : " + otpMailDTO.getEmail()); log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail());
} }
String sql = "INSERT INTO DM_OTP_DATA " String sql = "INSERT INTO DM_OTP_DATA "
@ -58,13 +58,13 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
stmt.setString(1, otpMailDTO.getOtpToken()); stmt.setString(1, oneTimePinDTO.getOtpToken());
stmt.setString(2, otpMailDTO.getEmail()); stmt.setString(2, oneTimePinDTO.getEmail());
stmt.setString(3, otpMailDTO.getEmailType()); stmt.setString(3, oneTimePinDTO.getEmailType());
stmt.setString(4, otpMailDTO.getMetaInfo()); stmt.setString(4, oneTimePinDTO.getMetaInfo());
stmt.setTimestamp(5, timestamp); stmt.setTimestamp(5, timestamp);
stmt.setInt(6, otpMailDTO.getTenantId()); stmt.setInt(6, oneTimePinDTO.getTenantId());
stmt.setString(7, otpMailDTO.getUsername()); stmt.setString(7, oneTimePinDTO.getUsername());
stmt.executeUpdate(); stmt.executeUpdate();
try (ResultSet rs = stmt.getGeneratedKeys()) { try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next()) { if (rs.next()) {
@ -75,18 +75,18 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to create an opt entry for email " String msg = "Error occurred while obtaining the DB connection to create an opt entry for email "
+ otpMailDTO.getEmail(); + oneTimePinDTO.getEmail();
log.error(msg, e); log.error(msg, e);
throw new OTPManagementDAOException(msg, e); throw new OTPManagementDAOException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while executing SQL to create an otp entry for email " + otpMailDTO.getEmail(); String msg = "Error occurred while executing SQL to create an otp entry for email " + oneTimePinDTO.getEmail();
log.error(msg, e); log.error(msg, e);
throw new OTPManagementDAOException(msg, e); throw new OTPManagementDAOException(msg, e);
} }
} }
@Override @Override
public OTPMailDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException { public OneTimePinDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get an OTP data entry for OTP"); log.debug("Request received in DAO Layer to get an OTP data entry for OTP");
@ -113,18 +113,18 @@ public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPM
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
OTPMailDTO otpMailDTO = new OTPMailDTO(); OneTimePinDTO oneTimePinDTO = new OneTimePinDTO();
otpMailDTO.setId(rs.getInt("ID")); oneTimePinDTO.setId(rs.getInt("ID"));
otpMailDTO.setOtpToken(rs.getString("OTP_TOKEN")); oneTimePinDTO.setOtpToken(rs.getString("OTP_TOKEN"));
otpMailDTO.setEmail(rs.getString("EMAIL")); oneTimePinDTO.setEmail(rs.getString("EMAIL"));
otpMailDTO.setEmailType(rs.getString("EMAIL_TYPE")); oneTimePinDTO.setEmailType(rs.getString("EMAIL_TYPE"));
otpMailDTO.setMetaInfo(rs.getString("META_INFO")); oneTimePinDTO.setMetaInfo(rs.getString("META_INFO"));
otpMailDTO.setCreatedAt(rs.getTimestamp("CREATED_AT")); oneTimePinDTO.setCreatedAt(rs.getTimestamp("CREATED_AT"));
otpMailDTO.setExpiryTime(rs.getInt("EXPIRY_TIME")); oneTimePinDTO.setExpiryTime(rs.getInt("EXPIRY_TIME"));
otpMailDTO.setExpired(rs.getBoolean("IS_EXPIRED")); oneTimePinDTO.setExpired(rs.getBoolean("IS_EXPIRED"));
otpMailDTO.setTenantId(rs.getInt("TENANT_ID")); oneTimePinDTO.setTenantId(rs.getInt("TENANT_ID"));
otpMailDTO.setUsername(rs.getString("USERNAME")); oneTimePinDTO.setUsername(rs.getString("USERNAME"));
return otpMailDTO; return oneTimePinDTO;
} }
return null; return null;
} }

@ -28,7 +28,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.OTPManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.UnAuthorizedException; import org.wso2.carbon.device.mgt.common.exceptions.UnAuthorizedException;
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata; import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OTPMailDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService; import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
@ -66,46 +66,21 @@ public class OTPManagementServiceImpl implements OTPManagementService {
@Override @Override
public void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException { public void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException {
Tenant tenant = validateTenantCreatingDetails(otpWrapper);
Tenant tenant = validateOTPTokenCreatingRequest(otpWrapper); OneTimePinDTO oneTimePinDTO = createOneTimePin(otpWrapper.getEmail(), otpWrapper.getEmailType(),
if (tenant == null){ otpWrapper.getUsername(), tenant, -1234);
String msg = "Found invalid payload with OTP creating request";
log.error(msg);
throw new BadRequestException(msg);
}
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance()
.getDeviceManagementConfig();
KeyManagerConfigurations kmConfig = deviceManagementConfig.getKeyManagerConfigurations();
String superTenantUsername = kmConfig.getAdminUsername();
if (!otpWrapper.getUsername().equals(superTenantUsername)) {
String msg = "You don't have required permission to create OTP";
log.error(msg);
throw new UnAuthorizedException(msg);
}
Gson gson = new Gson();
String metaInfo = gson.toJson(tenant);
String otpValue = UUID.randomUUID().toString();
OTPMailDTO otpMailDTO = new OTPMailDTO();
otpMailDTO.setEmail(otpWrapper.getEmail());
otpMailDTO.setTenantId(-1234);
otpMailDTO.setUsername(otpWrapper.getUsername());
otpMailDTO.setEmailType(otpWrapper.getEmailType());
otpMailDTO.setMetaInfo(metaInfo);
otpMailDTO.setOtpToken(otpValue);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
if (this.otpManagementDAO.addOTPData(otpMailDTO) == -1) { if (this.otpManagementDAO.addOTPData(oneTimePinDTO) == -1) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "OTP data saving failed. Please, contact Administrator"; String msg = "OTP data saving failed. Please, contact Administrator";
log.error(msg); log.error(msg);
throw new OTPManagementException(msg); throw new OTPManagementException(msg);
} }
sendMail(tenant.getAdminFirstName(), otpValue, tenant.getEmail()); Properties props = new Properties();
props.setProperty("first-name", tenant.getAdminFirstName());
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
sendMail(props, tenant.getEmail());
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Error occurred while disabling AutoCommit."; String msg = "Error occurred while disabling AutoCommit.";
@ -117,7 +92,7 @@ public class OTPManagementServiceImpl implements OTPManagementService {
throw new OTPManagementException(msg, e); throw new OTPManagementException(msg, e);
} catch (OTPManagementDAOException e) { } catch (OTPManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred while saving the OTP data. Email address: " + otpMailDTO.getEmail(); String msg = "Error occurred while saving the OTP data. Email address: " + oneTimePinDTO.getEmail();
log.error(msg, e); log.error(msg, e);
throw new OTPManagementException(msg, e); throw new OTPManagementException(msg, e);
} finally { } finally {
@ -126,15 +101,15 @@ public class OTPManagementServiceImpl implements OTPManagementService {
} }
@Override @Override
public OTPMailDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException { public OneTimePinDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException {
OTPMailDTO otpMailDTO = getOTPDataByToken(oneTimeToken); OneTimePinDTO oneTimePinDTO = getOTPDataByToken(oneTimeToken);
if (otpMailDTO == null) { if (oneTimePinDTO == null) {
String msg = "Couldn't found OTP data for the requesting OTP " + oneTimeToken + " In the system."; String msg = "Couldn't found OTP data for the requesting OTP " + oneTimeToken + " In the system.";
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (otpMailDTO.isExpired()) { if (oneTimePinDTO.isExpired()) {
log.warn("Token is expired. OTP: " + oneTimeToken); log.warn("Token is expired. OTP: " + oneTimeToken);
return null; return null;
} }
@ -142,26 +117,59 @@ public class OTPManagementServiceImpl implements OTPManagementService {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
Timestamp currentTimestamp = new Timestamp(calendar.getTime().getTime()); Timestamp currentTimestamp = new Timestamp(calendar.getTime().getTime());
Timestamp expiredTimestamp = new Timestamp( Timestamp expiredTimestamp = new Timestamp(
otpMailDTO.getCreatedAt().getTime() + otpMailDTO.getExpiryTime() * 1000); oneTimePinDTO.getCreatedAt().getTime() + oneTimePinDTO.getExpiryTime() * 1000);
if (currentTimestamp.after(expiredTimestamp)) { if (currentTimestamp.after(expiredTimestamp)) {
String renewedOTP = UUID.randomUUID().toString(); String renewedOTP = UUID.randomUUID().toString();
renewOTP(otpMailDTO, renewedOTP); renewOTP(oneTimePinDTO, renewedOTP);
Gson gson = new Gson(); Gson gson = new Gson();
Tenant tenant = gson.fromJson(otpMailDTO.getMetaInfo(), Tenant.class); Tenant tenant = gson.fromJson(oneTimePinDTO.getMetaInfo(), Tenant.class);
sendMail(tenant.getAdminFirstName(), renewedOTP, otpMailDTO.getEmail());
Properties props = new Properties();
props.setProperty("first-name", tenant.getAdminFirstName());
props.setProperty("otp-token", renewedOTP);
sendMail(props, oneTimePinDTO.getEmail());
return null; return null;
} }
return otpMailDTO; return oneTimePinDTO;
}
/**
* Create One Time Token
* @param email email
* @param emailType email type
* @param userName username
* @param metaDataObj meta data object
* @param tenantId tenant Id
* @return {@link OneTimePinDTO}
*/
private OneTimePinDTO createOneTimePin(String email, String emailType, String userName, Object metaDataObj,
int tenantId) {
String otpValue = UUID.randomUUID().toString();
Gson gson = new Gson();
String metaInfo = gson.toJson(metaDataObj);
OneTimePinDTO oneTimePinDTO = new OneTimePinDTO();
oneTimePinDTO.setEmail(email);
oneTimePinDTO.setTenantId(tenantId);
oneTimePinDTO.setUsername(userName);
oneTimePinDTO.setEmailType(emailType);
oneTimePinDTO.setMetaInfo(metaInfo);
oneTimePinDTO.setOtpToken(otpValue);
return oneTimePinDTO;
} }
/** /**
* Get OTPData from DB * Get OTPData from DB
* @param oneTimeToken One Time Token * @param oneTimeToken One Time Token
* @return {@link OTPMailDTO} * @return {@link OneTimePinDTO}
* @throws OTPManagementException if error occurred while getting OTP data for given OTP in DB * @throws OTPManagementException if error occurred while getting OTP data for given OTP in DB
*/ */
private OTPMailDTO getOTPDataByToken ( String oneTimeToken) throws OTPManagementException { private OneTimePinDTO getOTPDataByToken ( String oneTimeToken) throws OTPManagementException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
return otpManagementDAO.getOTPDataByToken(oneTimeToken); return otpManagementDAO.getOTPDataByToken(oneTimeToken);
@ -179,61 +187,85 @@ public class OTPManagementServiceImpl implements OTPManagementService {
} }
/** /**
* Validate OTP token creating payload * Validate Tenant details
* @param otpWrapper OTP-Wrapper * @param otpWrapper OTP-Wrapper
* @return true if its valid payload otherwise returns false * @return {@link Tenant} if its valid payload otherwise throws {@link DeviceManagementException}
* @throws DeviceManagementException if invalid payload or unauthorized request received
*/ */
private Tenant validateOTPTokenCreatingRequest(OTPWrapper otpWrapper) { private Tenant validateTenantCreatingDetails(OTPWrapper otpWrapper) throws DeviceManagementException {
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance()
.getDeviceManagementConfig();
KeyManagerConfigurations kmConfig = deviceManagementConfig.getKeyManagerConfigurations();
String superTenantUsername = kmConfig.getAdminUsername();
if (!otpWrapper.getUsername().equals(superTenantUsername)) {
String msg = "You don't have required permission to create OTP";
log.error(msg);
throw new UnAuthorizedException(msg);
}
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
List<Metadata> properties = otpWrapper.getProperties(); List<Metadata> properties = otpWrapper.getProperties();
for (Metadata property : properties) { for (Metadata property : properties) {
if (property == null) {
String msg = "Received invalid property to create OTP.";
log.error(msg);
throw new BadRequestException(msg);
}
switch (property.getMetaKey()) { switch (property.getMetaKey()) {
case OTPProperties.FIRST_NAME: case OTPProperties.FIRST_NAME:
String firstName = property.getMetaValue(); String firstName = property.getMetaValue();
if (StringUtils.isBlank(firstName)) { if (StringUtils.isBlank(firstName)) {
log.error("Received empty or blank first name field with OTP creating payload."); String msg = "Received empty or blank first name field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
tenant.setAdminFirstName(firstName); tenant.setAdminFirstName(firstName);
break; break;
case OTPProperties.LAST_NAME: case OTPProperties.LAST_NAME:
String lastName = property.getMetaValue(); String lastName = property.getMetaValue();
if (StringUtils.isBlank(lastName)) { if (StringUtils.isBlank(lastName)) {
log.error("Received empty or blank last name field with OTP creating payload."); String msg = "Received empty or blank last name field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
tenant.setAdminLastName(lastName); tenant.setAdminLastName(lastName);
break; break;
case OTPProperties.TENANT_ADMIN_USERNAME: case OTPProperties.TENANT_ADMIN_USERNAME:
String username = property.getMetaValue(); String username = property.getMetaValue();
if (StringUtils.isBlank(username)) { if (StringUtils.isBlank(username)) {
log.error("Received empty or blank admin username field with OTP creating payload."); String msg = "Received empty or blank admin username field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
tenant.setAdminName(username); tenant.setAdminName(username);
break; break;
case OTPProperties.TENANT_ADMIN_PASSWORD: case OTPProperties.TENANT_ADMIN_PASSWORD:
String pwd = property.getMetaValue(); String pwd = property.getMetaValue();
if (StringUtils.isBlank(pwd)) { if (StringUtils.isBlank(pwd)) {
log.error("Received empty or blank admin password field with OTP creating payload."); String msg = "Received empty or blank admin password field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
tenant.setAdminPassword(pwd); tenant.setAdminPassword(pwd);
break; break;
default: default:
log.error("Received invalid key with OTP properties for creating OTP."); String msg = "Received invalid key with OTP properties for creating OTP.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
} }
if (StringUtils.isBlank(otpWrapper.getEmail())) { if (StringUtils.isBlank(otpWrapper.getEmail())) {
log.error("Received empty or blank email field with OTP creating payload."); String msg = "Received empty or blank email field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
if (StringUtils.isBlank(otpWrapper.getEmailType())) { if (StringUtils.isBlank(otpWrapper.getEmailType())) {
log.error("Received empty or blank email type field with OTP creating payload."); String msg = "Received empty or blank email type field with OTP creating payload.";
return null; log.error(msg);
throw new BadRequestException(msg);
} }
tenant.setEmail(otpWrapper.getEmail()); tenant.setEmail(otpWrapper.getEmail());
return tenant; return tenant;
@ -241,19 +273,13 @@ public class OTPManagementServiceImpl implements OTPManagementService {
/** /**
* If OTP expired, resend the user verifying mail with renewed OTP * If OTP expired, resend the user verifying mail with renewed OTP
* @param firstName First Name of the User * @param props Mail body properties
* @param renewedOTP Renewed OTP
* @param mailAddress Mail Address of the User * @param mailAddress Mail Address of the User
* @throws OTPManagementException if error occurred while resend the user verifying mail * @throws OTPManagementException if error occurred while resend the user verifying mail
*/ */
private void sendMail(String firstName, String renewedOTP, String mailAddress) private void sendMail(Properties props, String mailAddress) throws OTPManagementException {
throws OTPManagementException {
Properties props = new Properties();
props.setProperty("first-name", firstName);
props.setProperty("otp-token", renewedOTP);
EmailMetaInfo metaInfo = new EmailMetaInfo(mailAddress, props);
try { try {
EmailMetaInfo metaInfo = new EmailMetaInfo(mailAddress, props);
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE, metaInfo); .sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE, metaInfo);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -261,20 +287,22 @@ public class OTPManagementServiceImpl implements OTPManagementService {
log.error(msg, e); log.error(msg, e);
throw new OTPManagementException(msg, e); throw new OTPManagementException(msg, e);
} catch (ConfigurationManagementException e) { } catch (ConfigurationManagementException e) {
throw new OTPManagementException(e); String msg = "Configuration error occurred. Hence mail sending failed.";
log.error(msg, e);
throw new OTPManagementException(msg, e);
} }
} }
/** /**
* Renew the OTP * Renew the OTP
* @param otpMailDTO {@link OTPMailDTO} * @param oneTimePinDTO {@link OneTimePinDTO}
* @param renewedOTP Renewed OTP * @param renewedOTP Renewed OTP
* @throws OTPManagementException if error occurred while renew the OTP * @throws OTPManagementException if error occurred while renew the OTP
*/ */
private void renewOTP(OTPMailDTO otpMailDTO, String renewedOTP) throws OTPManagementException { private void renewOTP(OneTimePinDTO oneTimePinDTO, String renewedOTP) throws OTPManagementException {
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
this.otpManagementDAO.renewOneTimeToken(otpMailDTO.getId(), renewedOTP); this.otpManagementDAO.renewOneTimeToken(oneTimePinDTO.getId(), renewedOTP);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Error occurred while disabling AutoCommit to renew the OTP."; String msg = "Error occurred while disabling AutoCommit to renew the OTP.";

@ -20,7 +20,7 @@ package org.wso2.carbon.webapp.authenticator.framework.authenticator;
import org.apache.catalina.connector.Response; import org.apache.catalina.connector.Response;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OTPMailDTO; import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService; import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
import org.wso2.carbon.webapp.authenticator.framework.AuthenticationInfo; import org.wso2.carbon.webapp.authenticator.framework.AuthenticationInfo;
import org.wso2.carbon.webapp.authenticator.framework.Constants; import org.wso2.carbon.webapp.authenticator.framework.Constants;
@ -49,7 +49,7 @@ public class OneTimeTokenAuthenticator implements WebappAuthenticator {
try { try {
OTPManagementService otpManagementService = AuthenticatorFrameworkDataHolder.getInstance() OTPManagementService otpManagementService = AuthenticatorFrameworkDataHolder.getInstance()
.getOtpManagementService(); .getOtpManagementService();
OTPMailDTO validOTP = otpManagementService.isValidOTP(request.getHeader(Constants.HTTPHeaders OneTimePinDTO validOTP = otpManagementService.isValidOTP(request.getHeader(Constants.HTTPHeaders
.ONE_TIME_TOKEN_HEADER)); .ONE_TIME_TOKEN_HEADER));
if (validOTP != null) { if (validOTP != null) {
authenticationInfo.setStatus(Status.CONTINUE); authenticationInfo.setStatus(Status.CONTINUE);

Loading…
Cancel
Save