Skip to content
Merged
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
language: php

dist: trusty
sudo: required

php:
- 5.4
- 5.5
- 5.6
- 7.1
- 7.2
- 7.3
- 7.4

env: TEST_PARAMS='-c tests/php.ini'

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
}
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "^5.3"
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"tracy/tracy": "^2.4",
"nette/tester": "^1.7",
"mockery/mockery": "^0.9.9",
"mockery/mockery": "^1.3",
"latte/latte": "^2.4"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion examples/clientApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require __DIR__ . '/bootstrap.php';

use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Psr7\Request;
use SizeID\OAuth2\ClientApi;


Expand Down
2 changes: 1 addition & 1 deletion examples/popupLogin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
require __DIR__ . '/bootstrap.php';
use GuzzleHttp\Message\Request;
use GuzzleHttp\Psr7\Request;
use Latte\Engine;
use SizeID\OAuth2\Exceptions\RedirectException;
use SizeID\OAuth2\Repositories\SessionAccessTokenRepository;
Expand Down
2 changes: 1 addition & 1 deletion examples/userApi.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require __DIR__ . '/bootstrap.php';
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Psr7\Request;
use SizeID\OAuth2\Exceptions\RedirectException;
use SizeID\OAuth2\UserApi;

Expand Down
29 changes: 16 additions & 13 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SizeID\OAuth2\Entities\AccessToken;
use SizeID\OAuth2\Exceptions\InvalidStateException;
use SizeID\OAuth2\Repositories\AccessTokenRepositoryInterface;
use GuzzleHttp\RequestOptions;

/**
* Shared functionality of API calls
Expand Down Expand Up @@ -59,19 +61,22 @@ public function __construct(
$authorizationServerUrl,
$apiBaseUrl,
$httpClient
)
{
) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->accessTokenRepository = $accessTokenRepository;
if ($authorizationServerUrl === NULL) {
if ($authorizationServerUrl === null) {
$authorizationServerUrl = Config::AUTHORIZATION_SERVER_URL;
}
if ($apiBaseUrl === NULL) {
if ($apiBaseUrl === null) {
$apiBaseUrl = Config::API_URL;
}
if ($httpClient === NULL) {
$httpClient = new Client();
if ($httpClient === null) {
$httpClient = new Client(
[
RequestOptions::HTTP_ERRORS => false,
]
);
}
$this->authorizationServerUrl = $authorizationServerUrl;
$this->apiBaseUrl = $apiBaseUrl;
Expand Down Expand Up @@ -154,7 +159,7 @@ protected function parseToken(ResponseInterface $response)
private function createResponse(RequestInterface $request)
{
$response = $this->callApi($this->buildRequest($request));
if ($response->getStatusCode() === 401 && $response->getHeader(self::SIZEID_ERROR_CODE_HEADER) == 109) {
if ($response->getStatusCode() === 401 && $response->getHeaderLine(self::SIZEID_ERROR_CODE_HEADER) == 109) {
$this->refreshAccessToken();
return $this->callApi($this->buildRequest($request));
}
Expand All @@ -176,9 +181,7 @@ private function callApi(RequestInterface $request)
*/
private function buildRequest(RequestInterface $request)
{
$request = clone $request;
$request->addHeader('Authorization', 'Bearer ' . $this->getAccessToken()->getAccessToken());
$request->setUrl($this->apiBaseUrl . '/' . $request->getUrl());
return $request;
return $request->withAddedHeader('Authorization', 'Bearer ' . $this->getAccessToken()->getAccessToken())
->withUri(new Uri($this->apiBaseUrl . '/' . $request->getUri()));
}
}
2 changes: 1 addition & 1 deletion src/ClientApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function acquireNewAccessToken()
$response = $this->httpClient->post(
$this->authorizationServerUrl . '/access-token',
[
'body' => [
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
Expand Down
24 changes: 11 additions & 13 deletions src/UserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Url;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;
use SizeID\OAuth2\Entities\AccessToken;
use SizeID\OAuth2\Exceptions\InvalidCSRFTokenException;
use SizeID\OAuth2\Exceptions\InvalidStateException;
Expand Down Expand Up @@ -69,16 +70,13 @@ public function __construct(
*/
public function getAuthorizationUrl()
{
$url = Url::fromString($this->authorizationServerUrl);
$url->setQuery(
[
'response_type' => 'code',
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'state' => $this->csrfTokenRepository->generateCSRFToken(),
]
);
return $url;
$url = new Uri($this->authorizationServerUrl);
return Uri::withQueryValues($url, [
'response_type' => 'code',
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'state' => $this->csrfTokenRepository->generateCSRFToken(),
]);
}

/**
Expand Down Expand Up @@ -149,7 +147,7 @@ public function refreshAccessToken()
$this->saveTokenFromResponse($response);
} catch (ClientException $ex) {
$response = $ex->getResponse();
$sizeIdErrorCode = (int)$response->getHeader(self::SIZEID_ERROR_CODE_HEADER);
$sizeIdErrorCode = (int)$response->getHeaderLine(self::SIZEID_ERROR_CODE_HEADER);
if ($response->getStatusCode() === 400 && $sizeIdErrorCode === 108) {
//refresh token expired
throw RedirectException::create(
Expand Down
6 changes: 3 additions & 3 deletions tests/ApiTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace SizeID\OAuth2\Tests;

use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mockery as m;
use SizeID\OAuth2\ClientApi;
use SizeID\OAuth2\Entities\AccessToken;
Expand All @@ -20,7 +20,7 @@ class ApiTest extends TestCase
$tokenRepository = m::mock('SizeID\OAuth2\Repositories\SessionAccessTokenRepository');
$tokenRepository
->shouldReceive('hasAccessToken')
->andReturn(NULL);
->andReturn(null);
$clientApi = new ClientApi(
'clientId',
'clientSecret',
Expand Down
20 changes: 11 additions & 9 deletions tests/ClientApiTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace SizeID\OAuth2\Tests;

use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mockery as m;
use Psr\Http\Message\StreamInterface;
use SizeID\OAuth2\Api;
use SizeID\OAuth2\ClientApi;
use SizeID\OAuth2\Entities\AccessToken;
use SizeID\OAuth2\Repositories\SessionAccessTokenRepository;
use Tester\Assert;
use Tester\TestCase;

Expand All @@ -27,7 +29,7 @@ class ClientApiTest extends TestCase

public function testAcquireToken()
{
$tokenRepository = m::mock('SizeID\OAuth2\Repositories\SessionAccessTokenRepository');
$tokenRepository = m::mock(SessionAccessTokenRepository::class);
$tokenRepository
->shouldReceive('hasAccessToken')
->andReturn(FALSE);
Expand All @@ -38,7 +40,7 @@ class ClientApiTest extends TestCase
->shouldReceive('getAccessToken')
->andReturn($accessToken);
$httpClient = m::mock('GuzzleHttp\Client');
$stream = m::mock('GuzzleHttp\Stream\StreamInterface');
$stream = m::mock(StreamInterface::class);
$stream
->shouldReceive('getContents')
->andReturn('{"access_token":"token", "expires_in": 60}');
Expand All @@ -57,13 +59,13 @@ class ClientApiTest extends TestCase
NULL,
$httpClient
);
Assert::type('SizeID\OAuth2\ClientApi', $clientApi);
Assert::type(ClientApi::class, $clientApi);
$clientApi->send(new Request('get', 'client'));
}

public function testRefreshToken()
{
$tokenRepository = m::mock('SizeID\OAuth2\Repositories\SessionAccessTokenRepository');
$tokenRepository = m::mock(SessionAccessTokenRepository::class);
$tokenRepository
->shouldReceive('hasAccessToken')
->andReturn(TRUE);
Expand All @@ -78,7 +80,7 @@ class ClientApiTest extends TestCase
$httpClient
->shouldReceive('send')
->andReturn($response);
$stream = m::mock('GuzzleHttp\Stream\StreamInterface');
$stream = m::mock(StreamInterface::class);
$stream
->shouldReceive('getContents')
->andReturn('{"access_token":"token", "expires_in": 60}');
Expand All @@ -97,8 +99,8 @@ class ClientApiTest extends TestCase
NULL,
$httpClient
);
Assert::type('SizeID\OAuth2\ClientApi', $clientApi);
Assert::type('GuzzleHttp\Message\Response', $clientApi->send(new Request('POST', 'client')));
Assert::type(ClientApi::class, $clientApi);
Assert::type(Response::class, $clientApi->send(new Request('POST', 'client')));
}
}

Expand Down
Loading