src/includes/classes/Core/Utils/OAuthServer.php
public function authorize(Request $Request, Response $Response, callable $callback)
    {
        try { // Handles `/authorize` endpoint.
            if (!$this->c::isActiveSession()) {
                throw $this->c::issue('Requires active session.');
            }
            $_SESSION['oauth']   = $_SESSION['oauth'] ?? [];
            $AuthorizationServer = $this->authorizationServer();

            $_nonce_action       = 'oauth-authorize';
            $_nonce              = (string) $Request->getParam('_nonce');

            if ($_nonce && !$this->c::verifyNonce($_nonce_action, $_nonce)) {
                throw $this->c::issue('Unable to verify `_nonce`.');
            }
            if ($_nonce && !empty($_SESSION['oauth'][$_nonce]['AuthorizationRequest'])) {
                if (!($AuthorizationRequest = unserialize($_SESSION['oauth'][$_nonce]['AuthorizationRequest']))) {
                    throw $this->c::issue('Unable to unserialize `AuthorizationRequest`.');
                }
                $user_id  = (string) ($_SESSION['oauth']['user_id'] ?? '');
                $approved = $this->c::isTruthy($Request->getParam('approved'));

                if ($user_id && $approved) {
                    $UserEntity = $this->App->Di->get(UserEntity::class);
                    $UserEntity->setIdentifier($user_id);

                    $AuthorizationRequest->setUser($UserEntity);
                    $AuthorizationRequest->setAuthorizationApproved(true);

                    unset($_SESSION['oauth']);
                    $Response = $AuthorizationServer->completeAuthorizationRequest($AuthorizationRequest, $Response);
                    $Response->output(['exit' => true]);
                } else {
                    $_nonce                                             = $this->c::createNonce($_nonce_action);
                    $_SESSION['oauth'][$_nonce]['AuthorizationRequest'] = serialize($AuthorizationRequest);

                    $callback($AuthorizationRequest, $_nonce);
                    exit; // Stop here.
                }
            } else {
                $AuthorizationRequest                               = $AuthorizationServer->validateAuthorizationRequest($Request);
                $_nonce                                             = $this->c::createNonce($_nonce_action);
                $_SESSION['oauth'][$_nonce]['AuthorizationRequest'] = serialize($AuthorizationRequest);

                $callback($AuthorizationRequest, $_nonce);
                exit; // Stop here.
            }
        } catch (OAuthServerException $Exception) {
            unset($_SESSION['oauth']);
            $Response = $Exception->generateHttpResponse($Response);
            $Response->output(['exit' => true]);
            //
        } catch (\Throwable $Exception) {
            unset($_SESSION['oauth']);
            $Exception = new OAuthServerException($Exception->getMessage(), 0, 'unknown', 500);
            $Response  = $Exception->generateHttpResponse($Response);
            $Response->output(['exit' => true]);
        }
    }
public function authorize(Request $Request, Response $Response, callable $callback)
    {
        try { // Handles `/authorize` endpoint.
            if (!$this->c::isActiveSession()) {
                throw $this->c::issue('Requires active session.');
            }
            $_SESSION['oauth']   = $_SESSION['oauth'] ?? [];
            $AuthorizationServer = $this->authorizationServer();

            $_nonce_action       = 'oauth-authorize';
            $_nonce              = (string) $Request->getParam('_nonce');

            if ($_nonce && !$this->c::verifyNonce($_nonce_action, $_nonce)) {
                throw $this->c::issue('Unable to verify `_nonce`.');
            }
            if ($_nonce && !empty($_SESSION['oauth'][$_nonce]['AuthorizationRequest'])) {
                if (!($AuthorizationRequest = unserialize($_SESSION['oauth'][$_nonce]['AuthorizationRequest']))) {
                    throw $this->c::issue('Unable to unserialize `AuthorizationRequest`.');
                }
                $user_id  = (string) ($_SESSION['oauth']['user_id'] ?? '');
                $approved = $this->c::isTruthy($Request->getParam('approved'));

                if ($user_id && $approved) {
                    $UserEntity = $this->App->Di->get(UserEntity::class);
                    $UserEntity->setIdentifier($user_id);

                    $AuthorizationRequest->setUser($UserEntity);
                    $AuthorizationRequest->setAuthorizationApproved(true);

                    unset($_SESSION['oauth']);
                    $Response = $AuthorizationServer->completeAuthorizationRequest($AuthorizationRequest, $Response);
                    $Response->output(['exit' => true]);
                } else {
                    $_nonce                                             = $this->c::createNonce($_nonce_action);
                    $_SESSION['oauth'][$_nonce]['AuthorizationRequest'] = serialize($AuthorizationRequest);

                    $callback($AuthorizationRequest, $_nonce);
                    exit; // Stop here.
                }
            } else {
                $AuthorizationRequest                               = $AuthorizationServer->validateAuthorizationRequest($Request);
                $_nonce                                             = $this->c::createNonce($_nonce_action);
                $_SESSION['oauth'][$_nonce]['AuthorizationRequest'] = serialize($AuthorizationRequest);

                $callback($AuthorizationRequest, $_nonce);
                exit; // Stop here.
            }
        } catch (OAuthServerException $Exception) {
            unset($_SESSION['oauth']);
            $Response = $Exception->generateHttpResponse($Response);
            $Response->output(['exit' => true]);
            //
        } catch (\Throwable $Exception) {
            unset($_SESSION['oauth']);
            $Exception = new OAuthServerException($Exception->getMessage(), 0, 'unknown', 500);
            $Response  = $Exception->generateHttpResponse($Response);
            $Response->output(['exit' => true]);
        }
    }
Authorize.

@since 17xxxx OAuth utils.

@param Request  $Request  Request.
@param Response $Response Response.
@param callable $callback Callback.