forked from community/device-mgt-core
Add OTP service and it's features See merge request entgra/carbon-device-mgt!624revert-70ac1926
commit
fcd92bc10e
@ -0,0 +1,33 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.exceptions;
|
||||
|
||||
/**
|
||||
* Exception thrown due to Database Connection issues.
|
||||
*/
|
||||
public class DBConnectionException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -6779125067467878014L;
|
||||
|
||||
public DBConnectionException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DBConnectionException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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.carbon.device.mgt.common.exceptions;
|
||||
|
||||
public class OTPManagementException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 397485329551276175L;
|
||||
|
||||
public OTPManagementException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
}
|
||||
|
||||
public OTPManagementException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public OTPManagementException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public OTPManagementException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OTPManagementException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.exceptions;
|
||||
|
||||
public class UnAuthorizedException extends DeviceManagementException {
|
||||
private static final long serialVersionUID = 2304023531260840549L;
|
||||
|
||||
public UnAuthorizedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UnAuthorizedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public UnAuthorizedException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public UnAuthorizedException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
}
|
||||
|
||||
public UnAuthorizedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.general;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class OneTimeTokenDetails extends TenantDetail {
|
||||
|
||||
String password;
|
||||
String token;
|
||||
long createdDate;
|
||||
long updatedDate;
|
||||
boolean isExpired;
|
||||
String metaInfo;
|
||||
Map<String, String> replaceValue;
|
||||
String emailType;
|
||||
|
||||
public long getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
public void setCreatedDate(long createdDate) {
|
||||
this.createdDate = createdDate;
|
||||
}
|
||||
|
||||
public long getUpdatedDate() {
|
||||
return updatedDate;
|
||||
}
|
||||
|
||||
public void setUpdatedDate(long updatedDate) {
|
||||
this.updatedDate = updatedDate;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return isExpired;
|
||||
}
|
||||
|
||||
public void setExpired(boolean expired) {
|
||||
isExpired = expired;
|
||||
}
|
||||
|
||||
public String getMetaInfo() {
|
||||
return metaInfo;
|
||||
}
|
||||
|
||||
public void setMetaInfo(String metaInfo) {
|
||||
this.metaInfo = metaInfo;
|
||||
}
|
||||
|
||||
public Map<String, String> getReplaceValue() {
|
||||
return replaceValue;
|
||||
}
|
||||
|
||||
public void setReplaceValue(Map<String, String> replaceValue) {
|
||||
this.replaceValue = replaceValue;
|
||||
}
|
||||
|
||||
public String getEmailType() {
|
||||
return emailType;
|
||||
}
|
||||
|
||||
public void setEmailType(String emailType) {
|
||||
this.emailType = emailType;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.invitation.mgt;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel(
|
||||
value = "DeviceEnrollmentInvitation",
|
||||
description = "Holds data to send device enrollment invitation to list of existing users.")
|
||||
public class DeviceEnrollmentInvitation implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6933837278652532052L;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "usernames",
|
||||
value = "List of usernames of users.",
|
||||
required = true)
|
||||
private List<String> usernames;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "deviceEnrollmentTypes",
|
||||
value = "List of enrollment types against device types.")
|
||||
private List<DeviceEnrollmentType> deviceEnrollmentTypes;
|
||||
|
||||
public List<String> getUsernames() {
|
||||
return usernames;
|
||||
}
|
||||
|
||||
public void setUsernames(List<String> usernames) {
|
||||
this.usernames = usernames;
|
||||
}
|
||||
|
||||
public List<DeviceEnrollmentType> getDeviceEnrollmentTypes() {
|
||||
return deviceEnrollmentTypes;
|
||||
}
|
||||
|
||||
public void setDeviceEnrollmentTypes(
|
||||
List<DeviceEnrollmentType> deviceEnrollmentTypes) {
|
||||
this.deviceEnrollmentTypes = deviceEnrollmentTypes;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.wso2.carbon.device.mgt.common.invitation.mgt;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
import java.util.List;
|
||||
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@XmlType(name = "DeviceEnrollmentInvitationDetails", propOrder = {
|
||||
"enrollmentDetails"
|
||||
})
|
||||
public class DeviceEnrollmentInvitationDetails {
|
||||
|
||||
@XmlElement(name = "EnrollmentDetails")
|
||||
private List<EnrollmentDetails> enrollmentDetails;
|
||||
|
||||
public List<EnrollmentDetails> getEnrollmentDetails() {
|
||||
return enrollmentDetails;
|
||||
}
|
||||
|
||||
public void setEnrollmentDetails(List<EnrollmentDetails> enrollmentDetails) {
|
||||
this.enrollmentDetails = enrollmentDetails;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.invitation.mgt;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel(
|
||||
value = "DeviceEnrollmentType",
|
||||
description = "Holds data of enrollment types against device types.")
|
||||
public class DeviceEnrollmentType implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6563596191450032613L;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "deviceType",
|
||||
value = "Device type (i.e: android, ios, windows)",
|
||||
required = true)
|
||||
private String deviceType;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "enrollmentType",
|
||||
value = "Enrollment type (i.e: BYOD, COPE, COSU)",
|
||||
required = true)
|
||||
private List<String> enrollmentType;
|
||||
|
||||
public String getDeviceType() {
|
||||
return deviceType;
|
||||
}
|
||||
|
||||
public void setDeviceType(String deviceType) {
|
||||
this.deviceType = deviceType;
|
||||
}
|
||||
|
||||
public List<String> getEnrollmentType() {
|
||||
return enrollmentType;
|
||||
}
|
||||
|
||||
public void setEnrollmentType(List<String> enrollmentType) {
|
||||
this.enrollmentType = enrollmentType;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.wso2.carbon.device.mgt.common.invitation.mgt;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@XmlType(name = "EnrollmentDetails", propOrder = {
|
||||
"enrollmentType",
|
||||
"enrollmentSteps"
|
||||
})
|
||||
public class EnrollmentDetails {
|
||||
|
||||
@XmlElement(name = "EnrollmentType")
|
||||
private String enrollmentType;
|
||||
|
||||
@XmlElement(name = "EnrollmentSteps")
|
||||
private String enrollmentSteps;
|
||||
|
||||
public String getEnrollmentType() {
|
||||
return enrollmentType;
|
||||
}
|
||||
|
||||
public void setEnrollmentType(String enrollmentType) {
|
||||
this.enrollmentType = enrollmentType;
|
||||
}
|
||||
|
||||
public String getEnrollmentSteps() {
|
||||
return enrollmentSteps;
|
||||
}
|
||||
|
||||
public void setEnrollmentSteps(String enrollmentSteps) {
|
||||
this.enrollmentSteps = enrollmentSteps;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.otp.mgt;
|
||||
|
||||
public enum OTPEmailTypes {
|
||||
USER_VERIFY, DEVICE_ENROLLMENT
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.otp.mgt.dto;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class OneTimePinDTO {
|
||||
|
||||
int id;
|
||||
String otpToken;
|
||||
int tenantId;
|
||||
String username;
|
||||
String email;
|
||||
String emailType;
|
||||
String metaInfo;
|
||||
Timestamp createdAt;
|
||||
int expiryTime;
|
||||
boolean isExpired;
|
||||
|
||||
public int getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public void setTenantId(int tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getOtpToken() {
|
||||
return otpToken;
|
||||
}
|
||||
|
||||
public void setOtpToken(String otpToken) {
|
||||
this.otpToken = otpToken;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getEmailType() {
|
||||
return emailType;
|
||||
}
|
||||
|
||||
public void setEmailType(String emailType) {
|
||||
this.emailType = emailType;
|
||||
}
|
||||
|
||||
public String getMetaInfo() { return metaInfo; }
|
||||
|
||||
public void setMetaInfo(String metaInfo) {
|
||||
this.metaInfo = metaInfo;
|
||||
}
|
||||
|
||||
public Timestamp getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(Timestamp createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public int getExpiryTime() {
|
||||
return expiryTime;
|
||||
}
|
||||
|
||||
public void setExpiryTime(int expiryTime) {
|
||||
this.expiryTime = expiryTime;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return isExpired;
|
||||
}
|
||||
|
||||
public void setExpired(boolean expired) {
|
||||
isExpired = expired;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.otp.mgt.wrapper;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OTPWrapper {
|
||||
|
||||
private String email;
|
||||
private String emailType;
|
||||
private String username;
|
||||
private List<Metadata> properties;
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getEmailType() {
|
||||
return emailType;
|
||||
}
|
||||
|
||||
public void setEmailType(String emailType) {
|
||||
this.emailType = emailType;
|
||||
}
|
||||
|
||||
public String getUsername() { return username; }
|
||||
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
|
||||
public List<Metadata> getProperties() { return properties; }
|
||||
|
||||
public void setProperties(List<Metadata> properties) { this.properties = properties; }
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.common.spi;
|
||||
|
||||
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.OTPManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation;
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPWrapper;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface OTPManagementService {
|
||||
|
||||
/**
|
||||
* Create OTP token and store tenant details in the DB
|
||||
* @param otpWrapper OTP Mail Wrapper object which contains tenant details of registering user
|
||||
* @throws OTPManagementException if error occurs while creating OTP token and storing tenant details.
|
||||
* @throws BadRequestException if found and incompatible payload to create OTP token.
|
||||
*/
|
||||
void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException;
|
||||
|
||||
/**
|
||||
* Check the validity of the OTP
|
||||
* @param oneTimeToken OTP
|
||||
* @return The OTP data
|
||||
* @throws OTPManagementException if error occurred whle verifying validity of the OPT
|
||||
* @throws BadRequestException if found an null value for OTP
|
||||
*/
|
||||
OneTimePinDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException;
|
||||
|
||||
/**
|
||||
* Invalidate the OTP and send welcome mail
|
||||
* @param oneTimeToken OTP
|
||||
* @param email email address
|
||||
* @param properties email properties to add to email body
|
||||
* @throws OTPManagementException if error occurred while invalidate the OTP or send welcome email
|
||||
*/
|
||||
void completeSelfRegistration(String oneTimeToken, String email, Map<String, String> properties)
|
||||
throws OTPManagementException;
|
||||
|
||||
/**
|
||||
* Create OTP token and send device enrollment invitation
|
||||
* @param deviceEnrollmentInvitation object which contains device enrollment invitation related details
|
||||
* @throws OTPManagementException if error occurred while creating OTP token &/ sending mail
|
||||
*/
|
||||
void sendDeviceEnrollmentInvitationMail(DeviceEnrollmentInvitation deviceEnrollmentInvitation)
|
||||
throws OTPManagementException;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.util.ConnectionManagerUtil;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* This class deals with getting the DB connection.
|
||||
*/
|
||||
public abstract class AbstractDAOImpl {
|
||||
|
||||
protected Connection getDBConnection() throws DBConnectionException {
|
||||
return ConnectionManagerUtil.getDBConnection();
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
|
||||
|
||||
public interface OTPManagementDAO {
|
||||
|
||||
/**
|
||||
* Save OTP token data and tenant details of registering user
|
||||
* @param oneTimePinDTO OTPMailDTO
|
||||
* @return Primary key of the newly adding data raw
|
||||
* @throws OTPManagementDAOException if error occurred whule storing data
|
||||
*/
|
||||
int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException;
|
||||
|
||||
/**
|
||||
* Get OTP data for requesting One Time Token
|
||||
* @param oneTimeToken One Time Token
|
||||
* @return {@link OneTimePinDTO}
|
||||
* @throws OTPManagementDAOException if error ocured while getting OTP data for requesting one time token
|
||||
*/
|
||||
OneTimePinDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException;
|
||||
|
||||
/**
|
||||
* Expire the OTP
|
||||
* @param oneTimeToken OTP
|
||||
* @throws OTPManagementDAOException if error occurred while updating the OTP validity.
|
||||
*/
|
||||
boolean expireOneTimeToken(String oneTimeToken) throws OTPManagementDAOException;
|
||||
|
||||
/**
|
||||
* Update OTP with renewed OTP
|
||||
* @param id ID
|
||||
* @param oneTimeToken One Time Token
|
||||
* @throws OTPManagementDAOException if error occured while updating OTP
|
||||
*/
|
||||
void renewOneTimeToken(int id, String oneTimeToken) throws OTPManagementDAOException;
|
||||
|
||||
/**
|
||||
* To veify whether email and email type exists or not
|
||||
* @param email email
|
||||
* @param emailType email type
|
||||
* @return true if email and email type exists otherwise returns false
|
||||
* @throws OTPManagementDAOException if error occurred while verify existance of the email and email type
|
||||
*/
|
||||
boolean isEmailExist (String email, String emailType) throws OTPManagementDAOException;
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineException;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.impl.GenericOTPManagementDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.impl.OracleOTPManagementDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.impl.PostgreSQLOTPManagementDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.impl.SQLServerOTPManagementDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.util.ConnectionManagerUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so
|
||||
* that the business objection handling layer doesn't need to be aware of the same providing seamless plug-ability of
|
||||
* different data sources, connection acquisition mechanisms as well as different forms of DAO implementations to the
|
||||
* high-level implementations that require Application management related metadata persistence.
|
||||
*/
|
||||
public class OTPManagementDAOFactory {
|
||||
|
||||
private static String databaseEngine;
|
||||
private static final Log log = LogFactory.getLog(OTPManagementDAOFactory.class);
|
||||
|
||||
public static void init(String datasourceName) {
|
||||
ConnectionManagerUtil.resolveDataSource(datasourceName);
|
||||
databaseEngine = ConnectionManagerUtil.getDatabaseType();
|
||||
}
|
||||
|
||||
public static void init(DataSource dtSource) {
|
||||
try (Connection connection = dtSource.getConnection()) {
|
||||
databaseEngine = connection.getMetaData().getDatabaseProductName();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while retrieving config.datasource connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static OTPManagementDAO getOTPManagementDAO() {
|
||||
if (databaseEngine != null) {
|
||||
switch (databaseEngine) {
|
||||
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
|
||||
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:
|
||||
return new GenericOTPManagementDAOImpl();
|
||||
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
|
||||
return new PostgreSQLOTPManagementDAOImpl();
|
||||
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
|
||||
return new SQLServerOTPManagementDAOImpl();
|
||||
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
|
||||
return new OracleOTPManagementDAOImpl();
|
||||
default:
|
||||
throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine);
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Database engine has not initialized properly.");
|
||||
}
|
||||
}
|
@ -0,0 +1,243 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
|
||||
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.OTPManagementDAO;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class GenericOTPManagementDAOImpl extends AbstractDAOImpl implements OTPManagementDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GenericOTPManagementDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public int addOTPData(OneTimePinDTO oneTimePinDTO) throws OTPManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to create an OTP data entry");
|
||||
log.debug("OTP Details : ");
|
||||
log.debug("OTP key : " + oneTimePinDTO.getOtpToken() + " Email : " + oneTimePinDTO.getEmail());
|
||||
}
|
||||
|
||||
String sql = "INSERT INTO DM_OTP_DATA "
|
||||
+ "(OTP_TOKEN, "
|
||||
+ "EMAIL, "
|
||||
+ "EMAIL_TYPE, "
|
||||
+ "META_INFO, "
|
||||
+ "CREATED_AT,"
|
||||
+ "TENANT_ID,"
|
||||
+ "USERNAME) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, oneTimePinDTO.getOtpToken());
|
||||
stmt.setString(2, oneTimePinDTO.getEmail());
|
||||
stmt.setString(3, oneTimePinDTO.getEmailType());
|
||||
stmt.setString(4, oneTimePinDTO.getMetaInfo());
|
||||
stmt.setTimestamp(5, timestamp);
|
||||
stmt.setInt(6, oneTimePinDTO.getTenantId());
|
||||
stmt.setString(7, oneTimePinDTO.getUsername());
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an opt entry for email "
|
||||
+ oneTimePinDTO.getEmail();
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an otp entry for email " + oneTimePinDTO.getEmail();
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OneTimePinDTO getOTPDataByToken (String oneTimeToken) throws OTPManagementDAOException {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to get an OTP data entry for OTP");
|
||||
log.debug("OTP Details : OTP key : " + oneTimeToken );
|
||||
}
|
||||
|
||||
String sql = "SELECT "
|
||||
+ "ID, "
|
||||
+ "OTP_TOKEN, "
|
||||
+ "EMAIL, "
|
||||
+ "EMAIL_TYPE, "
|
||||
+ "META_INFO, "
|
||||
+ "CREATED_AT, "
|
||||
+ "EXPIRY_TIME, "
|
||||
+ "IS_EXPIRED, "
|
||||
+ "TENANT_ID, "
|
||||
+ "USERNAME FROM DM_OTP_DATA "
|
||||
+ "WHERE OTP_TOKEN = ?";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, oneTimeToken);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
OneTimePinDTO oneTimePinDTO = new OneTimePinDTO();
|
||||
oneTimePinDTO.setId(rs.getInt("ID"));
|
||||
oneTimePinDTO.setOtpToken(rs.getString("OTP_TOKEN"));
|
||||
oneTimePinDTO.setEmail(rs.getString("EMAIL"));
|
||||
oneTimePinDTO.setEmailType(rs.getString("EMAIL_TYPE"));
|
||||
oneTimePinDTO.setMetaInfo(rs.getString("META_INFO"));
|
||||
oneTimePinDTO.setCreatedAt(rs.getTimestamp("CREATED_AT"));
|
||||
oneTimePinDTO.setExpiryTime(rs.getInt("EXPIRY_TIME"));
|
||||
oneTimePinDTO.setExpired(rs.getBoolean("IS_EXPIRED"));
|
||||
oneTimePinDTO.setTenantId(rs.getInt("TENANT_ID"));
|
||||
oneTimePinDTO.setUsername(rs.getString("USERNAME"));
|
||||
return oneTimePinDTO;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to get OPT data for given OTP. OTP: "
|
||||
+ oneTimeToken;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to get OTP data for OTP. One time token: " + oneTimeToken;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean expireOneTimeToken(String oneTimeToken) throws OTPManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to update an OTP data entry for OTP");
|
||||
log.debug("OTP Details : OTP key : " + oneTimeToken );
|
||||
}
|
||||
|
||||
String sql = "UPDATE DM_OTP_DATA "
|
||||
+ "SET "
|
||||
+ "IS_EXPIRED = ? "
|
||||
+ "WHERE OTP_TOKEN = ?";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setBoolean(1, true);
|
||||
stmt.setString(2, oneTimeToken);
|
||||
return stmt.executeUpdate() == 1;
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to update the OTP token validity.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred when obtaining database connection for updating the OTP token validity.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renewOneTimeToken(int id, String oneTimeToken) throws OTPManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to update an OTP data entry for OTP");
|
||||
log.debug("OTP Details : OTP key : " + oneTimeToken );
|
||||
}
|
||||
|
||||
String sql = "UPDATE DM_OTP_DATA "
|
||||
+ "SET "
|
||||
+ "OTP_TOKEN = ?, "
|
||||
+ "CREATED_AT = ? "
|
||||
+ "WHERE ID = ?";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, oneTimeToken);
|
||||
stmt.setTimestamp(2, timestamp);
|
||||
stmt.setInt(3, id);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to update the OTP token.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred when executing sql query to update the OTP token.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmailExist (String email, String emailType) throws OTPManagementDAOException {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to verify whether email was registed with emai type in OTP");
|
||||
log.debug("OTP Details : email : " + email + " email type: " + emailType );
|
||||
}
|
||||
|
||||
String sql = "SELECT "
|
||||
+ "ID "
|
||||
+ "FROM DM_OTP_DATA "
|
||||
+ "WHERE EMAIL = ? AND "
|
||||
+ "EMAIL_TYPE = ?";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, email);
|
||||
stmt.setString(2, emailType);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
return rs.next();
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to verify email and email type exist in OTP."
|
||||
+ " Email: " + email + "Email Type: " + emailType;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to verify email and email type exist in OTP. Email: "
|
||||
+ email + "Email Type: " + emailType;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao.impl;
|
||||
|
||||
/**
|
||||
* This handles OTP managing DAO methods which are specific to Oracle.
|
||||
*/
|
||||
public class OracleOTPManagementDAOImpl extends GenericOTPManagementDAOImpl{
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao.impl;
|
||||
|
||||
/**
|
||||
* This handles OTP managing DAO methods which are specific to PostgreSQL.
|
||||
*/
|
||||
public class PostgreSQLOTPManagementDAOImpl extends GenericOTPManagementDAOImpl{
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.dao.impl;
|
||||
|
||||
/**
|
||||
* This handles OTP managing DAO methods which are specific to MSSQL.
|
||||
*/
|
||||
public class SQLServerOTPManagementDAOImpl extends GenericOTPManagementDAOImpl{
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.exception;
|
||||
|
||||
/**
|
||||
* Exception thrown during the ApplicationDTO Management DAO operations.
|
||||
*/
|
||||
public class OTPManagementDAOException extends Exception {
|
||||
|
||||
public OTPManagementDAOException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public OTPManagementDAOException(String message) {
|
||||
super(message, new Exception());
|
||||
}
|
||||
}
|
@ -0,0 +1,461 @@
|
||||
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.base.MultitenantConstants;
|
||||
import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
|
||||
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.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.UnAuthorizedException;
|
||||
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitation;
|
||||
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentInvitationDetails;
|
||||
import org.wso2.carbon.device.mgt.common.invitation.mgt.DeviceEnrollmentType;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.OTPEmailTypes;
|
||||
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.core.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
|
||||
import org.wso2.carbon.device.mgt.core.config.keymanager.KeyManagerConfigurations;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.OTPManagementDAO;
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPWrapper;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.OTPManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.exception.OTPManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.otp.mgt.util.ConnectionManagerUtil;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo;
|
||||
import org.apache.commons.validator.routines.EmailValidator;
|
||||
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||
import org.wso2.carbon.user.api.Tenant;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import static org.wso2.carbon.device.mgt.common.DeviceManagementConstants.OTPProperties;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
public class OTPManagementServiceImpl implements OTPManagementService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(OTPManagementServiceImpl.class);
|
||||
private OTPManagementDAO otpManagementDAO;
|
||||
|
||||
public OTPManagementServiceImpl() {
|
||||
initDataAccessObjects();
|
||||
}
|
||||
|
||||
private void initDataAccessObjects() {
|
||||
otpManagementDAO = OTPManagementDAOFactory.getOTPManagementDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendUserVerifyingMail(OTPWrapper otpWrapper) throws OTPManagementException, DeviceManagementException {
|
||||
Tenant tenant = validateTenantCreatingDetails(otpWrapper);
|
||||
OneTimePinDTO oneTimePinDTO = createOneTimePin(otpWrapper.getEmail(), otpWrapper.getEmailType(),
|
||||
otpWrapper.getUsername(), tenant, -1234);
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
if (this.otpManagementDAO.addOTPData(oneTimePinDTO) == -1) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "OTP data saving failed. Please, contact Administrator";
|
||||
log.error(msg);
|
||||
throw new OTPManagementException(msg);
|
||||
}
|
||||
Properties props = new Properties();
|
||||
props.setProperty("first-name", tenant.getAdminFirstName());
|
||||
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
|
||||
sendMail(props, tenant.getEmail(), DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE);
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while disabling AutoCommit.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (OTPManagementDAOException e) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "Error occurred while saving the OTP data. Email address: " + oneTimePinDTO.getEmail();
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public OneTimePinDTO isValidOTP(String oneTimeToken) throws OTPManagementException, BadRequestException {
|
||||
if (StringUtils.isBlank(oneTimeToken)){
|
||||
String msg = "Received blank OTP to verify. OTP: " + oneTimeToken;
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
|
||||
OneTimePinDTO oneTimePinDTO = getOTPDataByToken(oneTimeToken);
|
||||
if (oneTimePinDTO == null) {
|
||||
String msg = "Couldn't found OTP data for the requesting OTP " + oneTimeToken + " In the system.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
if (oneTimePinDTO.isExpired()) {
|
||||
log.warn("Token is expired. OTP: " + oneTimeToken);
|
||||
return null;
|
||||
}
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
Timestamp currentTimestamp = new Timestamp(calendar.getTime().getTime());
|
||||
Timestamp expiredTimestamp = new Timestamp(
|
||||
oneTimePinDTO.getCreatedAt().getTime() + oneTimePinDTO.getExpiryTime() * 1000);
|
||||
|
||||
if (currentTimestamp.after(expiredTimestamp)) {
|
||||
String renewedOTP = UUID.randomUUID().toString();
|
||||
renewOTP(oneTimePinDTO, renewedOTP);
|
||||
Gson gson = new Gson();
|
||||
Tenant tenant = gson.fromJson(oneTimePinDTO.getMetaInfo(), Tenant.class);
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty("first-name", tenant.getAdminFirstName());
|
||||
props.setProperty("otp-token", renewedOTP);
|
||||
sendMail(props, oneTimePinDTO.getEmail(), DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE);
|
||||
return null;
|
||||
}
|
||||
return oneTimePinDTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeSelfRegistration(String oneTimeToken, String email, Map<String, String> properties)
|
||||
throws OTPManagementException {
|
||||
try {
|
||||
invalidateOTP(oneTimeToken);
|
||||
Properties props = new Properties();
|
||||
properties.forEach(props::setProperty);
|
||||
sendMail(props, email, DeviceManagementConstants.EmailAttributes.USER_WELCOME_TEMPLATE);
|
||||
} catch (OTPManagementException e) {
|
||||
String msg = "Error occurred while completing the self registration via OTP";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate the OTP
|
||||
* @param oneTimeToken OTP
|
||||
* @throws OTPManagementException If error occurred while invalidating the OTP
|
||||
*/
|
||||
private void invalidateOTP(String oneTimeToken) throws OTPManagementException {
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
if (!otpManagementDAO.expireOneTimeToken(oneTimeToken)) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "Couldn't find OTP entry for OTP: " + oneTimeToken;
|
||||
log.error(msg);
|
||||
throw new OTPManagementException(msg);
|
||||
}
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
} catch (OTPManagementDAOException e) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "Error occurred while invalidate the OTP: " + oneTimeToken;
|
||||
log.error(msg);
|
||||
throw new OTPManagementException(msg);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while disabling AutoCommit to invalidate OTP.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection to invalidate OPT.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void sendDeviceEnrollmentInvitationMail(DeviceEnrollmentInvitation deviceEnrollmentInvitation)
|
||||
throws OTPManagementException {
|
||||
DeviceManagementProviderService dms = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider();
|
||||
StringBuilder enrollmentSteps = new StringBuilder();
|
||||
DeviceEnrollmentInvitationDetails deviceEnrollmentInvitationDetails;
|
||||
for (DeviceEnrollmentType deviceEnrollmentType : deviceEnrollmentInvitation.getDeviceEnrollmentTypes()) {
|
||||
deviceEnrollmentInvitationDetails = dms.getDeviceEnrollmentInvitationDetails(
|
||||
deviceEnrollmentType.getDeviceType());
|
||||
if (deviceEnrollmentInvitationDetails != null &&
|
||||
deviceEnrollmentInvitationDetails.getEnrollmentDetails() != null) {
|
||||
for (String enrollmentType : deviceEnrollmentType.getEnrollmentType()) {
|
||||
deviceEnrollmentInvitationDetails.getEnrollmentDetails().stream()
|
||||
.filter(details -> enrollmentType.equals(details.getEnrollmentType())).findFirst()
|
||||
.ifPresent(details -> enrollmentSteps.append(details.getEnrollmentSteps()));
|
||||
}
|
||||
}
|
||||
}
|
||||
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
OneTimePinDTO oneTimePinDTO;
|
||||
Properties props = new Properties();
|
||||
props.setProperty("enrollment-steps", enrollmentSteps.toString());
|
||||
try {
|
||||
for (String username : deviceEnrollmentInvitation.getUsernames()) {
|
||||
String emailAddress = DeviceManagerUtil.getUserClaimValue(
|
||||
username, DeviceManagementConstants.User.CLAIM_EMAIL_ADDRESS);
|
||||
oneTimePinDTO = createOneTimePin(emailAddress, OTPEmailTypes.DEVICE_ENROLLMENT.toString(), username,
|
||||
null, tenantId);
|
||||
props.setProperty("first-name", DeviceManagerUtil.
|
||||
getUserClaimValue(username, DeviceManagementConstants.User.CLAIM_FIRST_NAME));
|
||||
props.setProperty("username", username);
|
||||
props.setProperty("otp-token", oneTimePinDTO.getOtpToken());
|
||||
sendMail(props, emailAddress, DeviceManagementConstants.EmailAttributes.USER_ENROLLMENT_TEMPLATE);
|
||||
}
|
||||
} catch (UserStoreException e) {
|
||||
String msg = "Error occurred while getting claim values to invite user";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param oneTimeToken One Time Token
|
||||
* @return {@link OneTimePinDTO}
|
||||
* @throws OTPManagementException if error occurred while getting OTP data for given OTP in DB
|
||||
*/
|
||||
private OneTimePinDTO getOTPDataByToken ( String oneTimeToken) throws OTPManagementException {
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
return otpManagementDAO.getOTPDataByToken(oneTimeToken);
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection to validate the given OTP.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (OTPManagementDAOException e) {
|
||||
String msg = "Error occurred while getting OTP data from DB. OTP: " + oneTimeToken;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Tenant details
|
||||
* @param otpWrapper OTP-Wrapper
|
||||
* @return {@link Tenant} if its valid payload otherwise throws {@link DeviceManagementException}
|
||||
* @throws DeviceManagementException if invalid payload or unauthorized request received
|
||||
*/
|
||||
private Tenant validateTenantCreatingDetails(OTPWrapper otpWrapper) throws DeviceManagementException {
|
||||
|
||||
DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance()
|
||||
.getDeviceManagementConfig();
|
||||
KeyManagerConfigurations kmConfig = deviceManagementConfig.getKeyManagerConfigurations();
|
||||
|
||||
if (StringUtils.isBlank(otpWrapper.getUsername())) {
|
||||
String msg = "Received Blank username to create OTP. Username: " + otpWrapper.getUsername();
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
|
||||
String[] superTenantDetails = otpWrapper.getUsername().split("@");
|
||||
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(superTenantDetails[superTenantDetails.length - 1])
|
||||
|| !superTenantDetails[0].equals(kmConfig.getAdminUsername())) {
|
||||
String msg = "You don't have required permission to create OTP";
|
||||
log.error(msg);
|
||||
throw new UnAuthorizedException(msg);
|
||||
}
|
||||
|
||||
Tenant tenant = new Tenant();
|
||||
List<Metadata> properties = otpWrapper.getProperties();
|
||||
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()) {
|
||||
case OTPProperties.FIRST_NAME:
|
||||
String firstName = property.getMetaValue();
|
||||
if (StringUtils.isBlank(firstName)) {
|
||||
String msg = "Received empty or blank first name field with OTP creating payload.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
tenant.setAdminFirstName(firstName);
|
||||
break;
|
||||
case OTPProperties.LAST_NAME:
|
||||
String lastName = property.getMetaValue();
|
||||
if (StringUtils.isBlank(lastName)) {
|
||||
String msg = "Received empty or blank last name field with OTP creating payload.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
tenant.setAdminLastName(lastName);
|
||||
break;
|
||||
case OTPProperties.TENANT_ADMIN_PASSWORD:
|
||||
String pwd = property.getMetaValue();
|
||||
if (StringUtils.isBlank(pwd)) {
|
||||
String msg = "Received empty or blank admin password field with OTP creating payload.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
tenant.setAdminPassword(pwd);
|
||||
break;
|
||||
default:
|
||||
String msg = "Received invalid key with OTP properties for creating OTP.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(otpWrapper.getEmail())) {
|
||||
String msg = "Received empty or blank email field with OTP creating payload.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
|
||||
EmailValidator validator = EmailValidator.getInstance();
|
||||
if (!validator.isValid(otpWrapper.getEmail())) {
|
||||
String msg = "Found invalid email. Hence please verify the email address and re-try. Email: " + otpWrapper
|
||||
.getEmail();
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(otpWrapper.getEmailType())) {
|
||||
String msg = "Received empty or blank email type field with OTP creating payload.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
if (otpManagementDAO.isEmailExist(otpWrapper.getEmail(), otpWrapper.getEmailType())) {
|
||||
String msg = "Email is registered to execute the same action. Hence can't proceed.";
|
||||
log.error(msg);
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection to validate the given email and email type.";
|
||||
log.error(msg);
|
||||
throw new DeviceManagementException(msg);
|
||||
} catch (OTPManagementDAOException e) {
|
||||
String msg = "Error occurred while executing SQL query to validate the given email and email type.";
|
||||
log.error(msg);
|
||||
throw new DeviceManagementException(msg);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
|
||||
String[] tenantUsernameDetails = otpWrapper.getEmail().split("@");
|
||||
tenant.setAdminName(tenantUsernameDetails[0]);
|
||||
tenant.setDomain(tenantUsernameDetails[tenantUsernameDetails.length - 1]);
|
||||
tenant.setEmail(otpWrapper.getEmail());
|
||||
return tenant;
|
||||
}
|
||||
|
||||
/**
|
||||
* If OTP expired, resend the user verifying mail with renewed OTP
|
||||
* @param props Mail body properties
|
||||
* @param mailAddress Mail Address of the User
|
||||
* @param template Mail template to be used
|
||||
* @throws OTPManagementException if error occurred while resend the user verifying mail
|
||||
*/
|
||||
private void sendMail(Properties props, String mailAddress, String template) throws OTPManagementException {
|
||||
try {
|
||||
EmailMetaInfo metaInfo = new EmailMetaInfo(mailAddress, props);
|
||||
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
|
||||
.sendEnrolmentInvitation(template, metaInfo);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while sending email using email template '" + template + "'.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (ConfigurationManagementException e) {
|
||||
String msg = "Configuration error occurred. Hence mail sending failed.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renew the OTP
|
||||
* @param oneTimePinDTO {@link OneTimePinDTO}
|
||||
* @param renewedOTP Renewed OTP
|
||||
* @throws OTPManagementException if error occurred while renew the OTP
|
||||
*/
|
||||
private void renewOTP(OneTimePinDTO oneTimePinDTO, String renewedOTP) throws OTPManagementException {
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
this.otpManagementDAO.renewOneTimeToken(oneTimePinDTO.getId(), renewedOTP);
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while disabling AutoCommit to renew the OTP.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection to renew the OTP.";
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} catch (OTPManagementDAOException e) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "Error occurred while renew the OTP. OTP: " + renewedOTP;
|
||||
log.error(msg, e);
|
||||
throw new OTPManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you 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.carbon.device.mgt.core.otp.mgt.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.IllegalTransactionStateException;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* ConnectionManagerUtil is responsible for handling all the datasource connections utilities.
|
||||
*/
|
||||
public class ConnectionManagerUtil {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class);
|
||||
private static final ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
|
||||
private static DataSource dataSource;
|
||||
|
||||
public static void openDBConnection() throws DBConnectionException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn != null) {
|
||||
String msg = "Database connection has already been obtained.";
|
||||
log.error(msg);
|
||||
throw new IllegalTransactionStateException(msg);
|
||||
}
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
String msg = "Failed to get a database connection.";
|
||||
log.error(msg, e);
|
||||
throw new DBConnectionException(msg, e);
|
||||
}
|
||||
currentConnection.set(conn);
|
||||
}
|
||||
|
||||
public static Connection getDBConnection() throws DBConnectionException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
currentConnection.set(conn);
|
||||
} catch (SQLException e) {
|
||||
throw new DBConnectionException("Failed to get database connection.", e);
|
||||
}
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static void beginDBTransaction() throws TransactionManagementException, DBConnectionException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
conn = getDBConnection();
|
||||
} else if (inTransaction(conn)) {
|
||||
String msg = "Transaction has already been started.";
|
||||
log.error(msg);
|
||||
throw new IllegalTransactionStateException(msg);
|
||||
}
|
||||
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while starting a database transaction.";
|
||||
log.error(msg, e);
|
||||
throw new TransactionManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void endDBTransaction() throws TransactionManagementException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("Database connection is not active.");
|
||||
}
|
||||
|
||||
if (!inTransaction(conn)) {
|
||||
throw new IllegalTransactionStateException("Transaction has not been started.");
|
||||
}
|
||||
|
||||
try {
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
throw new TransactionManagementException("Error occurred while ending database transaction.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void commitDBTransaction() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("Database connection is not active.");
|
||||
}
|
||||
|
||||
if (!inTransaction(conn)) {
|
||||
throw new IllegalTransactionStateException("Transaction has not been started.");
|
||||
}
|
||||
|
||||
try {
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while committing the transaction", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void rollbackDBTransaction() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("Database connection is not active.");
|
||||
}
|
||||
|
||||
if (!inTransaction(conn)) {
|
||||
throw new IllegalTransactionStateException("Transaction has not been started.");
|
||||
}
|
||||
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeDBConnection() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("Database connection is not active.");
|
||||
}
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while closing the connection", e);
|
||||
}
|
||||
currentConnection.remove();
|
||||
}
|
||||
|
||||
private static boolean inTransaction(Connection conn) {
|
||||
boolean inTransaction = true;
|
||||
try {
|
||||
if (conn.getAutoCommit()) {
|
||||
inTransaction = false;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalTransactionStateException("Failed to get transaction state.");
|
||||
}
|
||||
return inTransaction;
|
||||
}
|
||||
|
||||
public static boolean isTransactionStarted() throws DBConnectionException {
|
||||
Connection connection = getDBConnection();
|
||||
return inTransaction(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the datasource from the datasource definition.
|
||||
*
|
||||
* @param dataSourceName Name of the datasource
|
||||
* @return DataSource resolved by the datasource name
|
||||
*/
|
||||
public static DataSource resolveDataSource(String dataSourceName) {
|
||||
try {
|
||||
dataSource = InitialContext.doLookup(dataSourceName);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public static String getDatabaseType() {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
return connection.getMetaData().getDatabaseProductName();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while retrieving config.datasource connection", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* To check whether particular database that is used for application management supports batch query execution.
|
||||
*
|
||||
* @return true if batch query is supported, otherwise false.
|
||||
*/
|
||||
public static boolean isBatchQuerySupported() {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
return connection.getMetaData().supportsBatchUpdates();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while checking whether database supports batch updates", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void init(DataSource dtSource) {
|
||||
dataSource = dtSource;
|
||||
}
|
||||
}
|
@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.ui.request.interceptor;
|
||||
|
||||
import io.entgra.ui.request.interceptor.util.HandlerConstants;
|
||||
import io.entgra.ui.request.interceptor.util.HandlerUtil;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.FileUploadException;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.cookie.SM;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.entity.mime.content.InputStreamBody;
|
||||
import org.wso2.carbon.device.application.mgt.common.ProxyResponse;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
@WebServlet(
|
||||
name = "OTPRequestHandlerServlet",
|
||||
description = "This servlet intercepts the otp-api requests initiated from the user interface and validate "
|
||||
+ "before forwarding to the backend",
|
||||
urlPatterns = {
|
||||
"/otp-invoke/*"
|
||||
}
|
||||
)
|
||||
public class OTPInvokerHandler extends HttpServlet {
|
||||
private static final Log log = LogFactory.getLog(OTPInvokerHandler.class);
|
||||
private static final long serialVersionUID = 3109569827313066220L;
|
||||
private static String apiEndpoint;
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
||||
try {
|
||||
if (validateRequest(req, resp)) {
|
||||
HttpPost postRequest = new HttpPost(HandlerUtil.generateBackendRequestURL(req, apiEndpoint));
|
||||
generateRequestEntity(req, postRequest);
|
||||
ProxyResponse proxyResponse = HandlerUtil.execute(postRequest);
|
||||
|
||||
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||
log.error("Error occurred while invoking the POST API endpoint.");
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
return;
|
||||
}
|
||||
HandlerUtil.handleSuccess(resp, proxyResponse);
|
||||
}
|
||||
} catch (FileUploadException e) {
|
||||
log.error("Error occurred when processing Multipart POST request.", e);
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred when processing POST request.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
||||
try {
|
||||
if (validateRequest(req, resp)) {
|
||||
HttpGet getRequest = new HttpGet(HandlerUtil.generateBackendRequestURL(req, apiEndpoint));
|
||||
copyRequestHeaders(req, getRequest, false);
|
||||
ProxyResponse proxyResponse = HandlerUtil.execute(getRequest);
|
||||
|
||||
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||
log.error("Error occurred while invoking the GET API endpoint.");
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
return;
|
||||
}
|
||||
HandlerUtil.handleSuccess(resp, proxyResponse);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred when processing GET request.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHead(HttpServletRequest req, HttpServletResponse resp) {
|
||||
try {
|
||||
if (validateRequest(req, resp)) {
|
||||
HttpHead headRequest = new HttpHead(HandlerUtil.generateBackendRequestURL(req, apiEndpoint));
|
||||
copyRequestHeaders(req, headRequest, false);
|
||||
ProxyResponse proxyResponse = HandlerUtil.execute(headRequest);
|
||||
|
||||
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||
log.error("Error occurred while invoking the HEAD API endpoint.");
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
return;
|
||||
}
|
||||
HandlerUtil.handleSuccess(resp, proxyResponse);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred when processing HEAD request.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
|
||||
try {
|
||||
if (validateRequest(req, resp)) {
|
||||
HttpPut putRequest = new HttpPut(HandlerUtil.generateBackendRequestURL(req, apiEndpoint));
|
||||
generateRequestEntity(req, putRequest);
|
||||
ProxyResponse proxyResponse = HandlerUtil.execute(putRequest);
|
||||
|
||||
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||
log.error("Error occurred while invoking the PUT API endpoint.");
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
return;
|
||||
}
|
||||
HandlerUtil.handleSuccess(resp, proxyResponse);
|
||||
}
|
||||
} catch (FileUploadException e) {
|
||||
log.error("Error occurred when processing Multipart PUT request.", e);
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred when processing PUT request.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
|
||||
try {
|
||||
if (validateRequest(req, resp)) {
|
||||
HttpDelete deleteRequest = new HttpDelete(HandlerUtil.generateBackendRequestURL(req, apiEndpoint));
|
||||
copyRequestHeaders(req, deleteRequest, false);
|
||||
ProxyResponse proxyResponse = HandlerUtil.execute(deleteRequest);
|
||||
|
||||
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||
log.error("Error occurred while invoking the DELETE API endpoint.");
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
return;
|
||||
}
|
||||
HandlerUtil.handleSuccess(resp, proxyResponse);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred when processing DELETE request.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate te request entity for POST and PUT requests from the incoming request.
|
||||
*
|
||||
* @param req incoming {@link HttpServletRequest}.
|
||||
* @param proxyRequest proxy request instance.
|
||||
* @throws FileUploadException If unable to parse the incoming request for multipart content extraction.
|
||||
* @throws IOException If error occurred while generating the request body.
|
||||
*/
|
||||
private void generateRequestEntity(HttpServletRequest req, HttpEntityEnclosingRequestBase proxyRequest)
|
||||
throws FileUploadException, IOException {
|
||||
if (ServletFileUpload.isMultipartContent(req)) {
|
||||
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
|
||||
List<FileItem> fileItemList = servletFileUpload.parseRequest(req);
|
||||
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
|
||||
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
for (FileItem item: fileItemList) {
|
||||
if (!item.isFormField()) {
|
||||
entityBuilder.addPart(item.getFieldName(), new InputStreamBody(item.getInputStream(),
|
||||
ContentType.create(item.getContentType()), item.getName()));
|
||||
} else {
|
||||
entityBuilder.addTextBody(item.getFieldName(), item.getString());
|
||||
}
|
||||
}
|
||||
proxyRequest.setEntity(entityBuilder.build());
|
||||
copyRequestHeaders(req, proxyRequest, false);
|
||||
} else {
|
||||
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) ||
|
||||
StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
|
||||
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(),
|
||||
Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)));
|
||||
proxyRequest.setEntity(entity);
|
||||
}
|
||||
copyRequestHeaders(req, proxyRequest, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy incoming request headers to the proxy request.
|
||||
*
|
||||
* @param req incoming {@link HttpServletRequest}
|
||||
* @param httpRequest proxy request instance.
|
||||
* @param preserveContentType <code>true</code> if content type header needs to be preserved.
|
||||
* This should be set to <code>false</code> when handling multipart requests as Http
|
||||
* client will generate the Content-Type header automatically.
|
||||
*/
|
||||
private void copyRequestHeaders(HttpServletRequest req, HttpRequestBase httpRequest, boolean preserveContentType) {
|
||||
Enumeration<String> headerNames = req.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String headerName = headerNames.nextElement();
|
||||
if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH) ||
|
||||
headerName.equalsIgnoreCase(SM.COOKIE) ||
|
||||
(!preserveContentType && headerName.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE))) {
|
||||
continue;
|
||||
}
|
||||
Enumeration<String> headerValues = req.getHeaders(headerName);
|
||||
while (headerValues.hasMoreElements()) {
|
||||
httpRequest.setHeader(headerName, headerValues.nextElement());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Validates the incoming request.
|
||||
*
|
||||
* @param req {@link HttpServletRequest}
|
||||
* @param resp {@link HttpServletResponse}
|
||||
* @return If request is a valid one, returns TRUE, otherwise return FALSE
|
||||
* @throws IOException If and error occurs while witting error response to client side
|
||||
*/
|
||||
private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws IOException {
|
||||
String schema = req.getScheme();
|
||||
apiEndpoint = schema + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host")
|
||||
+ HandlerConstants.COLON + HandlerUtil.getCorePort(schema);
|
||||
|
||||
if (StringUtils.isBlank(req.getHeader(HandlerConstants.OTP_HEADER))) {
|
||||
log.error("Unauthorized, Please provide OTP token.");
|
||||
handleError(resp, HttpStatus.SC_UNAUTHORIZED);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle error requests
|
||||
*
|
||||
* @param resp {@link HttpServletResponse}
|
||||
* @param errorCode HTTP error status code
|
||||
* @throws IOException If error occurred when trying to send the error response.
|
||||
*/
|
||||
private static void handleError(HttpServletResponse resp, int errorCode)
|
||||
throws IOException {
|
||||
ProxyResponse proxyResponse = new ProxyResponse();
|
||||
proxyResponse.setCode(errorCode);
|
||||
proxyResponse.setExecutorResponse(
|
||||
HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(errorCode));
|
||||
HandlerUtil.handleError(resp, proxyResponse);
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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.carbon.webapp.authenticator.framework.authenticator;
|
||||
|
||||
import org.apache.catalina.connector.Response;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.otp.mgt.dto.OneTimePinDTO;
|
||||
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.AuthenticationInfo;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.Constants;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.Utils.Utils;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.internal.AuthenticatorFrameworkDataHolder;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class OneTimeTokenAuthenticator implements WebappAuthenticator {
|
||||
private static final Log log = LogFactory.getLog(OneTimeTokenAuthenticator.class);
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
public boolean canHandle(org.apache.catalina.connector.Request request) {
|
||||
return request.getHeader(Constants.HTTPHeaders.ONE_TIME_TOKEN_HEADER) != null;
|
||||
}
|
||||
|
||||
public AuthenticationInfo authenticate(org.apache.catalina.connector.Request request, Response response) {
|
||||
|
||||
AuthenticationInfo authenticationInfo = new AuthenticationInfo();
|
||||
|
||||
try {
|
||||
OTPManagementService otpManagementService = AuthenticatorFrameworkDataHolder.getInstance()
|
||||
.getOtpManagementService();
|
||||
OneTimePinDTO validOTP = otpManagementService.isValidOTP(request.getHeader(Constants.HTTPHeaders
|
||||
.ONE_TIME_TOKEN_HEADER));
|
||||
if (validOTP != null) {
|
||||
authenticationInfo.setStatus(Status.CONTINUE);
|
||||
authenticationInfo.setTenantId(validOTP.getTenantId());
|
||||
authenticationInfo.setTenantDomain(Utils.getTenantDomain(validOTP.getTenantId()));
|
||||
authenticationInfo.setUsername(validOTP.getUsername());
|
||||
} else {
|
||||
authenticationInfo.setStatus(Status.FAILURE);
|
||||
authenticationInfo.setMessage("Invalid OTP token.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String msg = "OTP Token Validation Failed.";
|
||||
log.error(msg, e);
|
||||
authenticationInfo.setStatus(Status.FAILURE);
|
||||
authenticationInfo.setMessage(msg);
|
||||
}
|
||||
return authenticationInfo;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "One-Time-Token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Properties getProperties() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
#*
|
||||
Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
|
||||
Entgra (Pvt) Ltd. licenses this file to you 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.
|
||||
*#
|
||||
<EmailConfig>
|
||||
<Subject>Verify email to register with Entgra IoTS</Subject>
|
||||
<Body>
|
||||
<![CDATA[
|
||||
<html>
|
||||
<head>
|
||||
<title>Entgra IoT Server</title>
|
||||
</head>
|
||||
<body style="color: #666666; background-color:#cdcdcd; padding: 0px; margin: 0px;">
|
||||
<div style="background-color:#cdcdcd; font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; padding: 20px 0px; margin: 0px;">
|
||||
<div style="width: 86%; max-width: 650px; padding: 2%; background-color: #ffffff; margin: auto; border-radius: 14px;">
|
||||
<div style="background-color: #ffebcc; line-height: 0px; border-top-left-radius: 10px; border-top-right-radius: 10px; padding: 10px;">
|
||||
<div style="display: inline-block; line-height: 0px;">
|
||||
<img alt="entgra" src="https://storage.googleapis.com/cdn-entgra/logo.png" height="50px" width="143px" />
|
||||
</div>
|
||||
</div>
|
||||
<div style="background-color: #ffffff; line-height: 170%; color: #666666; padding: 20px 25px;">
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px 20px;">
|
||||
Hi $first-name,
|
||||
</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;">
|
||||
Congratulations!!! Thank you for registering with Entgra cloud. Please click and log in to the
|
||||
following link to complete your registration with us. Click <a href="$base-url-https/self-register/sign-up?token=$otp-token">here</a>.
|
||||
</p>
|
||||
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;">
|
||||
If you need further assistance, please contact your administrator.
|
||||
</p>
|
||||
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
Regards,
|
||||
</p>
|
||||
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;">
|
||||
Entgra IoT Administrator
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
]]>
|
||||
</Body>
|
||||
</EmailConfig>
|
@ -0,0 +1,98 @@
|
||||
#*
|
||||
Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
|
||||
Entgra (Pvt) Ltd. licenses this file to you 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.
|
||||
*#
|
||||
<EmailConfig>
|
||||
<Subject>Welcome to Entgra Evaluation Cloud</Subject>
|
||||
<Body>
|
||||
<![CDATA[
|
||||
<html>
|
||||
<head>
|
||||
<title>Entgra IoT Server</title>
|
||||
</head>
|
||||
<body style="color: #666666; background-color:#cdcdcd; padding: 0px; margin: 0px;">
|
||||
<div style="background-color:#cdcdcd; font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; padding: 20px 0px; margin: 0px;">
|
||||
<div style="width: 86%; max-width: 650px; padding: 2%; background-color: #ffffff; margin: auto; border-radius: 14px;">
|
||||
<div style="background-color: #ffebcc; line-height: 0px; border-top-left-radius: 10px; border-top-right-radius: 10px; padding: 10px;">
|
||||
<div style="display: inline-block; line-height: 0px;">
|
||||
<img alt="entgra" src="https://storage.googleapis.com/cdn-entgra/logo.png" height="50px" width="143px" />
|
||||
</div>
|
||||
</div>
|
||||
<div style="background-color: #ffffff; line-height: 170%; color: #666666; padding: 20px 25px;">
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px 20px;">
|
||||
Hi $first-name,
|
||||
</p>
|
||||
<div>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
Welcome to Entgra Evaluation Cloud!! Entgra server support offers managing Android, iOS and Windows
|
||||
devices along with a wide range of features that support cooperate (COPE) or personal device (BYOD)
|
||||
enrollments.
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>Access different portals</h3>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;">Your log-in credentials to any of our portals(endpoint-mgt, store, publisher) are,</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;"><b>Username: </b> $portal-username</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;"><b>Password: </b> password provided at registration for an evaluation account.</p>
|
||||
<h4>Endpoint management portal URL: <a href="$base-url-https/endpoint-mgt/">$base-url-https/endpoint-mgt/</a></h4>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
This is the portal used to send operations and policies to devices and overall management of
|
||||
the server.
|
||||
</p>
|
||||
<h4>Application store portal URL: <a href="$base-url-https/store">$base-url-https/store</a></h4>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
This is an in-house corporate app store where you can host all your corporate applications.
|
||||
Users may browse apps and install them to their devices if the administrator has made the apps
|
||||
publicly visible to users. Administrator can install, uninstall and update apps in the user
|
||||
device or device groups.
|
||||
</p>
|
||||
<h4>Application publishing portal URL: <a href="$base-url-https/publisher">$base-url-https/publisher</a></h4>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
The portal for publishing new applications for internal use. This is the developer view of the
|
||||
enterprise application store that comes with the product.
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>Enroll a device</h3>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
Please find here a set of videos on how to enroll and onboard devices to our Cloud platform.
|
||||
<a href="https://entgra-documentation.gitlab.io/v4.0.0/docs/product-guide/enrollment-guide/enroll-cloud/">[Cloud Enrollment Guide]</a>
|
||||
When enrolling a device, Make sure to use the following log-in format:
|
||||
</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;"><b>Organisation:</b> $tenant-domain</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;"><b>Username:</b> $agent-username</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;"><b>Password:</b> password provided at registration.</p>
|
||||
</div>
|
||||
<div>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
If you have any further questions, please reach out to us using your registered mail to
|
||||
<a href="bizdev-group@entgra.io"> bizdev-group@entgra.io.</a> Looking forward to working with you.
|
||||
</p>
|
||||
<p style="font-length: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 20px 0px 5px;">
|
||||
Best Regards,
|
||||
</p>
|
||||
<p style="font-size: 1em; font-family: Arial, Helvetica; line-height: 170%; color: #666666; margin: 5px 0px;">
|
||||
Entgra Cloud Team
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
]]>
|
||||
</Body>
|
||||
</EmailConfig>
|
Loading…
Reference in new issue