|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Mediapart LaPresseLibre Library. |
|
5
|
|
|
* |
|
6
|
|
|
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Mediapart\LaPresseLibre\Security; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Used to sign documents has described in the API specifications. |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Fonctionnement-des-web-services#g%C3%A9n%C3%A9ralit%C3%A9s |
|
21
|
|
|
*/ |
|
22
|
|
|
class Identity implements LoggerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use \Psr\Log\LoggerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string Name of the hashing algorithm |
|
28
|
|
|
*/ |
|
29
|
|
|
const HASH_ALGORITHM = 'SHA1'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string Pattern used to concatenate public/secret key and timestamp |
|
33
|
|
|
*/ |
|
34
|
|
|
const SIGNATURE_PATTERN = '%s+%s+%s'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $secret_key; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \Datetime |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $datetime; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $secret_key |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct($secret_key) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->secret_key = $secret_key; |
|
52
|
|
|
$this->datetime = new \DateTime(); |
|
53
|
|
|
$this->logger = new NullLogger(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Generate a signature based on your private key. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $public_key |
|
60
|
|
|
* @param int $timestamp |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function sign($public_key, $timestamp = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$timestamp = $timestamp ? $timestamp : $this->datetime->getTimestamp(); |
|
67
|
|
|
$signchain = sprintf(self::SIGNATURE_PATTERN, $public_key, $timestamp, $this->secret_key); |
|
68
|
|
|
$signature = hash(self::HASH_ALGORITHM, $signchain); |
|
69
|
|
|
|
|
70
|
|
|
$this->logger->debug( |
|
71
|
|
|
'Generated signature', |
|
72
|
|
|
[$public_key, $timestamp, $signature] |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return $signature; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return \Datetime |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getDatetime() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->datetime; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|