Make loading of oauth libraries available without changing iotivity codebase.
authorOndrej Tomcik <ondrej.tomcik@kistler.com>
Wed, 17 May 2017 22:15:45 +0000 (00:15 +0200)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 22 May 2017 08:18:51 +0000 (08:18 +0000)
Change-Id: I4ac25b7f605e355fc3aad286baa206d67584be69
Signed-off-by: Ondrej Tomcik <ondrej.tomcik@kistler.com>
Issue: IOT-2316
Reviewed-on: https://gerrit.iotivity.org/gerrit/20009
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/account/pom.xml
cloud/account/src/main/java/org/iotivity/cloud/accountserver/Constants.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/oauth/OAuthProviderFactory.java
cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java

index 77acc5b945468a2129ce818f8ac48366769411eb..0f166ac5e3cfac6be2296eb5628e537f8cb44d63 100644 (file)
                        <artifactId>awaitility</artifactId>
                        <version>1.7.0</version>
                </dependency>
+               <dependency>
+                       <groupId>org.reflections</groupId>
+                       <artifactId>reflections</artifactId>
+                       <version>0.9.11</version>
+               </dependency>
                <dependency>
                        <groupId>org.powermock</groupId>
                        <artifactId>powermock-api-mockito</artifactId>
index 00b579ddb59ba2b62e68b752b38f7d076e007419..d684f2ac1e1a1157a08def0da03d474b4cb99531 100644 (file)
@@ -270,12 +270,8 @@ public class Constants extends OICConstants {
 
     public static final int    TOKEN_INFINITE             = -1;
 
-    // auth servers
+    // oauth
 
-    public static final String GITHUB                     = "Github";
-
-    public static final String SAMSUNG                    = "Samsung";
-
-    public static final String GOOGLE                     = "Google";
+    public static final String OAUTH_LIBRARIES_PATH       = "oauth/";
 
 }
index 5933c5f4ebac58ce5e44ef090205e107186e0990..52f31483af1583be80eff77d1bd5848764411055 100644 (file)
@@ -26,11 +26,17 @@ import java.io.IOException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Set;
 
+import org.iotivity.cloud.accountserver.Constants;
 import org.iotivity.cloud.accountserver.db.TokenTable;
 import org.iotivity.cloud.accountserver.db.UserTable;
 import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
 import org.iotivity.cloud.util.Log;
+import org.reflections.Reflections;
+import org.reflections.scanners.SubTypesScanner;
+import org.reflections.util.ClasspathHelper;
+import org.reflections.util.ConfigurationBuilder;
 
 /**
  *
@@ -128,27 +134,31 @@ public class OAuthProviderFactory {
 
     /**
      * API for creating OAuth server object
-     * 
-     * @param authServer
-     *            authorization server
+     *
+     * @param authProviderName
+     *            Valid name of auth server.
      * @return OAuthServer - object to handle authorization
      * @throws Exception
      */
-    public boolean load(String authProvider) {
+    public boolean load(String authProviderName) {
 
-        String fileName = authProvider + ".jar";
-        File jarFile = new File(fileName);
+        File jarFile = new File(Constants.OAUTH_LIBRARIES_PATH + authProviderName + ".jar");
         URLClassLoader classLoader = null;
 
         try {
-            URL urls = new URL("jar:" + jarFile.toURI() + "!/");
+            URL jarUrl = new URL("jar:" + jarFile.toURI() + "!/");
+            URL[] urls = new URL[] { jarUrl, ClasspathHelper.forClass(this.getClass()) };
+            classLoader = new URLClassLoader(urls);
 
-            Log.d("urls: " + urls.toString());
+            String authProvider = this.getClass().getPackage().getName() + "." + authProviderName;
+            Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(urls)
+                    .setScanners(new SubTypesScanner(false)));
+            for (String type : reflections.getAllTypes())
+                if (type.equalsIgnoreCase(authProvider))
+                    authProvider = type;
 
-            classLoader = new URLClassLoader(new URL[] { urls });
             Class<?> authProviderClass = classLoader
-                    .loadClass(this.getClass().getPackage().getName() + "."
-                            + authProvider);
+                    .loadClass(authProvider);
 
             this.authProviderClass = authProviderClass;
             Object object = authProviderClass.newInstance();
index cc3f05573a7f69173e9a7cc2d700e99abfb280de..fd2da9d9a139aec62a381df63966957e705a07a0 100644 (file)
@@ -21,6 +21,7 @@
  */
 package org.iotivity.cloud.accountserver.resources.account;
 
+import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -72,7 +73,7 @@ public class AccountManager {
      * @param authCode
      *            Unique identifier of the resource which is obtained from an
      *            auth provider or a single sign-on (SSO) client
-     * @param authProvider
+     * @param authProviderName
      *            Provider name user for authentication (e.g., "Github")
      * @param options
      *            Optional field (e.g., region authserver url, apiserver url)
@@ -81,22 +82,20 @@ public class AccountManager {
      */
 
     public HashMap<String, Object> signUp(String did, String authCode,
-            String authProvider, Object options) {
-        boolean res = false;
+            String authProviderName, Object options) {
 
-        // check auth provider name not to be case-sensitive
-        authProvider = checkAuthProviderName(authProvider);
-        res = loadAuthProviderLibrary(authProvider);
+        authProviderName = checkAuthProviderName(authProviderName);
+        boolean res = loadAuthProviderLibrary(authProviderName);
 
         if (!res) {
             throw new InternalServerErrorException(
-                    authProvider + " library is not loaded");
+                    authProviderName + " library is not loaded");
         }
         String userUuid = null;
         // set token data
         TokenTable tokenInfo = requestAccessToken(authCode, options);
         tokenInfo.setDid(did);
-        tokenInfo.setProvider(authProvider);
+        tokenInfo.setProvider(authProviderName);
         Date currentTime = new Date();
         DateFormat transFormat = new SimpleDateFormat("yyyyMMddkkmm");
         tokenInfo.setIssuedtime(transFormat.format(currentTime));
@@ -104,10 +103,10 @@ public class AccountManager {
         // set user data
         UserTable userInfo = requestUserInfo(tokenInfo.getAccesstoken(),
                 options);
-        userInfo.setProvider(authProvider);
+        userInfo.setProvider(authProviderName);
 
         // check uuid
-        userUuid = findUuid(userInfo.getUserid(), authProvider);
+        userUuid = findUuid(userInfo.getUserid(), authProviderName);
 
         // store token information and user information to the DB
         // private group creation and store group information to the DB
@@ -239,21 +238,27 @@ public class AccountManager {
         return userUuid;
     }
 
-    private String checkAuthProviderName(String authProvider) {
+    private String checkAuthProviderName(String authProviderName) {
+        String libraryFileName = getValidFileName(Constants.OAUTH_LIBRARIES_PATH, authProviderName + ".jar");
+        if (libraryFileName == null) {
+            Log.w("OAuth 3rd party library " + authProviderName + " does not exist.");
+            return authProviderName;
+        }
+        return libraryFileName.substring(0, libraryFileName.length() - 4);
+    }
 
-        String authProviderName = null;
+    private String getValidFileName(String path, String filename) {
+        File file = new File(path + filename);
+        if(file.exists())
+            return filename;
 
-        if (authProvider.equalsIgnoreCase(Constants.GITHUB)) {
-            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);
-        }
+        File parentFile = file.getAbsoluteFile().getParentFile();
+        if (parentFile.exists())
+            for (String directoryFile : parentFile.list())
+                if (directoryFile.equalsIgnoreCase(file.getName()))
+                    return directoryFile;
 
-        return authProviderName;
+        return null;
     }
 
     private String findUuid(String userId, String authProvider) {
@@ -335,10 +340,10 @@ public class AccountManager {
         return byteRootCert;
     }
 
-    private Boolean loadAuthProviderLibrary(String authProvider) {
+    private Boolean loadAuthProviderLibrary(String authProviderName) {
         mFactory = new OAuthProviderFactory();
 
-        return mFactory.load(authProvider);
+        return mFactory.load(authProviderName);
     }
 
     private TokenTable requestAccessToken(String authCode, Object options) {
@@ -475,17 +480,15 @@ public class AccountManager {
     }
 
     private TokenTable requestRefreshToken(String refreshToken,
-            String provider) {
+            String authProviderName) {
 
         if (mFactory == null) {
-
-            boolean res = false;
-            String authProvider = checkAuthProviderName(provider);
-            res = loadAuthProviderLibrary(authProvider);
+            authProviderName = checkAuthProviderName(authProviderName);
+            boolean res = loadAuthProviderLibrary(authProviderName);
 
             if (!res) {
                 throw new InternalServerErrorException(
-                        authProvider + " library is not loaded");
+                        authProviderName + " library is not loaded");
             }
         }