1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Paquet Judicaël <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace judicaelpaquet\IzbergBundle\Service; |
8
|
|
|
|
9
|
|
|
use Guzzle\Http\Client; |
10
|
|
|
use Guzzle\Http\Message\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class IzbergConnector |
14
|
|
|
* @package judicaelpaquet\IzbergBundle\Service |
15
|
|
|
*/ |
16
|
|
|
class IzbergConnector |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $izbergUrl; |
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $httpClient; |
26
|
|
|
/** |
27
|
|
|
* @var \Predis\Client |
28
|
|
|
*/ |
29
|
|
|
private $redis; |
30
|
|
|
/** |
31
|
|
|
* @var boolean |
32
|
|
|
*/ |
33
|
|
|
private $force = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* IzbergConnector constructor. |
37
|
|
|
* @param string $izbergUrl |
38
|
|
|
* @param Client $httpClient |
39
|
|
|
* @param \Predis\Client $redis |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $izbergUrl, Client $httpClient, \Predis\Client $redis) |
42
|
|
|
{ |
43
|
|
|
$this->izbergUrl = $izbergUrl; |
44
|
|
|
$this->httpClient = $httpClient; |
45
|
|
|
$this->redis = $redis; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $email |
50
|
|
|
* @param string $firstName |
51
|
|
|
* @param string $lastName |
52
|
|
|
* @param string $secretKey |
53
|
|
|
*/ |
54
|
|
|
public function connect(string $email = '', string $firstName = '', string $lastName = '', string $secretKey = '') |
55
|
|
|
{ |
56
|
|
|
// If the izberg_sso key already exists in redis, we just have to exit. |
57
|
|
|
|
58
|
|
|
if ($this->getAuthentication() && $this->force == false) { |
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Otherwise, we have to use izberg url to generate a new authentication. |
63
|
|
|
if ($this->getAuthentication() && !$this->force) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Otherwise, we have to izberg url to generate a new authentication. |
68
|
|
|
$request = $this->httpClient->get( |
69
|
|
|
$this->generateUrl($email, $firstName, $lastName, $secretKey) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
if ($response = $request->send()) { |
73
|
|
|
$this->setAuthentication($response); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $email |
79
|
|
|
* @param string $firstName |
80
|
|
|
* @param string $lastName |
81
|
|
|
* @param string $secretKey |
82
|
|
|
*/ |
83
|
|
|
public function forceAuthAndConnect( |
84
|
|
|
string $email = '', |
85
|
|
|
string $firstName = '', |
86
|
|
|
string $lastName = '', |
87
|
|
|
string $secretKey = '' |
88
|
|
|
) { |
89
|
|
|
$this->setForce(true); |
90
|
|
|
$this->connect($email, $firstName, $lastName, $secretKey); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $email |
95
|
|
|
* @param string $firstName |
96
|
|
|
* @param string $lastName |
97
|
|
|
* @param string $secretKey |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function generateUrl( |
101
|
|
|
string $email = '', |
102
|
|
|
string $firstName = '', |
103
|
|
|
string $lastName = '', |
104
|
|
|
string $secretKey = '' |
105
|
|
|
):string |
106
|
|
|
{ |
107
|
|
|
$timestamp = time(); |
108
|
|
|
$toCompose = [$email, $firstName, $lastName, $timestamp]; |
109
|
|
|
$message_auth = hash_hmac('sha1', implode(";", $toCompose), $secretKey); |
110
|
|
|
|
111
|
|
|
$url = $this->izbergUrl; |
112
|
|
|
$url .= 'first_name=' . urlencode($firstName) . '&last_name=' . urlencode($lastName) . '&message_auth=' . $message_auth . '&email=' . urlencode($email); |
113
|
|
|
$url .= '×tamp=' . $timestamp . '&is_staff=true'; |
114
|
|
|
return $url; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* <<<<<<< HEAD |
119
|
|
|
* @param bool $force |
120
|
|
|
*/ |
121
|
|
|
public function setForce(bool $force) |
122
|
|
|
{ |
123
|
|
|
$this->force = $force; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function getForce():bool |
130
|
|
|
{ |
131
|
|
|
return $this->force; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Response $response |
136
|
|
|
*/ |
137
|
|
|
public function setAuthentication(Response $response) |
138
|
|
|
{ |
139
|
|
|
$this->redis->set('izberg_sso', json_encode( |
140
|
|
|
json_decode($response->getBody(), true) |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function getAuthentication() |
149
|
|
|
{ |
150
|
|
|
return json_decode($this->redis->get('izberg_sso'), true); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.