Fix not to issue session if user unauthorized. 11/7511/2
authorJee Hyeok Kim <jihyeok13.kim@samsung.com>
Fri, 1 Apr 2016 01:05:25 +0000 (10:05 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Fri, 1 Apr 2016 04:01:52 +0000 (04:01 +0000)
1. Check userId and return unauthorized response
   if cannot find userId

2. Code cleanup

Change-Id: I2b0053512f51168e809b2b3320dd5645ff9a4632
Signed-off-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7511
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
cloud/account/src/main/java/org/iotivity/cloud/accountserver/AccountServer.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/GitHub.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/AccountResource.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/AuthResource.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/ResourceDirectoryServer.java

index 11bec5d..a9d6925 100644 (file)
@@ -61,7 +61,7 @@ public class AccountServer {
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
 
-        Scanner in = new Scanner(System.in, "UTF8");
+        Scanner in = new Scanner(System.in, "UTF-8");
 
         System.out.println("press 'q' to terminate");
 
index d44a6dc..e682257 100644 (file)
@@ -76,16 +76,21 @@ public class GitHub extends OAuthServer {
     @Override
     public String requestGetUserInfo(String accessToken) {
 
-        String userInfo = "{}";
+        String userInfo = null;
+
+        if (accessToken == null) {
+            Logger.w("accessToken is null!");
+            return null;
+        }
 
         try {
 
             OAuthClientRequest request = new OAuthBearerClientRequest(
                     resource_url).setAccessToken(accessToken)
-                            .buildQueryMessage();
+                    .buildQueryMessage();
 
-            OAuthClient oAuthClient = new OAuthClient(
-                    new URLConnectionClient());
+            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
+            
             OAuthResourceResponse resourceResponse = oAuthClient.resource(
                     request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
 
index 23d48a6..bde32bf 100644 (file)
@@ -216,8 +216,7 @@ public class AccountResource extends Resource {
         ArrayList<String> deviceList = response.getDeviceList();
         responseMap.put(Constants.RESPONSE_DEVICES, deviceList);
 
-        JSONUtil jsonUtil = new JSONUtil();
-        String responseJson = jsonUtil.writeJSON(responseMap);
+        String responseJson = JSONUtil.writeJSON(responseMap);
 
         return responseJson;
     }
index 734acb9..a88bcf8 100644 (file)
@@ -173,30 +173,37 @@ public class AuthResource extends Resource {
         if (authCode != null && authServer != null) {
             userId = oauthServerManager.requestUserId(authCode, authServer);
         }
-        String sessionCode = oauthServerManager.registerUserAccount(userId);
-
-        Logger.d("userId: " + userId + ", sessionCode: " + sessionCode);
-
+        
         CoapMessageBuilder responseMessage = new CoapMessageBuilder();
         CoapResponse coapResponse;
 
-        if (userId != null && sessionCode != null) {
+        if (userId != null) {
+            
+            String sessionCode = oauthServerManager.registerUserAccount(userId);
 
-            ResponseObject response = new ResponseObject();
-            response.setSessionCode(sessionCode);
-            response.setUserId(userId);
+            Logger.d("userId: " + userId + ", sessionCode: " + sessionCode);
 
-            String responseJson = convertRegisterResponseToJson(response);
-            Logger.d("responseJson: " + responseJson);
+            if (sessionCode != null) {
 
-            coapResponse = responseMessage.buildCoapResponse(request.getToken(),
-                    responseJson, CoapStatus.CREATED);
+                ResponseObject response = new ResponseObject();
+                response.setSessionCode(sessionCode);
+                response.setUserId(userId);
+
+                String responseJson = convertRegisterResponseToJson(response);
+                Logger.d("responseJson: " + responseJson);
+
+                coapResponse = responseMessage.buildCoapResponse(
+                        request.getToken(), responseJson, CoapStatus.CREATED);
+            }
+            else  {
+                coapResponse = responseMessage.buildCoapResponse(request.getToken(),
+                        CoapStatus.UNAUTHORIZED);                
+            }
 
         } else {
 
             coapResponse = responseMessage.buildCoapResponse(request.getToken(),
                     CoapStatus.UNAUTHORIZED);
-
         }
 
         return coapResponse;
@@ -215,8 +222,7 @@ public class AuthResource extends Resource {
         if (sessionCode != null)
             responseMap.put(Constants.RESPONSE_SESSION_CODE, sessionCode);
 
-        JSONUtil jsonUtil = new JSONUtil();
-        String responseJson = jsonUtil.writeJSON(responseMap);
+        String responseJson = JSONUtil.writeJSON(responseMap);
 
         return responseJson;
     }
@@ -230,8 +236,7 @@ public class AuthResource extends Resource {
         if (userId != null)
             responseMap.put(Constants.RESPONSE_USER_ID, userId);
 
-        JSONUtil jsonUtil = new JSONUtil();
-        String responseJson = jsonUtil.writeJSON(responseMap);
+        String responseJson = JSONUtil.writeJSON(responseMap);
 
         return responseJson;
     }
index 75953de..ca6aa00 100644 (file)
@@ -22,7 +22,6 @@
 package org.iotivity.cloud.ciserver;
 
 import java.net.InetSocketAddress;
-import java.nio.charset.StandardCharsets;
 import java.util.Scanner;
 
 import org.iotivity.cloud.base.CoapServer;
@@ -89,7 +88,7 @@ public class CloudInterfaceServer {
 
         keepAliveResource.startSessionChecker();
 
-        Scanner in = new Scanner(System.in, "UTF8");
+        Scanner in = new Scanner(System.in, "UTF-8");
 
         System.out.println("press 'q' to terminate");
 
index 9b0903e..8f58000 100644 (file)
@@ -55,7 +55,7 @@ public class ResourceDirectoryServer {
         coapServer
                 .startServer(new InetSocketAddress(Integer.parseInt(args[0])));
 
-        Scanner in = new Scanner(System.in, "UTF8");
+        Scanner in = new Scanner(System.in, "UTF-8");
 
         System.out.println("press 'q' to terminate");