Skip to content

Commit

Permalink
refactor: compat php 5.6 & wellknown (#3)
Browse files Browse the repository at this point in the history
* refactor: wider php compat

* feat : wellknown implementation

* refactor: well known not responsible for making http requests
  • Loading branch information
hschoenenberger authored May 22, 2024
1 parent d3b065a commit 694ea5b
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/tests/ export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/README.md export-ignore
#/README.md export-ignore
13 changes: 12 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ name: PHP tests
on: [push, pull_request]
jobs:
php-linter:
name: PHP Syntax check 7.2|7.3|8.0|8.1
name: PHP Syntax check 5.6|7.2|8.0|8.1
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: PHP syntax checker 5.6
uses: prestashop/github-action-php-lint/5.6@master
with:
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""

- name: PHP syntax checker 7.2
uses: prestashop/github-action-php-lint/7.2@master
with:
folder-to-exclude: "! -path \"./vendor/*\" ! -path \"./tests/*\""

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

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

php-cs-fixer:
name: PHP-CS-Fixer
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

[![Source Code](https://img.shields.io/badge/source-PrestaShopCorp/oauth2--prestashop-blue.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop)
[![Latest Version](https://img.shields.io/github/release/PrestaShopCorp/oauth2-prestashop.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/PrestaShopCorp/oauth2-prestashop/blob/main/LICENSE)
[![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)
[![Total Downloads](https://img.shields.io/packagist/dt/PrestaShopCorp/oauth2-prestashop.svg?style=flat-square)](https://packagist.org/packages/prestashopcorp/oauth2-prestashop)

---

## Installation

```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"authentication"
],
"require": {
"php": ">=7.1",
"php": ">=5.6",
"league/oauth2-client": "^2.0"
},
"require-dev": {
Expand Down
10 changes: 5 additions & 5 deletions src/Provider/LogoutTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ trait LogoutTrait
/**
* @return string
*/
public function getBaseSessionLogoutUrl(): string
public function getBaseSessionLogoutUrl()
{
return 'https://oauth.prestashop.com/oauth2/sessions/logout';
return $this->getWellKnown()->end_session_endpoint;
}

/**
Expand All @@ -26,7 +26,7 @@ public function getBaseSessionLogoutUrl(): string
*
* @throws \Exception
*/
public function getLogoutUrl(array $options = []): string
public function getLogoutUrl(array $options = [])
{
$base = $this->getBaseSessionLogoutUrl();
$params = $this->getLogoutParameters($options);
Expand All @@ -42,7 +42,7 @@ public function getLogoutUrl(array $options = []): string
*
* @throws \Exception
*/
protected function getLogoutParameters(array $options): array
protected function getLogoutParameters(array $options)
{
if (empty($options['id_token_hint'])) {
// $options['id_token_hint'] = $this->getSessionAccessToken()->getValues()['id_token'];
Expand All @@ -67,7 +67,7 @@ protected function getLogoutParameters(array $options): array
*
* @return string Query string
*/
protected function getLogoutQuery(array $params): string
protected function getLogoutQuery(array $params)
{
return $this->buildQueryString($params);
}
Expand Down
97 changes: 84 additions & 13 deletions src/Provider/PrestaShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,111 @@ class PrestaShop extends AbstractProvider
*/
protected $uiLocales;

/**
* @var WellKnown
*/
protected $wellKnown;

/**
* @var bool
*/
protected $verify = true;

/**
* @param array $options
* @param array $collaborators
*
* @throws \Exception
*/
public function __construct(array $options = [], array $collaborators = [])
{
parent::__construct($options, $collaborators);
}

/**
* @return string
*/
public function getOauth2Url()
{
return 'https://oauth.prestashop.com';
}

/**
* @return WellKnown
*/
public function getWellKnown()
{
/* @phpstan-ignore-next-line */
if (!isset($this->wellKnown)) {
try {
$this->wellKnown = new WellKnown(
$this->fetchWellKnown($this->getOauth2Url(), $this->verify)
);
} catch (\Error $e) {
} catch (\Exception $e) {
}
if (isset($e)) {
$this->wellKnown = new WellKnown();
}
}

return $this->wellKnown;
}

/**
* @param string $url
* @param bool $secure
*
* @return array
*
* @throws \Exception
*/
protected function fetchWellKnown($url, $secure = true)
{
$wellKnownUrl = $url;
if (strpos($wellKnownUrl, '/.well-known') === false) {
$wellKnownUrl = preg_replace('/\/?$/', '/.well-known/openid-configuration', $wellKnownUrl);
}

$response = $this->getResponse($this->getRequest('GET', $wellKnownUrl));

return json_decode($response->getBody(), true);
}

/**
* @return string
*/
public function getBaseAuthorizationUrl(): string
public function getBaseAuthorizationUrl()
{
return 'https://oauth.prestashop.com/oauth2/auth';
return $this->getWellKnown()->authorization_endpoint;
}

/**
* @param array $params
*
* @return string
*/
public function getBaseAccessTokenUrl(array $params): string
public function getBaseAccessTokenUrl(array $params)
{
return 'https://oauth.prestashop.com/oauth2/token';
return $this->getWellKnown()->token_endpoint;
}

/**
* @param AccessToken $token
*
* @return string
*/
public function getResourceOwnerDetailsUrl(AccessToken $token): string
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return 'https://oauth.prestashop.com/userinfo';
return $this->getWellKnown()->userinfo_endpoint;
}

/**
* @param array $options
*
* @return string[]
*/
protected function getAuthorizationParameters(array $options): array
protected function getAuthorizationParameters(array $options)
{
if (empty($options['prompt']) && $this->prompt) {
$options['prompt'] = $this->prompt;
Expand All @@ -107,15 +178,15 @@ protected function getAuthorizationParameters(array $options): array
/**
* @return string[]
*/
public function getDefaultScopes(): array
public function getDefaultScopes()
{
return ['openid', 'offline_access'];
}

/**
* @return string
*/
protected function getScopeSeparator(): string
protected function getScopeSeparator()
{
return ' ';
}
Expand All @@ -128,13 +199,13 @@ protected function getScopeSeparator(): string
*
* @throws IdentityProviderException
*/
protected function checkResponse(ResponseInterface $response, $data): void
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() !== 200) {
$errorDescription = '';
$error = '';
if (\is_array($data) && !empty($data)) {
$errorDescription = $data['error_description'] ?? $data['message'];
$errorDescription = isset($data['error_description']) ? $data['error_description'] : $data['message'];
$error = $data['error'];
}
throw new IdentityProviderException(sprintf('%d - %s: %s', $response->getStatusCode(), $error, $errorDescription), $response->getStatusCode(), $data);
Expand All @@ -147,7 +218,7 @@ protected function checkResponse(ResponseInterface $response, $data): void
*
* @return PrestaShopUser
*/
protected function createResourceOwner(array $response, AccessToken $token): PrestaShopUser
protected function createResourceOwner(array $response, AccessToken $token)
{
return new PrestaShopUser($response);
}
Expand All @@ -159,7 +230,7 @@ protected function createResourceOwner(array $response, AccessToken $token): Pre
*
* @return PrestaShopUser
*/
public function getResourceOwner(AccessToken $token): PrestaShopUser
public function getResourceOwner(AccessToken $token)
{
/** @var PrestaShopUser $resourceOwner */
$resourceOwner = parent::getResourceOwner($token);
Expand Down
Loading

0 comments on commit 694ea5b

Please sign in to comment.