dev/core#2141 - If a mail-store has an OAuth2 token, then use it
[civicrm-core.git] / ext / oauth-client / CRM / OAuth / MailSetup.php
index 7f144f14643a83d5b0dd1f439700ab5a61b27d3e..d12a03a5d07a76b81113c5d936195b8a43f241ef 100644 (file)
@@ -141,4 +141,28 @@ class CRM_OAuth_MailSetup {
     return $values;
   }
 
+  /**
+   * If we have a stored token for this for this, then use it.
+   *
+   * @see CRM_Utils_Hook::alterMailStore()
+   */
+  public static function alterMailStore(&$mailSettings) {
+    $token = civicrm_api4('OAuthSysToken', 'refresh', [
+      'checkPermissions' => FALSE,
+      'where' => [['tag', '=', 'MailSettings:' . $mailSettings['id']]],
+      'orderBy' => ['id' => 'DESC'],
+    ])->first();
+
+    if ($token === NULL) {
+      return;
+    }
+    // Not certain if 'refresh' will complain about staleness. Doesn't hurt to double-check.
+    if (empty($token['access_token']) || $token['expires'] < CRM_Utils_Time::getTimeRaw()) {
+      throw new \OAuthException("Found invalid token for mail store #" . $mailSettings['id']);
+    }
+
+    $mailSettings['auth'] = 'XOAuth2';
+    $mailSettings['password'] = $token['access_token'];
+  }
+
 }