1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Api; |
4
|
|
|
|
5
|
|
|
class Token |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The Client instance. |
9
|
|
|
* |
10
|
|
|
* @var \ElfSundae\Laravel\Api\Client |
11
|
|
|
*/ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
* |
17
|
|
|
* @param \ElfSundae\Laravel\Api\Client $client |
18
|
|
|
*/ |
19
|
|
|
public function __construct(Client $client) |
20
|
|
|
{ |
21
|
|
|
$this->client = $client; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the Client instance. |
26
|
|
|
* |
27
|
|
|
* @return \ElfSundae\Laravel\Api\Client |
28
|
|
|
*/ |
29
|
|
|
public function getClient() |
30
|
|
|
{ |
31
|
|
|
return $this->client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the Client instance. |
36
|
|
|
* |
37
|
|
|
* @param \ElfSundae\Laravel\Api\Client $client |
38
|
|
|
*/ |
39
|
|
|
public function setClient(Client $client) |
40
|
|
|
{ |
41
|
|
|
$this->client = $client; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generate an api token. |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* @param string $secret |
49
|
|
|
* @param string|int $time |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function generate($key, $secret, $time) |
53
|
|
|
{ |
54
|
|
|
return substr(md5($key.$secret.$time), 10, 20); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate an api token for the given app key. |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* @param string|int $time |
62
|
|
|
* @return string|null |
63
|
|
|
*/ |
64
|
|
|
public function generateForKey($key, $time) |
65
|
|
|
{ |
66
|
|
|
if ($secret = $this->client->getAppSecretForKey($key)) { |
67
|
|
|
return $this->generate($key, $secret, $time); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Verify an api token. |
73
|
|
|
* |
74
|
|
|
* @param string $token |
75
|
|
|
* @param string $key |
76
|
|
|
* @param string|int $time |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function verify($token, $key, $time) |
80
|
|
|
{ |
81
|
|
|
return $token && $key && $time && $token === $this->generateForKey($key, $time); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate a token data array. |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* @param string $secret |
89
|
|
|
* @param string|int|null $time |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function generateData($key, $secret, $time = null) |
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
'key' => (string) $key, |
96
|
|
|
'time' => $time = $time ?: time(), |
97
|
|
|
'token' => $this->generate($key, $secret, $time), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Generate a token data array. |
103
|
|
|
* |
104
|
|
|
* @param string $key |
105
|
|
|
* @param string|int|null $time |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function generateDataForKey($key, $time = null) |
109
|
|
|
{ |
110
|
|
|
if ($secret = $this->client->getAppSecretForKey($key)) { |
111
|
|
|
return $this->generateData($key, $secret, $time); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|