mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Improve OTP creating functionality
This commit is contained in:
parent
13b4663e7d
commit
efee2b01bf
@ -1221,50 +1221,4 @@ public interface UserManagementService {
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getPermissionsOfUser();
|
||||
|
||||
@POST
|
||||
@Path("/one-time-pin")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "Getting the permission details of the current user",
|
||||
notes = "A user may granted more than one permission in IoTS. Using this REST API "
|
||||
+ "you can get the permission/permission the current user has granted. ",
|
||||
tags = "User Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:user:permission-view")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully fetched the list of permissions the user "
|
||||
+ "has granted.",
|
||||
response = PermissionList.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 404,
|
||||
message = "Not Found. \n The specified resource does not exist.\n",
|
||||
response = ErrorResponse.class),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. \n Server error occurred while fetching the "
|
||||
+ "list of roles assigned to the specified user.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response sendEmailVerifyingMail(OTPMailWrapper otpMailWrapper);
|
||||
}
|
||||
|
||||
@ -1118,53 +1118,6 @@ public class UserManagementServiceImpl implements UserManagementService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used to send an invitation email to a existing user to enroll a device.
|
||||
*
|
||||
* @param otpMailWrapper Username list of the users to be invited
|
||||
*/
|
||||
@POST
|
||||
@Path("/one-time-pin")
|
||||
@Produces({MediaType.APPLICATION_JSON})
|
||||
public Response sendEmailVerifyingMail(OTPMailWrapper otpMailWrapper) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending enrollment invitation mail to existing user.");
|
||||
}
|
||||
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
|
||||
OTPManagementService oms = DeviceMgtAPIUtils.getOTPManagementService();
|
||||
try {
|
||||
String otpToken = oms.createOTPToken(otpMailWrapper);
|
||||
Properties props = new Properties();
|
||||
props.setProperty("first-name", otpMailWrapper.getFirstName());
|
||||
props.setProperty("otp-token", otpToken);
|
||||
|
||||
EmailMetaInfo metaInfo = new EmailMetaInfo(otpMailWrapper.getEmail(), props);
|
||||
dms.sendEnrolmentInvitation(DeviceManagementConstants.EmailAttributes.USER_VERIFY_TEMPLATE,
|
||||
metaInfo);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while inviting user to enrol their device";
|
||||
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
|
||||
msg = e.getMessage();
|
||||
}
|
||||
log.error(msg, e);
|
||||
return Response.serverError().entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||
} catch (ConfigurationManagementException e) {
|
||||
String msg = "Error occurred while sending the email invitations. Mail server not configured.";
|
||||
return Response.serverError().entity(
|
||||
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
||||
} catch (OTPManagementException e) {
|
||||
String msg = "Error occurred while generating and storing the OTP data";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (org.wso2.carbon.device.mgt.common.exceptions.BadRequestException e) {
|
||||
String msg = "Bad Request : Found invalid request payload to create OTP toke.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
}
|
||||
return Response.status(Response.Status.OK).entity("Invitation mails have been sent.").build();
|
||||
}
|
||||
|
||||
private Map<String, String> buildDefaultUserClaims(String firstName, String lastName, String emailAddress,
|
||||
boolean isFresh) {
|
||||
Map<String, String> defaultUserClaims = new HashMap<>();
|
||||
|
||||
@ -142,7 +142,6 @@ public class DeviceMgtAPIUtils {
|
||||
|
||||
private static IntegrationClientService integrationClientService;
|
||||
private static MetadataManagementService metadataManagementService;
|
||||
private static volatile OTPManagementService otpManagementService;
|
||||
|
||||
static {
|
||||
String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password");
|
||||
@ -461,29 +460,6 @@ public class DeviceMgtAPIUtils {
|
||||
return metadataManagementService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializing and accessing method for OTPManagementService.
|
||||
*
|
||||
* @return OTPManagementService instance
|
||||
* @throws IllegalStateException if OTPManagementService cannot be initialized
|
||||
*/
|
||||
public static OTPManagementService getOTPManagementService() {
|
||||
if (otpManagementService == null) {
|
||||
synchronized (DeviceMgtAPIUtils.class) {
|
||||
if (otpManagementService == null) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
otpManagementService = (OTPManagementService) ctx.getOSGiService(OTPManagementService.class, null);
|
||||
if (otpManagementService == null) {
|
||||
String msg = "OTP Management service not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return otpManagementService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for initializing ReportManagementService
|
||||
* @return ReportManagementServie Instance
|
||||
|
||||
@ -48,8 +48,7 @@
|
||||
<context-param>
|
||||
<param-name>nonSecuredEndPoints</param-name>
|
||||
<param-value>
|
||||
/api/device-mgt/v1.0/users/validate,
|
||||
/api/device-mgt/v1.0/users/one-time-pin
|
||||
/api/device-mgt/v1.0/users/validate
|
||||
</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
@ -94,6 +94,7 @@
|
||||
org.wso2.carbon.certificate.mgt.core.*,
|
||||
org.wso2.carbon.device.mgt.core.permission.mgt,
|
||||
org.wso2.carbon.device.mgt.common,
|
||||
org.wso2.carbon.device.mgt.core.*,
|
||||
org.wso2.carbon.device.mgt.common.permission.mgt,
|
||||
org.apache.axis2,
|
||||
org.apache.axis2.client,
|
||||
|
||||
@ -42,11 +42,11 @@ public class OneTimeTokenAuthenticator implements WebappAuthenticator {
|
||||
|
||||
public AuthenticationInfo authenticate(org.apache.catalina.connector.Request request, Response response) {
|
||||
|
||||
OTPManagementService otpManagementService = AuthenticatorFrameworkDataHolder.getInstance()
|
||||
.getOtpManagementService();
|
||||
AuthenticationInfo authenticationInfo = new AuthenticationInfo();
|
||||
|
||||
try {
|
||||
OTPManagementService otpManagementService = AuthenticatorFrameworkDataHolder.getInstance()
|
||||
.getOtpManagementService();
|
||||
if (otpManagementService.isValidOTP(request.getHeader(Constants.HTTPHeaders.ONE_TIME_TOKEN_HEADER))) {
|
||||
authenticationInfo.setStatus(Status.CONTINUE);
|
||||
authenticationInfo.setTenantId(-1);
|
||||
@ -55,8 +55,10 @@ public class OneTimeTokenAuthenticator implements WebappAuthenticator {
|
||||
authenticationInfo.setMessage("Invalid OTP token.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String msg = "OTP Token Validation Failed.";
|
||||
log.error(msg, e);
|
||||
authenticationInfo.setStatus(Status.FAILURE);
|
||||
authenticationInfo.setMessage("CToken Validation Failed.");
|
||||
authenticationInfo.setMessage(msg);
|
||||
}
|
||||
return authenticationInfo;
|
||||
}
|
||||
|
||||
@ -24,5 +24,9 @@
|
||||
<Name>CertificateAuth</Name>
|
||||
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.CertificateAuthenticator</ClassName>
|
||||
</Authenticator>
|
||||
<Authenticator>
|
||||
<Name>OTPAuth</Name>
|
||||
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.OneTimeTokenAuthenticator</ClassName>
|
||||
</Authenticator>
|
||||
</Authenticators>
|
||||
</WebappAuthenticatorConfig>
|
||||
|
||||
@ -31,6 +31,10 @@
|
||||
<Name>CertificateAuth</Name>
|
||||
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.CertificateAuthenticator</ClassName>
|
||||
</Authenticator>
|
||||
<Authenticator>
|
||||
<Name>OTPAuth</Name>
|
||||
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.OneTimeTokenAuthenticator</ClassName>
|
||||
</Authenticator>
|
||||
<Authenticator>
|
||||
<Name>BST</Name>
|
||||
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.BSTAuthenticator</ClassName>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user