Skip to content

Commit 694ea5b

Browse files
refactor: compat php 5.6 & wellknown (#3)
* refactor: wider php compat * feat : wellknown implementation * refactor: well known not responsible for making http requests
1 parent d3b065a commit 694ea5b

File tree

10 files changed

+311
-55
lines changed

10 files changed

+311
-55
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
/tests/ export-ignore
66
/.gitignore export-ignore
77
/phpunit.xml.dist export-ignore
8-
/README.md export-ignore
8+
#/README.md export-ignore

.github/workflows/php.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@ name: PHP tests
22
on: [push, pull_request]
33
jobs:
44
php-linter:
5-
name: PHP Syntax check 7.2|7.3|8.0|8.1
5+
name: PHP Syntax check 5.6|7.2|8.0|8.1
66
runs-on: ubuntu-latest
77
steps:
88
- name: Checkout
99
uses: actions/checkout@v3
1010

11+
- name: PHP syntax checker 5.6
12+
uses: prestashop/github-action-php-lint/5.6@master
13+
with:
14+
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""
15+
1116
- name: PHP syntax checker 7.2
1217
uses: prestashop/github-action-php-lint/7.2@master
18+
with:
19+
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""
1320

1421
- name: PHP syntax checker 8.0
1522
uses: prestashop/github-action-php-lint/8.0@master
23+
with:
24+
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""
1625

1726
- name: PHP syntax checker 8.1
1827
uses: prestashop/github-action-php-lint/8.1@master
28+
with:
29+
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""
1930

2031
php-cs-fixer:
2132
name: PHP-CS-Fixer

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
This package provides PrestaShop OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).
44

5+
[![Source Code](https://img.shields.io/badge/source-PrestaShopCorp/oauth2--prestashop-blue.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop)
6+
[![Latest Version](https://img.shields.io/github/release/PrestaShopCorp/oauth2-prestashop.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop/releases)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop/blob/main/LICENSE)
8+
[![Build Status](https://img.shields.io/github/actions/workflow/status/PrestaShopCorp/oauth2-prestashop/.github/workflows/php.yml?label=CI&logo=github&style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop/actions?query=workflow%3ACI)
9+
[![Total Downloads](https://img.shields.io/packagist/dt/PrestaShopCorp/oauth2-prestashop.svg?style=flat-square)](https://packagist.org/packages/prestashopcorp/oauth2-prestashop)
10+
11+
---
12+
513
## Installation
614

715
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"authentication"
2121
],
2222
"require": {
23-
"php": ">=7.1",
23+
"php": ">=5.6",
2424
"league/oauth2-client": "^2.0"
2525
},
2626
"require-dev": {

src/Provider/LogoutTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ trait LogoutTrait
1212
/**
1313
* @return string
1414
*/
15-
public function getBaseSessionLogoutUrl(): string
15+
public function getBaseSessionLogoutUrl()
1616
{
17-
return 'https://oauth.prestashop.com/oauth2/sessions/logout';
17+
return $this->getWellKnown()->end_session_endpoint;
1818
}
1919

2020
/**
@@ -26,7 +26,7 @@ public function getBaseSessionLogoutUrl(): string
2626
*
2727
* @throws \Exception
2828
*/
29-
public function getLogoutUrl(array $options = []): string
29+
public function getLogoutUrl(array $options = [])
3030
{
3131
$base = $this->getBaseSessionLogoutUrl();
3232
$params = $this->getLogoutParameters($options);
@@ -42,7 +42,7 @@ public function getLogoutUrl(array $options = []): string
4242
*
4343
* @throws \Exception
4444
*/
45-
protected function getLogoutParameters(array $options): array
45+
protected function getLogoutParameters(array $options)
4646
{
4747
if (empty($options['id_token_hint'])) {
4848
// $options['id_token_hint'] = $this->getSessionAccessToken()->getValues()['id_token'];
@@ -67,7 +67,7 @@ protected function getLogoutParameters(array $options): array
6767
*
6868
* @return string Query string
6969
*/
70-
protected function getLogoutQuery(array $params): string
70+
protected function getLogoutQuery(array $params)
7171
{
7272
return $this->buildQueryString($params);
7373
}

src/Provider/PrestaShop.php

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,111 @@ class PrestaShop extends AbstractProvider
5252
*/
5353
protected $uiLocales;
5454

55+
/**
56+
* @var WellKnown
57+
*/
58+
protected $wellKnown;
59+
60+
/**
61+
* @var bool
62+
*/
63+
protected $verify = true;
64+
65+
/**
66+
* @param array $options
67+
* @param array $collaborators
68+
*
69+
* @throws \Exception
70+
*/
71+
public function __construct(array $options = [], array $collaborators = [])
72+
{
73+
parent::__construct($options, $collaborators);
74+
}
75+
76+
/**
77+
* @return string
78+
*/
79+
public function getOauth2Url()
80+
{
81+
return 'https://oauth.prestashop.com';
82+
}
83+
84+
/**
85+
* @return WellKnown
86+
*/
87+
public function getWellKnown()
88+
{
89+
/* @phpstan-ignore-next-line */
90+
if (!isset($this->wellKnown)) {
91+
try {
92+
$this->wellKnown = new WellKnown(
93+
$this->fetchWellKnown($this->getOauth2Url(), $this->verify)
94+
);
95+
} catch (\Error $e) {
96+
} catch (\Exception $e) {
97+
}
98+
if (isset($e)) {
99+
$this->wellKnown = new WellKnown();
100+
}
101+
}
102+
103+
return $this->wellKnown;
104+
}
105+
106+
/**
107+
* @param string $url
108+
* @param bool $secure
109+
*
110+
* @return array
111+
*
112+
* @throws \Exception
113+
*/
114+
protected function fetchWellKnown($url, $secure = true)
115+
{
116+
$wellKnownUrl = $url;
117+
if (strpos($wellKnownUrl, '/.well-known') === false) {
118+
$wellKnownUrl = preg_replace('/\/?$/', '/.well-known/openid-configuration', $wellKnownUrl);
119+
}
120+
121+
$response = $this->getResponse($this->getRequest('GET', $wellKnownUrl));
122+
123+
return json_decode($response->getBody(), true);
124+
}
125+
55126
/**
56127
* @return string
57128
*/
58-
public function getBaseAuthorizationUrl(): string
129+
public function getBaseAuthorizationUrl()
59130
{
60-
return 'https://oauth.prestashop.com/oauth2/auth';
131+
return $this->getWellKnown()->authorization_endpoint;
61132
}
62133

63134
/**
64135
* @param array $params
65136
*
66137
* @return string
67138
*/
68-
public function getBaseAccessTokenUrl(array $params): string
139+
public function getBaseAccessTokenUrl(array $params)
69140
{
70-
return 'https://oauth.prestashop.com/oauth2/token';
141+
return $this->getWellKnown()->token_endpoint;
71142
}
72143

73144
/**
74145
* @param AccessToken $token
75146
*
76147
* @return string
77148
*/
78-
public function getResourceOwnerDetailsUrl(AccessToken $token): string
149+
public function getResourceOwnerDetailsUrl(AccessToken $token)
79150
{
80-
return 'https://oauth.prestashop.com/userinfo';
151+
return $this->getWellKnown()->userinfo_endpoint;
81152
}
82153

83154
/**
84155
* @param array $options
85156
*
86157
* @return string[]
87158
*/
88-
protected function getAuthorizationParameters(array $options): array
159+
protected function getAuthorizationParameters(array $options)
89160
{
90161
if (empty($options['prompt']) && $this->prompt) {
91162
$options['prompt'] = $this->prompt;
@@ -107,15 +178,15 @@ protected function getAuthorizationParameters(array $options): array
107178
/**
108179
* @return string[]
109180
*/
110-
public function getDefaultScopes(): array
181+
public function getDefaultScopes()
111182
{
112183
return ['openid', 'offline_access'];
113184
}
114185

115186
/**
116187
* @return string
117188
*/
118-
protected function getScopeSeparator(): string
189+
protected function getScopeSeparator()
119190
{
120191
return ' ';
121192
}
@@ -128,13 +199,13 @@ protected function getScopeSeparator(): string
128199
*
129200
* @throws IdentityProviderException
130201
*/
131-
protected function checkResponse(ResponseInterface $response, $data): void
202+
protected function checkResponse(ResponseInterface $response, $data)
132203
{
133204
if ($response->getStatusCode() !== 200) {
134205
$errorDescription = '';
135206
$error = '';
136207
if (\is_array($data) && !empty($data)) {
137-
$errorDescription = $data['error_description'] ?? $data['message'];
208+
$errorDescription = isset($data['error_description']) ? $data['error_description'] : $data['message'];
138209
$error = $data['error'];
139210
}
140211
throw new IdentityProviderException(sprintf('%d - %s: %s', $response->getStatusCode(), $error, $errorDescription), $response->getStatusCode(), $data);
@@ -147,7 +218,7 @@ protected function checkResponse(ResponseInterface $response, $data): void
147218
*
148219
* @return PrestaShopUser
149220
*/
150-
protected function createResourceOwner(array $response, AccessToken $token): PrestaShopUser
221+
protected function createResourceOwner(array $response, AccessToken $token)
151222
{
152223
return new PrestaShopUser($response);
153224
}
@@ -159,7 +230,7 @@ protected function createResourceOwner(array $response, AccessToken $token): Pre
159230
*
160231
* @return PrestaShopUser
161232
*/
162-
public function getResourceOwner(AccessToken $token): PrestaShopUser
233+
public function getResourceOwner(AccessToken $token)
163234
{
164235
/** @var PrestaShopUser $resourceOwner */
165236
$resourceOwner = parent::getResourceOwner($token);

0 commit comments

Comments
 (0)