0

When I try to access API when i'm not authorized I want to customize response to return link where I can get token. What class should I override or change? I have no idea where to start with this and sorry for this long post but i can't post shorter. :/ I did not tried anything because I don't know how. Just let me post question please

1 Answer 1

0

Ok, i added this:

if ($this->getStatusCode() == 401)
        $this->setHeader('WWW-Authenticate', 'basic');

In \Magento\Framework\Webapi\Rest\Response.php class at the end of _renderMessages() and that works fine but it is not practice to directly change core functionality. So i made modul Rewriter. Made etc/di.xml that looks like:

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Webapi\Rest\Response" type="Sdu\Rewriter\Rest\Response" />
</config>

And made new Response.php class in this new module:

<?php
namespace Sdu\Rewriter\Rest;


class Response extends \Magento\Framework\Webapi\Rest\Response
{
   public function _renderMessages()
   {
       $responseHttpCode = null;
       /** @var \Exception $exception */
       foreach ($this->getException() as $exception) {
           $maskedException = $this->_errorProcessor->maskException($exception);
           $messageData = [
               'message' => $maskedException->getMessage(),
           ];
           if ($maskedException->getErrors()) {
               $messageData['errors'] = [];
               foreach ($maskedException->getErrors() as $errorMessage) {
                   $errorData['message'] = $errorMessage->getRawMessage();
                   $errorData['parameters'] = $errorMessage->getParameters();
                   $messageData['errors'][] = $errorData;
               }
           }
           if ($maskedException->getCode()) {
               $messageData['code'] = $maskedException->getCode();
           }
           if ($maskedException->getDetails()) {
               $messageData['parameters'] = $maskedException->getDetails();
           }
           if ($this->_appState->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) {
               $messageData['trace'] = $exception instanceof \Magento\Framework\Webapi\Exception
                   ? $exception->getStackTrace()
                   : $exception->getTraceAsString();
           }
           $responseHttpCode = $maskedException->getHttpCode();
       }
       // set HTTP code of the last error, Content-Type, and all rendered error messages to body
       $this->setHttpResponseCode($responseHttpCode);
       $this->setMimeType($this->_renderer->getMimeType());
       if ($this->getStatusCode() == 401)
           $this->setHeader('WWW-Authenticate', 'basic');
       $this->setBody($this->_renderer->render($messageData));
       return $this;
   }
}

You can see added to lines at the end of method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.