diff --git a/src/LtiServiceConnector.php b/src/LtiServiceConnector.php index fb9631b6..d19ab8a2 100644 --- a/src/LtiServiceConnector.php +++ b/src/LtiServiceConnector.php @@ -23,6 +23,7 @@ class LtiServiceConnector implements ILtiServiceConnector public const SYNC_GRADE_REQUEST = 1; public const CREATE_LINEITEM_REQUEST = 2; public const GET_LINEITEMS_REQUEST = 3; + public const AUTH_REQUEST = 3; private $cache; private $client; @@ -41,6 +42,7 @@ public function __construct( static::SYNC_GRADE_REQUEST => 'Syncing grade for this lti_user_id: ', static::CREATE_LINEITEM_REQUEST => 'Creating lineitem: ', static::GET_LINEITEMS_REQUEST => 'Getting lineitems: ', + static::AUTH_REQUEST => 'Authenticating: ', ]; } @@ -84,9 +86,9 @@ public function getAccessToken(ILtiRegistration $registration, array $scopes) $url = $registration->getAuthTokenUrl(); // Get Access - $response = $this->client->post($url, [ - 'form_params' => $authRequest, - ]); + $request = new ServiceRequest(static::METHOD_POST, $url); + $request->setBody(json_encode($authRequest)); + $response = $this->makeRequest($request); $tokenData = $this->getResponseBody($response); @@ -98,11 +100,32 @@ public function getAccessToken(ILtiRegistration $registration, array $scopes) public function makeRequest(IServiceRequest $request) { - return $this->client->request( + $response = $this->client->request( $request->getMethod(), $request->getUrl(), $request->getPayload() ); + + if ($this->debuggingMode) { + $this->logRequest( + static::AUTH_REQUEST, + $request, + $this->getResponseHeaders($response), + $this->getResponseBody($response) + ); + } + + return $response; + } + + public function getResponseHeaders(Response $response): ?array + { + $responseHeaders = $response->getHeaders(); + array_walk($responseHeaders, function (&$value) { + $value = $value[0]; + }); + + return $responseHeaders; } public function getResponseBody(Response $response): ?array @@ -143,24 +166,10 @@ public function makeServiceRequest( throw $e; } - $responseHeaders = $response->getHeaders(); - array_walk($responseHeaders, function (&$value) { - $value = $value[0]; - }); - $responseBody = $this->getResponseBody($response); - - if ($this->debuggingMode) { - $this->logRequest( - $requestType, - $request, - $responseHeaders, - $responseBody - ); - } return [ - 'headers' => $responseHeaders, - 'body' => $responseBody, + 'headers' => $this->getResponseHeaders($response), + 'body' => $this->getResponseBody($response), 'status' => $response->getStatusCode(), ]; } diff --git a/tests/LtiServiceConnectorTest.php b/tests/LtiServiceConnectorTest.php index 46318ed9..977ec53c 100644 --- a/tests/LtiServiceConnectorTest.php +++ b/tests/LtiServiceConnectorTest.php @@ -96,7 +96,7 @@ public function testItGetsNewAccessToken() $this->cache->shouldReceive('getAccessToken') ->once()->andReturn(false); - $this->client->shouldReceive('post') + $this->client->shouldReceive('request') ->once()->andReturn($this->response); $this->response->shouldReceive('getBody') ->once()->andReturn($this->streamInterface);