1<?php
2
3namespace dokuwiki\plugin\oauth;
4
5use dokuwiki\HTTP\DokuHTTPClient;
6use OAuth\Common\Http\Client\ClientInterface;
7use OAuth\Common\Http\Uri\UriInterface;
8
9/**
10 * Implements the client interface using DokuWiki's HTTPClient
11 */
12class HTTPClient implements ClientInterface
13{
14    /** @inheritDoc */
15    public function retrieveResponse(
16        UriInterface $endpoint,
17        $requestBody,
18        array $extraHeaders = [],
19        $method = 'POST'
20    ) {
21        $http = new DokuHTTPClient();
22        $http->keep_alive = false;
23        $http->headers = array_merge($http->headers, $extraHeaders);
24
25        $ok = $http->sendRequest($endpoint->getAbsoluteUri(), $requestBody, $method);
26        if (!$ok || $http->status < 200 || $http->status > 299) {
27            $msg = "An error occured during the request to the oauth provider:\n";
28            throw new HttpTokenResponseException(
29                $msg . $http->error . ' [HTTP ' . $http->status . ']',
30                $http->status,
31                $http->error,
32                $http->resp_body
33            );
34        }
35
36        return $http->resp_body;
37    }
38}
39