Modify AccountManager to support Google OAuth2 Provider
authorGlen Youngjin Kim <glen.kim@samsung.com>
Mon, 5 Sep 2016 01:49:10 +0000 (10:49 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Tue, 20 Sep 2016 10:42:44 +0000 (10:42 +0000)
and Token Refresh

Change-Id: Ie4c430ecfbf6ae4620e0490a82e2ef237a569ed9
Signed-off-by: Glen Youngjin Kim <glen.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11375
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Yeonghun Nam <yeonghun.nam@samsung.com>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
(cherry picked from commit f4c2bf41a3fe18d5cda53ccacc4698ee587bd777)
Reviewed-on: https://gerrit.iotivity.org/gerrit/11983
Tested-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java
cloud/samples/client/README

index 452bc00..58ca384 100644 (file)
@@ -126,6 +126,10 @@ public class AccountManager {
             authProviderName = Constants.GITHUB;
         } else if (authProvider.equalsIgnoreCase(Constants.SAMSUNG)) {
             authProviderName = Constants.SAMSUNG;
+        } else if (authProvider.equalsIgnoreCase(Constants.GOOGLE))
+            authProviderName = Constants.GOOGLE;
+        else {
+            Log.w("Unsupported oauth provider : " + authProvider);
         }
 
         return authProviderName;
@@ -221,7 +225,7 @@ public class AccountManager {
                 options);
         Log.d("access token : " + tokenInfo.getAccesstoken());
         Log.d("refresh token : " + tokenInfo.getRefreshtoken());
-        Log.d("expired time" + tokenInfo.getExpiredtime());
+        Log.d("expired time : " + tokenInfo.getExpiredtime());
 
         return tokenInfo;
     }
@@ -374,6 +378,7 @@ public class AccountManager {
         // find record about uuid and did
         HashMap<String, Object> condition = new HashMap<>();
         condition.put(Constants.KEYFIELD_UUID, uuid);
+        condition.put(Constants.KEYFIELD_DID, did);
 
         ArrayList<HashMap<String, Object>> recordList = findRecord(
                 AccountDBManager.getInstance()
@@ -387,12 +392,13 @@ public class AccountManager {
         HashMap<String, Object> record = recordList.get(0);
 
         TokenTable oldTokenInfo = castMapToTokenTable(record);
+        String provider = oldTokenInfo.getProvider();
 
         if (!checkRefreshTokenInDB(oldTokenInfo, refreshToken)) {
             throw new NotFoundException("refresh token is not correct");
         }
         // call 3rd party refresh token method
-        TokenTable newTokenInfo = requestRefreshToken(refreshToken);
+        TokenTable newTokenInfo = requestRefreshToken(refreshToken, provider);
 
         // record change
         oldTokenInfo.setAccesstoken(newTokenInfo.getAccesstoken());
@@ -420,7 +426,20 @@ public class AccountManager {
         return response;
     }
 
-    private TokenTable requestRefreshToken(String refreshToken) {
+    private TokenTable requestRefreshToken(String refreshToken,
+            String provider) {
+
+        if (mFactory == null) {
+
+            boolean res = false;
+            String authProvider = checkAuthProviderName(provider);
+            res = loadAuthProviderLibrary(authProvider);
+
+            if (!res) {
+                throw new InternalServerErrorException(
+                        authProvider + " library is not loaded");
+            }
+        }
 
         TokenTable tokenInfo = mFactory.requestRefreshTokenInfo(refreshToken);
 
index 50f9380..fef9eed 100644 (file)
@@ -6,15 +6,16 @@ Cloud clients runs over CoAP over TCP transport. So you should declare WITH_TCP
 
 Once you get samples which file name is 'aircon_controlee' and 'aircon_controller', you need 'Auth Code' to register resources on cloud with account scenario.
 
-Cloud stack has sample github oauth2 adaptor.
+Cloud stack has sample github and google oauth2 adaptor.
 
 So you can instantly test samples using 'Auth Code'.
 
 Paste below URL to your browser
 
-https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F
+[Github] https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F
+[Google] https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=http://www.example.com/oauth2callback&prompt=consent&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&response_type=code&client_id=447649044559-f9r5sl6op3kkk0312u384o4g6hhucje1.apps.googleusercontent.com&access_type=offline
 
-And you'll see login page provided by github, and after logged in, you'll be redirectd to below site.
+And you'll see login page provided by oauth2 providers, and after logged in, you'll be redirectd to below site.
 
 http://www.example.com/oauth_callback/?code=bf9beb5db17ea476fa46