From 2752428e24c10fcac9e0591f3f9230b5eb9498ed Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 30 Oct 2020 02:04:37 -0700 Subject: [PATCH] dev/core#2141 - Split OAuthProvider files into subdirs. Use long cache. --- ext/oauth-client/Civi/Api4/OAuthProvider.php | 2 +- .../data/oauth-providers.dist.json | 30 ------------------- .../data/oauth-providers.test.json | 21 ------------- ext/oauth-client/oauth_client.php | 25 +++++++++------- ext/oauth-client/providers/gmail.dist.json | 26 ++++++++++++++++ .../providers/ms-exchange.dist.json | 27 +++++++++++++++++ .../providers/test_example_1.test.json | 9 ++++++ .../providers/test_example_2.test.json | 9 ++++++ 8 files changed, 86 insertions(+), 63 deletions(-) delete mode 100644 ext/oauth-client/data/oauth-providers.dist.json delete mode 100644 ext/oauth-client/data/oauth-providers.test.json create mode 100644 ext/oauth-client/providers/gmail.dist.json create mode 100644 ext/oauth-client/providers/ms-exchange.dist.json create mode 100644 ext/oauth-client/providers/test_example_1.test.json create mode 100644 ext/oauth-client/providers/test_example_2.test.json diff --git a/ext/oauth-client/Civi/Api4/OAuthProvider.php b/ext/oauth-client/Civi/Api4/OAuthProvider.php index 47b33005e2..fc556d5192 100644 --- a/ext/oauth-client/Civi/Api4/OAuthProvider.php +++ b/ext/oauth-client/Civi/Api4/OAuthProvider.php @@ -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 index 685ee5000b..0000000000 --- a/ext/oauth-client/data/oauth-providers.dist.json +++ /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 index 7f2339f33d..0000000000 --- a/ext/oauth-client/data/oauth-providers.test.json +++ /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"] - } - } -] diff --git a/ext/oauth-client/oauth_client.php b/ext/oauth-client/oauth_client.php index 0940263af5..804658a299 100644 --- a/ext/oauth-client/oauth_client.php +++ b/ext/oauth-client/oauth_client.php @@ -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 index 0000000000..c8b9426e71 --- /dev/null +++ b/ext/oauth-client/providers/gmail.dist.json @@ -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 index 0000000000..5cf1317614 --- /dev/null +++ b/ext/oauth-client/providers/ms-exchange.dist.json @@ -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 index 0000000000..99a59c6d81 --- /dev/null +++ b/ext/oauth-client/providers/test_example_1.test.json @@ -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 index 0000000000..e5e57483b4 --- /dev/null +++ b/ext/oauth-client/providers/test_example_2.test.json @@ -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"] + } +} -- 2.25.1