This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mediapart/v2.0
V2.0
- Loading branch information
Showing
9 changed files
with
449 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/coverage/ | ||
/vendor/ | ||
/composer.lock | ||
/phpunit.xml | ||
/examples/credentials.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* Liaison de compte utilisateur par redirection. | ||
* | ||
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Liaison-de-compte-utilisateur-par-redirection | ||
*/ | ||
|
||
require 'bootstrap.php'; | ||
|
||
use Mediapart\LaPresseLibre\Account\Liaison; | ||
use Mediapart\LaPresseLibre\Account\Account; | ||
use Mediapart\LaPresseLibre\Account\Repository; | ||
|
||
class MemoryRepository implements Repository | ||
{ | ||
private $accounts = []; | ||
public function __construct($accounts) | ||
{ | ||
array_map([$this, 'save'], $accounts); | ||
} | ||
public function find($code) | ||
{ | ||
return $this->accounts[$code]; | ||
} | ||
public function save(Account $account) | ||
{ | ||
$this->accounts[$account->getCode()] = $account; | ||
} | ||
} | ||
|
||
$repository = new MemoryRepository([ | ||
new Account('[email protected]', '99f104e8-2fa3-4a77-1664-5bac75fb668d'), | ||
new Account('[email protected]', '68b3c837-c7f4-1b54-2efa-1c5cc2945c3f'), | ||
]); | ||
$logguedAccount = new Account('[email protected]', '7f75e972-d5c7-b0c5-1a1b-9d5a582cbd27'); | ||
|
||
$liaison = new Liaison($encryption, $repository, $public_key); | ||
$redirection = $liaison->generateUrl($_GET['lpluser'], $logguedAccount); | ||
|
||
header('Location: '.$redirection); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Mediapart LaPresseLibre Library. | ||
* | ||
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> | ||
* | ||
* For the full license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Mediapart\LaPresseLibre\Account; | ||
|
||
class Account | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $code; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $email; | ||
|
||
/** | ||
* @param string $email | ||
* @param string $code | ||
*/ | ||
public function __construct($email, $code = null) | ||
{ | ||
$this->code = $code; | ||
$this->email = $email; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getCode() | ||
{ | ||
return $this->code; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getEmail() | ||
{ | ||
return $this->email; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Mediapart LaPresseLibre Library. | ||
* | ||
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> | ||
* | ||
* For the full license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Mediapart\LaPresseLibre\Account; | ||
|
||
use Mediapart\LaPresseLibre\Security\Encryption; | ||
use Mediapart\LaPresseLibre\Account\Account; | ||
use Mediapart\LaPresseLibre\Account\Repository; | ||
|
||
/** | ||
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Liaison-de-compte-utilisateur-par-redirection | ||
*/ | ||
class Link | ||
{ | ||
const RETURN_URL = 'https://beta.lapresselibre.fr/manage/link-result?lpl=%1$s&part=%2$s'; | ||
const STATUS_SUCCESS = 1; | ||
const STATUS_FAILURE = 2; | ||
const STATUS_CONFLICT = 3; | ||
|
||
/** | ||
* @var Encryption | ||
*/ | ||
private $encryption; | ||
|
||
/** | ||
* @var Repository | ||
*/ | ||
private $repository; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $public_key; | ||
|
||
/** | ||
* @param Encryption $encryption | ||
* @param Repository $repository | ||
* @param int $public_key | ||
*/ | ||
public function __construct(Encryption $encryption, Repository $repository, $public_key) | ||
{ | ||
$this->encryption = $encryption; | ||
$this->repository = $repository; | ||
$this->public_key = $public_key; | ||
} | ||
|
||
/** | ||
* Liaison de compte utilisateur par redirection | ||
* | ||
* @param string $lplUser | ||
* @param Account $logguedAccount | ||
* @return string | ||
*/ | ||
public function generate($lplUser, Account $logguedAccount) | ||
{ | ||
/* Le paramètre "lpluser" représente l'ID LPL de l'utilisateur qui | ||
souhaite lier son compte. Il est chiffré en AES256 puis codé en | ||
base64 en reprenant la méthode de chiffrement utilisée pour les | ||
web services. */ | ||
$code = $this->encryption->decrypt($lplUser); | ||
|
||
if ($existingAccount = $this->repository->find($code)) { | ||
|
||
/* En cas de conflit la valeur du statut que le partenaire doit | ||
retourner sera "3". Sauf évidement s'il s'agit du bon compte | ||
utilisateur. */ | ||
$status = ($existingAccount != $logguedAccount) | ||
? self::STATUS_CONFLICT | ||
: self::STATUS_SUCCESS | ||
; | ||
|
||
} else { | ||
try { | ||
|
||
/* Si l'ID LPL reçu n'est pas déjà présent, le partenaire | ||
doit rechercher le compte utilisateur pour y rattacher | ||
L'ID LPL. Puis on retourne un statut "1" pour indiquer | ||
que la liaison s'est effectuée avec succès. */ | ||
$account = new Account($logguedAccount->getEmail(), $code); | ||
$this->repository->save($account); | ||
$status = self::STATUS_SUCCESS; | ||
|
||
} catch (\Exception $e) { | ||
|
||
/* Le statut retourné par le partenaire LPL est "2" en cas | ||
d'erreur. */ | ||
$status = self::STATUS_FAILURE; | ||
} | ||
} | ||
|
||
/* Le partenaire doit rediriger l'utilisateur vers l'url fournie par | ||
LPL avec les paramètres : */ | ||
return sprintf( | ||
self::RETURN_URL, | ||
|
||
/* "lpl" : composé de l'ID LPL et du statut. Ce paramètre sera | ||
ensuite chiffré en AES puis codé en base64. | ||
Exemple : { Guid: xxxx, statut: 1 } */ | ||
rawurlencode( | ||
$this->encryption->encrypt( | ||
[ | ||
'Guid' => $code, | ||
'statut' => $status, | ||
], | ||
OPENSSL_RAW_DATA & OPENSSL_NO_PADDING | ||
) | ||
), | ||
|
||
/* "part" : qui représente le code du partenaire. */ | ||
$this->public_key | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Mediapart LaPresseLibre Library. | ||
* | ||
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> | ||
* | ||
* For the full license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Mediapart\LaPresseLibre\Account; | ||
|
||
interface Repository | ||
{ | ||
/** | ||
* @param string $code | ||
* @return Account|null | ||
*/ | ||
public function find($code); | ||
|
||
/** | ||
* @param Account $account | ||
* @return void | ||
*/ | ||
public function save(Account $account); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Mediapart LaPresseLibre Library. | ||
* | ||
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> | ||
* | ||
* For the full license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Mediapart\LaPresseLibre\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Mediapart\LaPresseLibre\Account\Account; | ||
|
||
class AccountTest extends TestCase | ||
{ | ||
public function testAccountWithoutCode() | ||
{ | ||
$email = '[email protected]'; | ||
|
||
$account = new Account($email); | ||
|
||
$this->assertEquals($email, $account->getEmail()); | ||
$this->assertNull($account->getCode()); | ||
} | ||
|
||
public function testAccountWithCode() | ||
{ | ||
$email = '[email protected]'; | ||
$lplCode = '68b3c837-c7f4-1b54-2efa-1c5cc2945c3f'; | ||
|
||
$account = new Account($email, $lplCode); | ||
|
||
$this->assertEquals($email, $account->getEmail()); | ||
$this->assertEquals($lplCode, $account->getCode()); | ||
} | ||
} |
Oops, something went wrong.