(REF) authx - Add option for authenticator to emit exceptions
authorTim Otten <totten@civicrm.org>
Fri, 10 Dec 2021 05:51:22 +0000 (21:51 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 21 Dec 2021 22:55:45 +0000 (14:55 -0800)
Before: If the authenticator encounters an error, it sends a response document.

After: The authenticator encoutners an error, it may either:

- (Default) Send a response document
- (Option) Emit an exception

ext/authx/Civi/Authx/Authenticator.php
ext/authx/Civi/Authx/AuthxException.php [new file with mode: 0644]

index 6fa9dd702b57ddf3408a0e44cb0eca460f8c41ae..a52a1675ded0349f7d6b6ce4e2a71582200f9e01 100644 (file)
@@ -27,6 +27,12 @@ class Authenticator {
    */
   protected $authxUf;
 
+  /**
+   * @var string
+   *   Ex: 'send' or 'exception
+   */
+  protected $rejectMode = 'send';
+
   /**
    * Authenticator constructor.
    */
@@ -222,12 +228,27 @@ class Authenticator {
     );
   }
 
+  /**
+   * Specify the rejection mode.
+   *
+   * @param string $mode
+   * @return $this
+   */
+  public function setRejectMode(string $mode) {
+    $this->rejectMode = $mode;
+    return $this;
+  }
+
   /**
    * Reject a bad authentication attempt.
    *
    * @param string $message
    */
   protected function reject($message = 'Authentication failed') {
+    if ($this->rejectMode === 'exception') {
+      throw new AuthxException($message);
+    }
+
     \CRM_Core_Session::useFakeSession();
     $r = new Response(401, ['Content-Type' => 'text/plain'], "HTTP 401 $message");
     \CRM_Utils_System::sendResponse($r);
diff --git a/ext/authx/Civi/Authx/AuthxException.php b/ext/authx/Civi/Authx/AuthxException.php
new file mode 100644 (file)
index 0000000..9866e6e
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Authx;
+
+class AuthxException extends \CRM_Core_Exception {
+
+}