Separated the Android Sense services and api secured

Menaka Madushanka 9 years ago
parent 2f7a480864
commit cd9abee0eb

@ -19,14 +19,14 @@
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<!--<context-param>--> <context-param>
<!--<param-name>isAdminService</param-name>--> <param-name>isAdminService</param-name>
<!--<param-value>false</param-value>--> <param-value>false</param-value>
<!--</context-param>--> </context-param>
<!--<context-param>--> <context-param>
<!--<param-name>doAuthentication</param-name>--> <param-name>doAuthentication</param-name>
<!--<param-value>false</param-value>--> <param-value>false</param-value>
<!--</context-param>--> </context-param>
<!--publish to apim--> <!--publish to apim-->
<context-param> <context-param>
@ -38,20 +38,12 @@
<param-value>admin</param-value> <param-value>admin</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>managed-api-name</param-name> <param-name>managed-api-context-template</param-name>
<param-value>android_sense</param-value> <param-value>/android_sense/{version}</param-value>
</context-param>
<context-param>
<param-name>managed-api-endpoint</param-name>
<param-value>http://localhost:9763/android_sense</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>managed-api-version</param-name> <param-name>managed-api-application</param-name>
<param-value>1.0.0</param-value> <param-value>android_sense</param-value>
</context-param>
<context-param>
<param-name>managed-api-context</param-name>
<param-value>/android_sense</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>managed-api-isSecured</param-name> <param-name>managed-api-isSecured</param-name>

@ -20,6 +20,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.API;
import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil; import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -29,6 +31,9 @@ import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSense
import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo; import org.wso2.carbon.device.mgt.iot.apimgt.AccessTokenInfo;
import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient; import org.wso2.carbon.device.mgt.iot.apimgt.TokenClient;
import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException; import org.wso2.carbon.device.mgt.iot.exception.AccessTokenException;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -38,7 +43,7 @@ import javax.ws.rs.core.Response;
import java.io.File; import java.io.File;
import java.util.Date; import java.util.Date;
@API( name="android_sense", version="1.0.0", context="/android_sense") @API( name="android_sense_mgt", version="1.0.0", context="/android_sense_mgt")
public class AndroidSenseService { public class AndroidSenseService {
private static Log log = LogFactory.getLog(AndroidSenseService.class); private static Log log = LogFactory.getLog(AndroidSenseService.class);
@ -48,34 +53,62 @@ public class AndroidSenseService {
@Context //injected response proxy supporting multiple thread @Context //injected response proxy supporting multiple thread
private HttpServletResponse response; private HttpServletResponse response;
private PrivilegedCarbonContext ctx;
private RealmService getRealmServiceProvider() {
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
PrivilegedCarbonContext.startTenantFlow();
ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(tenantDomain, true);
if (log.isDebugEnabled()) {
log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain);
}
return (RealmService) ctx.getOSGiService(RealmService.class, null);
}
@Path("manager/device") @Path("manager/device")
@PUT @PUT
public boolean register(@FormParam("deviceId") String deviceId, public boolean register(@FormParam("username") String username,
@FormParam("password") String password,
@FormParam("deviceId") String deviceId,
@FormParam("owner") String owner) { @FormParam("owner") String owner) {
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
RealmService realmService = getRealmServiceProvider();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
UserStoreManager userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
log.debug(userStoreManager.authenticate(username, password)+"");
if (!userStoreManager.authenticate(username, password)){
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
return false;
}
} catch (UserStoreException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false;
}
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId); deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
try { try {
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
response.setStatus(Response.Status.CONFLICT.getStatusCode()); response.setStatus(Response.Status.CONFLICT.getStatusCode());
getTokens(owner, deviceId, response);
return true; return true;
} }
KeyGenerationUtil.createApplicationKeys("android_sense");
getTokens(owner, deviceId, response);
Device device = new Device(); Device device = new Device();
device.setDeviceIdentifier(deviceId); device.setDeviceIdentifier(deviceId);
EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
// KeyGenerationUtil.createApplicationKeys("android_sense");
//
// TokenClient accessTokenClient = new TokenClient(AndroidSenseConstants.DEVICE_TYPE);
// AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
//
// //create token
// String accessToken = accessTokenInfo.getAccess_token();
// String refreshToken = accessTokenInfo.getRefresh_token();
enrolmentInfo.setDateOfEnrolment(new Date().getTime()); enrolmentInfo.setDateOfEnrolment(new Date().getTime());
enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
@ -90,8 +123,6 @@ public class AndroidSenseService {
if (added) { if (added) {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
// response.addHeader("access", accessToken);
// response.addHeader("refresh", refreshToken);
} else { } else {
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
@ -100,7 +131,7 @@ public class AndroidSenseService {
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
} catch (Exception e) { } catch (AccessTokenException e) {
e.printStackTrace(); e.printStackTrace();
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return false; return false;
@ -220,4 +251,16 @@ public class AndroidSenseService {
} }
public void getTokens(String owner, String deviceId, HttpServletResponse response)
throws AccessTokenException {
TokenClient accessTokenClient = new TokenClient(AndroidSenseConstants.DEVICE_TYPE);
AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
String accessToken = accessTokenInfo.getAccess_token();
String refreshToken = accessTokenInfo.getRefresh_token();
response.addHeader("access", accessToken);
response.addHeader("refresh", refreshToken);
}
} }

@ -29,7 +29,7 @@
<cxf:logging/> <cxf:logging/>
</jaxrs:features> </jaxrs:features>
<jaxrs:serviceBeans> <jaxrs:serviceBeans>
<bean id="AndroidSenseMgtService" <bean id="AndroidSenseService"
class="org.wso2.carbon.device.mgt.iot.androidsense.mgt.service.impl.AndroidSenseService"/> class="org.wso2.carbon.device.mgt.iot.androidsense.mgt.service.impl.AndroidSenseService"/>
</jaxrs:serviceBeans> </jaxrs:serviceBeans>
<jaxrs:providers> <jaxrs:providers>

@ -19,14 +19,14 @@
<url-pattern>/*</url-pattern> <url-pattern>/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<!--<context-param>--> <context-param>
<!--<param-name>isAdminService</param-name>--> <param-name>isAdminService</param-name>
<!--<param-value>false</param-value>--> <param-value>false</param-value>
<!--</context-param>--> </context-param>
<!--<context-param>--> <context-param>
<!--<param-name>doAuthentication</param-name>--> <param-name>doAuthentication</param-name>
<!--<param-value>false</param-value>--> <param-value>false</param-value>
<!--</context-param>--> </context-param>
<!--publish to apim--> <!--publish to apim-->
<context-param> <context-param>
@ -38,24 +38,16 @@
<param-value>admin</param-value> <param-value>admin</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name>managed-api-name</param-name> <param-name>managed-api-context-template</param-name>
<param-value>android_sense</param-value> <param-value>/android_sense_mgt/{version}</param-value>
</context-param>
<context-param>
<param-name>managed-api-endpoint</param-name>
<param-value>https://localhost:9443/android_sense</param-value>
</context-param>
<context-param>
<param-name>managed-api-version</param-name>
<param-value>1.0.0</param-value>
</context-param>
<context-param>
<param-name>managed-api-context</param-name>
<param-value>/android_sense</param-value>
</context-param> </context-param>
<context-param>
<param-name>managed-api-application</param-name>
<param-value>android_sense</param-value>
</context-param>
<context-param> <context-param>
<param-name>managed-api-isSecured</param-name> <param-name>managed-api-isSecured</param-name>
<param-value>true</param-value> <param-value>false</param-value>
</context-param> </context-param>
<!-- Below configuration is used to redirect http requests to https --> <!-- Below configuration is used to redirect http requests to https -->

@ -44,7 +44,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.api</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.androidsense.mgt.api</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.controller.api</artifactId>
<type>war</type> <type>war</type>
</dependency> </dependency>
<dependency> <dependency>
@ -94,7 +99,7 @@
<artifactItems> <artifactItems>
<artifactItem> <artifactItem>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.api</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.androidsense.mgt.api</artifactId>
<type>war</type> <type>war</type>
<overWrite>true</overWrite> <overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources/webapps/</outputDirectory> <outputDirectory>${basedir}/src/main/resources/webapps/</outputDirectory>
@ -103,6 +108,25 @@
</artifactItems> </artifactItems>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>copy-controller-jaxrs-war</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.controller.api</artifactId>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources/webapps/</outputDirectory>
<destFileName>android_sense.war</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions> </executions>
</plugin> </plugin>

@ -379,7 +379,13 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId> <groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.api</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.androidsense.controller.api</artifactId>
<version>${carbon.iot.device.mgt.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt-plugins</groupId>
<artifactId>org.wso2.carbon.device.mgt.iot.androidsense.mgt.api</artifactId>
<version>${carbon.iot.device.mgt.version}</version> <version>${carbon.iot.device.mgt.version}</version>
<type>war</type> <type>war</type>
</dependency> </dependency>

Loading…
Cancel
Save