/** * @param \Cake\Network\Request $request Request to get authentication information from. * @param \Cake\Network\Response $response A response object that can have headers added. * @return bool|\Cake\Network\Response */ public function unauthenticated(Request $request, Response $response) { if ($this->_config['continue']) { return null; } if (isset($this->_exception)) { $response->statusCode($this->_exception->httpStatusCode); //add : to http code for cakephp (header method in Network/Response expects header separated with colon notation) $headers = $this->_exception->getHttpHeaders(); $code = (string) $this->_exception->httpStatusCode; $headers = array_map(function ($header) use($code) { $pos = strpos($header, $code); if ($pos !== false) { return substr($header, 0, $pos + strlen($code)) . ':' . substr($header, $pos + strlen($code) + 1); } return $header; }, $headers); $response->header($headers); $response->body(json_encode(['error' => $this->_exception->errorType, 'message' => $this->_exception->getMessage()])); return $response; } $message = __d('authenticate', 'You are not authenticated.'); throw new BadRequestException($message); }
public function testPassesExceptionDetailsThroughAnAdaptor() { $request = 'x'; $e = new OAuthException('message here'); $e->errorType = 'invalid_client'; $e->httpStatusCode = 401; $mockAdaptor = m::mock(Adaptor::class)->shouldReceive('adapt')->with(['error' => 'invalid_client', 'error_description' => 'message here'], $e->httpStatusCode, $e->getHttpHeaders())->andReturn('this is a response')->getMock(); $mockAdaptorFactory = m::mock(AdaptorFactory::class)->shouldReceive('make')->with($request)->andReturn($mockAdaptor)->getMock(); $formatter = new LeagueOAuthExceptionFormatter($mockAdaptorFactory); $response = $formatter->handle($e, $request); $this->assertEquals('this is a response', $response); }
/** * @param OAuthException $e * @return mixed */ protected function unsupportedResponseType(OAuthException $e) { return Response::make(['errors' => ['status' => '400', 'code' => 'UnsupportedResponseType', 'title' => 'Unsupported Response Type', 'detail' => $e->getMessage()]], 400); }
/** * Get a response object for an OAuthException. * * @param OAuthException $e * * @return Response */ protected function getExceptionResponse(OAuthException $e) { return new Response(json_encode(['error' => $e->errorType, 'message' => $e->getMessage()]), $e->httpStatusCode, $e->getHttpHeaders()); }
public function handle(OAuthException $e, $request) { return $this->adaptors->make($request)->adapt(['error' => $e->errorType, 'error_description' => $e->getMessage()], $e->httpStatusCode, $e->getHttpHeaders()); }
/** * {@inheritdoc} */ public function __construct() { parent::__construct('User authentication failed. Check authorization token'); }
/** * {@inheritdoc} */ public function __construct() { parent::__construct('Client authentication failed. Reference URL not found'); }