forked from community/device-mgt-core
Merge branch 'tenant-improve' of https://gitlab.com/entgra/carbon-device-mgt into tenant-improve
commit
640a50ccd9
@ -0,0 +1 @@
|
|||||||
|
registry=http://nexus.entgra.io/repository/npm-group/
|
@ -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,104 @@
|
|||||||
|
/* 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 OTPMailDTO {
|
||||||
|
|
||||||
|
int id;
|
||||||
|
String otpToken;
|
||||||
|
String tenantDomain;
|
||||||
|
String email;
|
||||||
|
String emailType;
|
||||||
|
String metaInfo;
|
||||||
|
Timestamp createdAt;
|
||||||
|
int expiryTime;
|
||||||
|
boolean isExpired;
|
||||||
|
boolean isTenantCreated;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTenantDomain() { return tenantDomain; }
|
||||||
|
|
||||||
|
public void setTenantDomain(String tenantDomain) { this.tenantDomain = tenantDomain; }
|
||||||
|
|
||||||
|
public boolean isTenantCreated() { return isTenantCreated; }
|
||||||
|
|
||||||
|
public void setTenantCreated(boolean tenantCreated) { isTenantCreated = tenantCreated; }
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
/* 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;
|
||||||
|
|
||||||
|
public class OTPMailWrapper {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String tenantDomain;
|
||||||
|
private String adminUsername;
|
||||||
|
private String adminPassword;
|
||||||
|
private String email;
|
||||||
|
private String emailType;
|
||||||
|
|
||||||
|
public String getTenantDomain() {
|
||||||
|
return tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantDomain(String tenantDomain) {
|
||||||
|
this.tenantDomain = tenantDomain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAdminUsername() {
|
||||||
|
return adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminUsername(String adminUsername) {
|
||||||
|
this.adminUsername = adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAdminPassword() {
|
||||||
|
return adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminPassword(String adminPassword) {
|
||||||
|
this.adminPassword = adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
/* 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.roles.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "DefaultRoles")
|
||||||
|
public class DefaultRoles {
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
private List<Role> roles;
|
||||||
|
|
||||||
|
@XmlElement(name = "Enabled", required = true)
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElementWrapper(name = "Roles", required = true)
|
||||||
|
@XmlElement(name = "Role", required = true)
|
||||||
|
public List<Role> getRoles() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoles(List<Role> roles) {
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
/* 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.roles.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "Role")
|
||||||
|
public class Role {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private List<String> permissions;
|
||||||
|
|
||||||
|
@XmlElement(name = "Name", required = true)
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElementWrapper(name = "Permissions", required = true)
|
||||||
|
@XmlElement(name = "Permission", required = true)
|
||||||
|
public List<String> getPermissions() {
|
||||||
|
return permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissions(List<String> permissions) {
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/* 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.OTPManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPMailWrapper;
|
||||||
|
|
||||||
|
public interface OTPManagementService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cretae OTP token and store tenant details in the DB
|
||||||
|
* @param otpMailWrapper OTP Mail Wrapper object which contains tenant details of registering user
|
||||||
|
* @return OTPToken
|
||||||
|
* @throws OTPManagementException if error occurs while creating OTP token and storing tenant details.
|
||||||
|
* @throws BadRequestException if found and incompatible payload to create OTP token.
|
||||||
|
*/
|
||||||
|
String createOTPToken (OTPMailWrapper otpMailWrapper) throws OTPManagementException, BadRequestException;
|
||||||
|
}
|
@ -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,32 @@
|
|||||||
|
/* 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.OTPMailDTO;
|
||||||
|
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 otpMailDTO OTPMailDTO
|
||||||
|
* @return Primary key of the newly adding data raw
|
||||||
|
* @throws OTPManagementDAOException if error occurred whule storing data
|
||||||
|
*/
|
||||||
|
int addOTPData(OTPMailDTO otpMailDTO) 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,85 @@
|
|||||||
|
/* 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.OTPMailDTO;
|
||||||
|
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(OTPMailDTO otpMailDTO) 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 : " + otpMailDTO.getOtpToken() + " Email : " + otpMailDTO.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
String sql = "INSERT INTO DM_OTP_DATA "
|
||||||
|
+ "(OTP_TOKEN, "
|
||||||
|
+ "TENANT_DOMAIN,"
|
||||||
|
+ "EMAIL, "
|
||||||
|
+ "EMAIL_TYPE, "
|
||||||
|
+ "META_INFO, "
|
||||||
|
+ "CREATED_AT) 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, otpMailDTO.getOtpToken());
|
||||||
|
stmt.setString(2, otpMailDTO.getTenantDomain());
|
||||||
|
stmt.setString(3, otpMailDTO.getEmail());
|
||||||
|
stmt.setString(4, otpMailDTO.getEmailType());
|
||||||
|
stmt.setString(5, otpMailDTO.getMetaInfo());
|
||||||
|
stmt.setTimestamp(6, timestamp);
|
||||||
|
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 "
|
||||||
|
+ otpMailDTO.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 " + otpMailDTO.getEmail();
|
||||||
|
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,132 @@
|
|||||||
|
/* 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.device.mgt.common.exceptions.BadRequestException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.exceptions.DBConnectionException;
|
||||||
|
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.otp.mgt.dto.OTPMailDTO;
|
||||||
|
import org.wso2.carbon.device.mgt.common.spi.OTPManagementService;
|
||||||
|
import org.wso2.carbon.device.mgt.core.otp.mgt.dao.OTPManagementDAO;
|
||||||
|
import org.wso2.carbon.device.mgt.common.otp.mgt.wrapper.OTPMailWrapper;
|
||||||
|
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 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 String createOTPToken(OTPMailWrapper otpMailWrapper) throws OTPManagementException, BadRequestException {
|
||||||
|
|
||||||
|
if (!isValidOTPTokenCreatingRequest(otpMailWrapper)){
|
||||||
|
String msg = "Found invalid payload with OTP creating request";
|
||||||
|
log.error(msg);
|
||||||
|
throw new BadRequestException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String metaInfo = gson.toJson(otpMailWrapper);
|
||||||
|
String otpValue = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
OTPMailDTO otpMailDTO = new OTPMailDTO();
|
||||||
|
otpMailDTO.setEmail(otpMailWrapper.getEmail());
|
||||||
|
otpMailDTO.setTenantDomain(otpMailWrapper.getTenantDomain());
|
||||||
|
otpMailDTO.setEmailType(otpMailWrapper.getEmailType());
|
||||||
|
otpMailDTO.setMetaInfo(metaInfo);
|
||||||
|
otpMailDTO.setOtpToken(otpValue);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ConnectionManagerUtil.beginDBTransaction();
|
||||||
|
if (this.otpManagementDAO.addOTPData(otpMailDTO) == -1) {
|
||||||
|
ConnectionManagerUtil.rollbackDBTransaction();
|
||||||
|
String msg = "OTP data saving failed. Please, contact Administrator";
|
||||||
|
log.error(msg);
|
||||||
|
throw new OTPManagementException(msg);
|
||||||
|
}
|
||||||
|
ConnectionManagerUtil.commitDBTransaction();
|
||||||
|
return otpValue;
|
||||||
|
} 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: " + otpMailDTO.getEmail();
|
||||||
|
log.error(msg, e);
|
||||||
|
throw new OTPManagementException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate OTP token creating payload
|
||||||
|
* @param otpMailWrapper OTPMailWrapper
|
||||||
|
* @return true if its valid payload otherwise returns false
|
||||||
|
*/
|
||||||
|
private boolean isValidOTPTokenCreatingRequest(OTPMailWrapper otpMailWrapper) {
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getFirstName())) {
|
||||||
|
log.error("Received empty or blank first name field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getLastName())) {
|
||||||
|
log.error("Received empty or blank last name field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getAdminUsername())) {
|
||||||
|
log.error("Received empty or blank admin username field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getAdminPassword())) {
|
||||||
|
log.error("Received empty or blank admin password field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getEmail())) {
|
||||||
|
log.error("Received empty or blank email field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getEmailType())) {
|
||||||
|
log.error("Received empty or blank email type field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(otpMailWrapper.getTenantDomain())) {
|
||||||
|
log.error("Received empty or blank tenant domain field with OTP creating payload.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -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,148 @@
|
|||||||
|
/* 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.util;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.common.roles.config.Role;
|
||||||
|
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.internal.DeviceManagementDataHolder;
|
||||||
|
import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils;
|
||||||
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||||
|
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
|
||||||
|
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
|
||||||
|
import org.wso2.carbon.user.api.Permission;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreException;
|
||||||
|
import org.wso2.carbon.user.api.UserStoreManager;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DeviceMgtTenantMgtListener implements TenantMgtListener {
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceMgtTenantMgtListener.class);
|
||||||
|
private static final int EXEC_ORDER = 10;
|
||||||
|
private static final String PERMISSION_ACTION = "ui.execute";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantCreate(TenantInfoBean tenantInfoBean) {
|
||||||
|
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
|
||||||
|
if (config.getDefaultRoles().isEnabled()) {
|
||||||
|
Map<String, List<Permission>> roleMap = getValidRoleMap(config);
|
||||||
|
try {
|
||||||
|
PrivilegedCarbonContext.startTenantFlow();
|
||||||
|
PrivilegedCarbonContext.getThreadLocalCarbonContext()
|
||||||
|
.setTenantDomain(tenantInfoBean.getTenantDomain(), true);
|
||||||
|
UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService()
|
||||||
|
.getTenantUserRealm(tenantInfoBean.getTenantId()).getUserStoreManager();
|
||||||
|
|
||||||
|
roleMap.forEach((key, value) -> {
|
||||||
|
try {
|
||||||
|
userStoreManager.addRole(key, null, value.toArray(new Permission[0]));
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
log.error("Error occurred while adding default roles into user store.", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (UserStoreException e) {
|
||||||
|
log.error("Error occurred while getting user store manager.", e);
|
||||||
|
} finally {
|
||||||
|
PrivilegedCarbonContext.endTenantFlow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantUpdate(TenantInfoBean tenantInfoBean) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantDelete(int i) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantRename(int i, String s, String s1) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantInitialActivation(int i) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantActivation(int i) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTenantDeactivation(int i) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSubscriptionPlanChange(int i, String s, String s1) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getListenerOrder() {
|
||||||
|
return EXEC_ORDER;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPreDelete(int i) {
|
||||||
|
// doing nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use the default roles defined in the cdm-config and evaluate the defined permissions. If permissions does not
|
||||||
|
* exist then exclude them and return role map which contains defined roles in the cdm-config and existing
|
||||||
|
* permission list as a roleMap
|
||||||
|
* @param config cdm-config
|
||||||
|
* @return {@link Map} key is role name and value is list of permissions which needs to be assigned to the role
|
||||||
|
* defined in the key.
|
||||||
|
*/
|
||||||
|
private Map<String, List<Permission>> getValidRoleMap(DeviceManagementConfig config) {
|
||||||
|
Map<String, List<Permission>> roleMap = new HashMap<>();
|
||||||
|
try {
|
||||||
|
for (Role role : config.getDefaultRoles().getRoles()) {
|
||||||
|
List<Permission> permissionList = new ArrayList<>();
|
||||||
|
for (String permissionPath : role.getPermissions()) {
|
||||||
|
if (PermissionUtils.checkResourceExists(permissionPath)) {
|
||||||
|
Permission permission = new Permission(permissionPath, PERMISSION_ACTION);
|
||||||
|
|
||||||
|
permissionList.add(permission);
|
||||||
|
} else {
|
||||||
|
log.warn("Permission " + permissionPath + " does not exist. Hence it will not add to role "
|
||||||
|
+ role.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
roleMap.put(role.getName(), permissionList);
|
||||||
|
}
|
||||||
|
} catch (PermissionManagementException | RegistryException e) {
|
||||||
|
log.error("Error occurred while checking permission existence.", e);
|
||||||
|
}
|
||||||
|
return roleMap;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,134 @@
|
|||||||
|
/* 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 com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import io.entgra.ui.request.interceptor.beans.AuthData;
|
||||||
|
import io.entgra.ui.request.interceptor.util.HandlerConstants;
|
||||||
|
import io.entgra.ui.request.interceptor.util.HandlerUtil;
|
||||||
|
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.HttpGet;
|
||||||
|
import org.apache.http.entity.ContentType;
|
||||||
|
import org.wso2.carbon.device.application.mgt.common.ProxyResponse;
|
||||||
|
|
||||||
|
import javax.servlet.annotation.MultipartConfig;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@MultipartConfig
|
||||||
|
@WebServlet("/default-credentials")
|
||||||
|
public class DefaultTokenHandler extends HttpServlet {
|
||||||
|
private static final Log log = LogFactory.getLog(DefaultTokenHandler.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
try {
|
||||||
|
HttpSession httpSession = req.getSession(false);
|
||||||
|
|
||||||
|
if (httpSession != null) {
|
||||||
|
AuthData authData = (AuthData) httpSession.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
|
||||||
|
if (authData == null) {
|
||||||
|
HandlerUtil.sendUnAuthorizeResponse(resp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AuthData defaultAuthData = (AuthData) httpSession
|
||||||
|
.getAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY);
|
||||||
|
if (defaultAuthData != null) {
|
||||||
|
HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultAuthData.getAccessToken()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String clientId = authData.getClientId();
|
||||||
|
String clientSecret = authData.getClientSecret();
|
||||||
|
|
||||||
|
String iotsCorePort = System.getProperty("iot.core.https.port");
|
||||||
|
if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) {
|
||||||
|
iotsCorePort = System.getProperty("iot.core.http.port");
|
||||||
|
}
|
||||||
|
|
||||||
|
String tokenUrl =
|
||||||
|
req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host")
|
||||||
|
+ HandlerConstants.COLON + iotsCorePort + "/api/device-mgt/v1.0/devices/" + clientId
|
||||||
|
+ "/" + clientSecret + "/default-token";
|
||||||
|
|
||||||
|
HttpGet defaultTokenRequest = new HttpGet(tokenUrl);
|
||||||
|
defaultTokenRequest
|
||||||
|
.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken());
|
||||||
|
defaultTokenRequest
|
||||||
|
.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString());
|
||||||
|
ProxyResponse tokenResultResponse = HandlerUtil.execute(defaultTokenRequest);
|
||||||
|
|
||||||
|
if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
|
||||||
|
log.error("Error occurred while invoking the API to get default token data.");
|
||||||
|
HandlerUtil.handleError(resp, tokenResultResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String tokenResult = tokenResultResponse.getData();
|
||||||
|
if (tokenResult == null) {
|
||||||
|
log.error("Invalid default token response is received.");
|
||||||
|
HandlerUtil.handleError(resp, tokenResultResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonParser jsonParser = new JsonParser();
|
||||||
|
JsonElement jTokenResult = jsonParser.parse(tokenResult);
|
||||||
|
if (jTokenResult.isJsonObject()) {
|
||||||
|
JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject();
|
||||||
|
AuthData newDefaultAuthData = new AuthData();
|
||||||
|
newDefaultAuthData.setClientId(clientId);
|
||||||
|
newDefaultAuthData.setClientSecret(clientSecret);
|
||||||
|
|
||||||
|
String defaultToken = jTokenResultAsJsonObject.get("accessToken").getAsString();
|
||||||
|
newDefaultAuthData.setAccessToken(defaultToken);
|
||||||
|
newDefaultAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refreshToken").getAsString());
|
||||||
|
newDefaultAuthData.setScope(jTokenResultAsJsonObject.get("scopes").getAsString());
|
||||||
|
httpSession.setAttribute(HandlerConstants.SESSION_DEFAULT_AUTH_DATA_KEY, newDefaultAuthData);
|
||||||
|
|
||||||
|
HandlerUtil.handleSuccess(resp, constructSuccessProxyResponse(defaultToken));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
HandlerUtil.sendUnAuthorizeResponse(resp);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error occurred when processing GET request to get default token.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Success Proxy Response
|
||||||
|
* @param responseString Response String
|
||||||
|
* @return {@link ProxyResponse}
|
||||||
|
*/
|
||||||
|
private ProxyResponse constructSuccessProxyResponse (String responseString) {
|
||||||
|
ProxyResponse proxyResponse = new ProxyResponse();
|
||||||
|
proxyResponse.setCode(HttpStatus.SC_OK);
|
||||||
|
proxyResponse.setData(responseString);
|
||||||
|
return proxyResponse;
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue