Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACCOUNT-2740] feat: expose hydra token #459

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Alternatively you can still use : [PrestaShop Accounts Installer](http://github.

For detailed usage you can follow the component's documentation : [prestashop_accounts_vue_components](https://github.com/PrestaShopCorp/prestashop_accounts_vue_components)

## How to get up to date (legacy) JWT Tokens
## How to retrieve tokens with PsAccounts

### About tokens provided :

Expand All @@ -91,7 +91,25 @@ This module provides the following tokens:
- **ShopAccessToken** (provided by [Prestashop OpenId Connect Provider](https://oauth.prestashop.com/.well-known/openid-configuration))
For machine to machine calls. (also used to keep up to date legacy Shop and Owner tokens

### Using PsAccountsService (recommended) :
### How to get up-to-date JWT Shop Access Tokens

```php
// /!\ TODO: Starting here you are responsible to check that the module is installed

/** @var Ps_accounts $module */
$module = \Module::getModuleIdByName('ps_accounts');

/** @var \PrestaShop\Module\PsAccounts\Service\PsAccountsService $service */
$service = $module->getService(\PrestaShop\Module\PsAccounts\Service\PsAccountsService::class);

try {
$jwtAccessToken = $service->getShopToken();
} catch (\PrestaShop\Module\PsAccounts\Exception\RefreshTokenException $e) {
//
}
```

### How to get up-to-date (legacy) JWT Tokens
```php
// /!\ TODO: Starting here you are responsible to check that the module is installed

Expand Down Expand Up @@ -135,7 +153,7 @@ $jwtOwner = $service->getUserToken();

[//]: # (```)

### Calling AJAX controller in backend context :
### Calling AJAX controller in backend context (legacy shop token only)
That way you will retrieve an up to date **Shop Token**
```js
const response = await fetch("https://<shop-admin-url>/index.php", {
Expand Down
2 changes: 1 addition & 1 deletion _dev/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ps_accounts",
"version": "7.1.0",
"version": "7.2.0",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_accounts</name>
<displayName><![CDATA[PrestaShop Account]]></displayName>
<version><![CDATA[7.1.0]]></version>
<version><![CDATA[7.2.0]]></version>
<description><![CDATA[Link your store to your PrestaShop account to activate and manage your subscriptions in your back office. Do not uninstall this module if you have a current subscription.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
Expand Down
4 changes: 2 additions & 2 deletions ps_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Ps_accounts extends Module

// Needed in order to retrieve the module version easier (in api call headers) than instanciate
// the module each time to get the version
const VERSION = '7.1.0';
const VERSION = '7.2.0';

/**
* Admin tabs
Expand Down Expand Up @@ -131,7 +131,7 @@ public function __construct()

// We cannot use the const VERSION because the const is not computed by addons marketplace
// when the zip is uploaded
$this->version = '7.1.0';
$this->version = '7.2.0';

$this->module_key = 'abf2cd758b4d629b2944d3922ef9db73';

Expand Down
34 changes: 29 additions & 5 deletions src/Service/PsAccountsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
namespace PrestaShop\Module\PsAccounts\Service;

use PrestaShop\Module\PsAccounts\Account\LinkShop;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase\OwnerSession;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase\ShopSession;
use PrestaShop\Module\PsAccounts\Account\Session\Firebase;
use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Account\Token\Token;
use PrestaShop\Module\PsAccounts\Adapter\Link;
use PrestaShop\Module\PsAccounts\Entity\EmployeeAccount;
use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException;
Expand All @@ -47,10 +48,15 @@ class PsAccountsService
/**
* @var ShopSession
*/
private $session;

/**
* @var Firebase\ShopSession
*/
private $shopSession;

/**
* @var OwnerSession
* @var Firebase\OwnerSession
*/
private $ownerSession;

Expand All @@ -67,8 +73,9 @@ class PsAccountsService
public function __construct(\Ps_accounts $module)
{
$this->module = $module;
$this->shopSession = $this->module->getService(ShopSession::class);
$this->ownerSession = $this->module->getService(OwnerSession::class);
$this->session = $this->module->getService(ShopSession::class);
$this->shopSession = $this->module->getService(Firebase\ShopSession::class);
$this->ownerSession = $this->module->getService(Firebase\OwnerSession::class);
$this->link = $this->module->getService(Link::class);
$this->linkShop = $module->getService(LinkShop::class);
}
Expand Down Expand Up @@ -100,7 +107,12 @@ public function getShopUuid()
}

/**
* Returns a Shop Token from the Legacy Authority: https://securetoken.google.com/prestashop-newsso-production
* and an empty string if any error occurs
*
* @return string
*
* @deprecated please move to hydra tokens as soon as possible
*/
public function getOrRefreshToken()
{
Expand All @@ -127,6 +139,18 @@ public function getToken()
return $this->getOrRefreshToken();
}

/**
* Returns Shop Token with the new authority: https://oauth.prestashop.com
*
* @return string
*
* @throws RefreshTokenException
*/
public function getShopToken()
{
return (string) $this->session->getValidToken();
}

/**
* @return string
*
Expand Down
72 changes: 72 additions & 0 deletions tests/Unit/Service/PsAccountsService/GetShopTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace PrestaShop\Module\PsAccounts\Tests\Unit\Service\PsAccountsService;

use PrestaShop\Module\PsAccounts\Account\Session\ShopSession;
use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Provider\OAuth2\Oauth2Client;
use PrestaShop\Module\PsAccounts\Service\PsAccountsService;
use PrestaShop\Module\PsAccounts\Tests\TestCase;

class GetShopTokenTest extends TestCase
{
/**
* @inject
*
* @var PsAccountsService
*/
protected $service;

/**
* @inject
*
* @var ShopSession
*/
protected $shopSession;

/**
* @inject
*
* @var Oauth2Client
*/
protected $oauthClient;

public function set_up()
{
parent::set_up();

$this->configurationRepository->updateAccessToken('');

// Can't get access token without oauth2client
$this->oauthClient->delete();
}

/**
* @test
*
* @throws \Exception
*/
public function itShouldReturnAValidToken()
{
$validToken = $this->makeJwtToken(new \DateTimeImmutable('+1 hour'));

$this->shopSession->setToken((string) $validToken);

$this->assertEquals($validToken, $this->service->getShopToken());
}

/**
* @test
*
* @throws \Exception
*/
public function itShouldThrowRefreshTokenExceptionOnError()
{
// FIXME: we assume we can't resolve external apis here
$this->shopSession->setToken((string) $this->makeJwtToken(new \DateTimeImmutable('yesterday')));

$this->expectException(RefreshTokenException::class);

$this->service->getShopToken();
}
}
Loading