Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from mediapart/v2.0
Browse files Browse the repository at this point in the history
V2.0
  • Loading branch information
methylbro authored Nov 17, 2017
2 parents 6e2a3ac + 37b1f10 commit 573a998
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/coverage/
/vendor/
/composer.lock
/phpunit.xml
/examples/credentials.txt
41 changes: 41 additions & 0 deletions examples/liaison.php
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);
51 changes: 51 additions & 0 deletions src/Account/Account.php
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;
}
}
121 changes: 121 additions & 0 deletions src/Account/Link.php
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
);
}
}
27 changes: 27 additions & 0 deletions src/Account/Repository.php
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);
}
5 changes: 4 additions & 1 deletion src/Security/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ public function decrypt($message, $options = null)
$options,
$this->iv
);
$result = json_decode(rtrim($result, "\0"), true);
$result = rtrim($result, "\0");

$decodedJson = json_decode($result, true);
$result = null!==$decodedJson ? $decodedJson : $result;

$this->logger->debug('Uncrypting message', [$message, $result]);

Expand Down
39 changes: 39 additions & 0 deletions tests/Account/AccountTest.php
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());
}
}
Loading

0 comments on commit 573a998

Please sign in to comment.