dev/core#2141 - Split OAuthProvider files into subdirs. Use long cache.
authorTim Otten <totten@civicrm.org>
Fri, 30 Oct 2020 09:04:37 +0000 (02:04 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 3 Nov 2020 12:32:48 +0000 (04:32 -0800)
ext/oauth-client/Civi/Api4/OAuthProvider.php
ext/oauth-client/data/oauth-providers.dist.json [deleted file]
ext/oauth-client/data/oauth-providers.test.json [deleted file]
ext/oauth-client/oauth_client.php
ext/oauth-client/providers/gmail.dist.json [new file with mode: 0644]
ext/oauth-client/providers/ms-exchange.dist.json [new file with mode: 0644]
ext/oauth-client/providers/test_example_1.test.json [new file with mode: 0644]
ext/oauth-client/providers/test_example_2.test.json [new file with mode: 0644]

index 47b33005e2fb47145062381cb3030a5301db2995..fc556d51923cee391cf0ccd21857fa5e971b8e45 100644 (file)
@@ -15,7 +15,7 @@ class OAuthProvider extends Generic\AbstractEntity {
    */
   public static function get($checkPermissions = TRUE) {
     $action = new Generic\BasicGetAction('OAuthProvider', __FUNCTION__, function () {
-      $cache = \Civi::cache('short');
+      $cache = \Civi::cache('long');
       if (!$cache->has('OAuthProvider_list')) {
         $providers = [];
         $event = GenericHookEvent::create([
diff --git a/ext/oauth-client/data/oauth-providers.dist.json b/ext/oauth-client/data/oauth-providers.dist.json
deleted file mode 100644 (file)
index 685ee50..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-[
-  {
-    "name": "ms-exchange",
-    "title": "Microsoft: Exchange Online",
-    "options": {
-      "urlAuthorize": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
-      "urlAccessToken": "https://login.microsoftonline.com/common/oauth2/v2.0/token",
-      "urlResourceOwnerDetails": "https://graph.microsoft.com/v1.0/me",
-      "scopeSeparator": " ",
-      "scopes": [
-        "User.Read",
-        "https://outlook.office.com/IMAP.AccessAsUser.All",
-        "https://outlook.office.com/POP.AccessAsUser.All",
-        "https://outlook.office.com/SMTP.Send",
-        "offline_access"
-      ]
-    },
-    "mailSettingsTemplate": {
-      "name": "{{provider.name}}: {{token.resource_owner.mail}}",
-      "domain": "{{token.resource_owner.mail|getMailDomain}}",
-      "localpart": null,
-      "return_path": null,
-      "protocol:name": "IMAP",
-      "server": "outlook.office365.com",
-      "username": "{{token.resource_owner.mail}}",
-      "password": null,
-      "is_ssl": true
-    }
-  }
-]
diff --git a/ext/oauth-client/data/oauth-providers.test.json b/ext/oauth-client/data/oauth-providers.test.json
deleted file mode 100644 (file)
index 7f2339f..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-[
-  {
-    "name": "test_example_1",
-    "title": "First Test Example",
-    "options": {
-      "urlAuthorize": "https://example.com/one/auth",
-      "urlAccessToken": "https://example.com/one/token",
-      "urlResourceOwnerDetails": "https://example.com/one/owner",
-      "scopes": ["scope-1-foo", "scope-1-bar"]
-    }
-  },
-  {
-    "name": "test_example_2",
-    "title": "Second Test Example",
-    "class": "My\\Example2",
-    "options": {
-      "urlAuthorize": "https://example.com/two",
-      "scopes": ["scope-2-foo", "scope-2-bar"]
-    }
-  }
-]
index 0940263af5c31bdafc8a81d5056b6aafa594b027..804658a299a098afffe30b9d53d04abd764e4f23 100644 (file)
@@ -193,19 +193,22 @@ function oauth_client_civicrm_themes(&$themes) {
  * Implements hook_civicrm_oauthProviders().
  */
 function oauth_client_civicrm_oauthProviders(&$providers) {
-  $ingest = function($file) use (&$providers) {
-    $parsed = json_decode(file_get_contents($file), 1);
-    foreach ($parsed as $provider) {
-      $providers[$provider['name']] = $provider;
+  $ingest = function($pat) use (&$providers) {
+    $files = (array) glob($pat);
+    foreach ($files as $file) {
+      if (!defined('CIVICRM_TEST') && preg_match(';\.test\.json$;', $file)) {
+        continue;
+      }
+      $name = preg_replace(';\.(dist\.|test\.|)json$;', '', basename($file));
+      $provider = json_decode(file_get_contents($file), 1);
+      $provider['name'] = $name;
+      $providers[$name] = $provider;
     }
   };
 
-  $ingest(__DIR__ . '/data/oauth-providers.dist.json');
-  if (defined('CIVICRM_TEST')) {
-    $ingest(__DIR__ . '/data/oauth-providers.test.json');
-  }
-  $localFile = Civi::paths()->getPath('[civicrm.private]/oauth-providers.local.json');
-  if (file_exists($localFile)) {
-    $ingest($localFile);
+  $ingest(__DIR__ . '/providers/*.json');
+  $localDir = Civi::paths()->getPath('[civicrm.private]/oauth-providers');
+  if (file_exists($localDir)) {
+    $ingest($localDir . '/*.json');
   }
 }
diff --git a/ext/oauth-client/providers/gmail.dist.json b/ext/oauth-client/providers/gmail.dist.json
new file mode 100644 (file)
index 0000000..c8b9426
--- /dev/null
@@ -0,0 +1,26 @@
+{
+  "title": "Google Mail",
+  "class": "League\\OAuth2\\Client\\Provider\\Google",
+  "options": {
+    "urlAuthorize": "https://accounts.google.com/o/oauth2/v2/auth",
+    "urlAccessToken": "https://www.googleapis.com/oauth2/v4/token",
+    "urlResourceOwnerDetails": "https://openidconnect.googleapis.com/v1/userinfo",
+    "accessType": "offline",
+    "scopeSeparator": " ",
+    "scopes": [
+      "https://mail.google.com/",
+      "openid"
+    ]
+  },
+  "mailSettingsTemplate": {
+    "name": "{{token.resource_owner.email}}",
+    "domain": "{{token.resource_owner.email|getMailDomain}}",
+    "localpart": null,
+    "return_path": null,
+    "protocol:name": "IMAP",
+    "server": "imap.gmail.com",
+    "username": "{{token.resource_owner.email}}",
+    "password": null,
+    "is_ssl": true
+  }
+}
\ No newline at end of file
diff --git a/ext/oauth-client/providers/ms-exchange.dist.json b/ext/oauth-client/providers/ms-exchange.dist.json
new file mode 100644 (file)
index 0000000..5cf1317
--- /dev/null
@@ -0,0 +1,27 @@
+{
+  "title": "Microsoft Exchange Online",
+  "options": {
+    "urlAuthorize": "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
+    "urlAccessToken": "https://login.microsoftonline.com/common/oauth2/v2.0/token",
+    "urlResourceOwnerDetails": "https://graph.microsoft.com/v1.0/me",
+    "scopeSeparator": " ",
+    "scopes": [
+      "User.Read",
+      "https://outlook.office.com/IMAP.AccessAsUser.All",
+      "https://outlook.office.com/POP.AccessAsUser.All",
+      "https://outlook.office.com/SMTP.Send",
+      "offline_access"
+    ]
+  },
+  "mailSettingsTemplate": {
+    "name": "{{token.resource_owner.mail}}",
+    "domain": "{{token.resource_owner.mail|getMailDomain}}",
+    "localpart": null,
+    "return_path": null,
+    "protocol:name": "IMAP",
+    "server": "outlook.office365.com",
+    "username": "{{token.resource_owner.mail}}",
+    "password": null,
+    "is_ssl": true
+  }
+}
diff --git a/ext/oauth-client/providers/test_example_1.test.json b/ext/oauth-client/providers/test_example_1.test.json
new file mode 100644 (file)
index 0000000..99a59c6
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "title": "First Test Example",
+  "options": {
+    "urlAuthorize": "https://example.com/one/auth",
+    "urlAccessToken": "https://example.com/one/token",
+    "urlResourceOwnerDetails": "https://example.com/one/owner",
+    "scopes": ["scope-1-foo", "scope-1-bar"]
+  }
+}
diff --git a/ext/oauth-client/providers/test_example_2.test.json b/ext/oauth-client/providers/test_example_2.test.json
new file mode 100644 (file)
index 0000000..e5e5748
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "name": "test_example_2",
+  "title": "Second Test Example",
+  "class": "My\\Example2",
+  "options": {
+    "urlAuthorize": "https://example.com/two",
+    "scopes": ["scope-2-foo", "scope-2-bar"]
+  }
+}