From cf4a137715594c6f67e5764f2574b7bd00ed4eca Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 9 Dec 2021 21:51:22 -0800 Subject: [PATCH] (REF) authx - Add option for authenticator to emit exceptions 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 | 21 +++++++++++++++++++++ ext/authx/Civi/Authx/AuthxException.php | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 ext/authx/Civi/Authx/AuthxException.php diff --git a/ext/authx/Civi/Authx/Authenticator.php b/ext/authx/Civi/Authx/Authenticator.php index 6fa9dd702b..a52a1675de 100644 --- a/ext/authx/Civi/Authx/Authenticator.php +++ b/ext/authx/Civi/Authx/Authenticator.php @@ -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 index 0000000000..9866e6eb9b --- /dev/null +++ b/ext/authx/Civi/Authx/AuthxException.php @@ -0,0 +1,16 @@ +