1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/gigya-client |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/gigya-client/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/gigya-client |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\Gigya\Auth\OAuth2; |
15
|
|
|
|
16
|
|
|
use DateInterval; |
17
|
|
|
use DateTime; |
18
|
|
|
use Graze\Gigya\Gigya; |
19
|
|
|
use Graze\Gigya\Response\ErrorCode; |
20
|
|
|
|
21
|
|
|
class GigyaGrant implements GrantInterface |
22
|
|
|
{ |
23
|
|
|
/** @var string */ |
24
|
|
|
private $apiKey; |
25
|
|
|
/** @var string */ |
26
|
|
|
private $secret; |
27
|
|
|
/** @var null|string */ |
28
|
|
|
private $userKey; |
29
|
|
|
/** @var AccessToken|null */ |
30
|
|
|
private $token; |
31
|
|
|
/** |
32
|
|
|
* @var Gigya |
33
|
|
|
*/ |
34
|
|
|
private $gigya; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* GigyaGrant constructor. |
38
|
|
|
* |
39
|
|
|
* @param Gigya $gigya |
40
|
|
|
* @param string $apiKey |
41
|
|
|
* @param string $secret |
42
|
|
|
* @param string|null $userKey |
43
|
|
|
*/ |
44
|
6 |
|
public function __construct(Gigya $gigya, $apiKey, $secret, $userKey = null) |
45
|
|
|
{ |
46
|
6 |
|
$this->gigya = $gigya; |
47
|
6 |
|
$this->apiKey = $apiKey; |
48
|
6 |
|
$this->secret = $secret; |
49
|
6 |
|
$this->userKey = $userKey; |
50
|
6 |
|
$this->token = null; |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return AccessToken|null |
55
|
|
|
*/ |
56
|
4 |
|
public function getToken() |
57
|
|
|
{ |
58
|
4 |
|
if (!is_null($this->token) && $this->token->isExpired()) { |
59
|
1 |
|
$this->token = null; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
4 |
|
if (is_null($this->token)) { |
63
|
4 |
|
$response = $this->gigya->socialize()->getToken([ |
64
|
4 |
|
'client_id' => $this->userKey ?: $this->apiKey, |
65
|
4 |
|
'client_secret' => $this->secret, |
66
|
4 |
|
'grant_type' => 'none', |
67
|
4 |
|
], ['auth' => 'none']); |
68
|
4 |
|
if ($response->getErrorCode() == ErrorCode::OK) { |
69
|
3 |
|
$data = $response->getData(); |
70
|
3 |
|
$token = $data->get('access_token'); |
71
|
3 |
|
$expiresIn = $data->get('expires_in', null); |
72
|
3 |
|
$expires = null; |
73
|
3 |
|
if (!is_null($expiresIn)) { |
74
|
2 |
|
$expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', $expiresIn))); |
75
|
2 |
|
} |
76
|
3 |
|
$this->token = new AccessToken($token, $expires); |
77
|
3 |
|
} |
78
|
4 |
|
} |
79
|
|
|
|
80
|
4 |
|
return $this->token; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|