|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\AdForm; |
|
4
|
|
|
|
|
5
|
|
|
use Audiens\AdForm\Exception\OauthException; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
8
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
|
9
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
10
|
|
|
|
|
11
|
|
|
class Authentication |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var AccessToken|null */ |
|
14
|
|
|
protected $accessToken; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $username; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $password; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(string $username, string $password) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->username = $username; |
|
25
|
|
|
$this->password = $password; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Authenticate on AdForm API using the password grant |
|
30
|
|
|
* |
|
31
|
|
|
* @throws OauthException if authentication fails |
|
32
|
|
|
*/ |
|
33
|
|
|
public function authenticate(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$urlAccessToken = Client::BASE_URL.'/v1/token'; |
|
36
|
|
|
|
|
37
|
|
|
// we are using a very simple password grant |
|
38
|
|
|
// AdForm doesn't even return a Refresh Token |
|
39
|
|
|
$provider = new GenericProvider( |
|
40
|
|
|
[ |
|
41
|
|
|
'clientId' => '', |
|
42
|
|
|
'clientSecret' => '', |
|
43
|
|
|
'redirectUri' => '', |
|
44
|
|
|
'urlAuthorize' => '', |
|
45
|
|
|
'urlAccessToken' => $urlAccessToken, |
|
46
|
|
|
'urlResourceOwnerDetails' => '', |
|
47
|
|
|
] |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$this->accessToken = $provider->getAccessToken( |
|
52
|
|
|
'password', |
|
53
|
|
|
[ |
|
54
|
|
|
'username' => $this->username, |
|
55
|
|
|
'password' => $this->password, |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} catch (IdentityProviderException $e) { |
|
59
|
|
|
throw OauthException::connect($e->getMessage()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the Access Token, or try to re-authenticate if needed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getAccessToken(): string |
|
67
|
|
|
{ |
|
68
|
|
|
$expiryCutoff = new DateTime('+10 seconds'); // Maybe the token will expire in next 10 seconds |
|
69
|
|
|
|
|
70
|
|
|
if (!$this->accessToken || $this->getExpires() < $expiryCutoff->getTimestamp()) { |
|
71
|
|
|
$this->authenticate(); // If the token expires try to re-authenticate |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!$this->accessToken) { |
|
75
|
|
|
throw OauthException::connect('Cannot authenticate, token is null after request'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->accessToken->getToken(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the Expires timestamp of the Access Token |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getExpires(): ?int |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$this->accessToken) { |
|
87
|
|
|
throw OauthException::connect('Cannot extract Expires as the token is empty'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->accessToken->getExpires(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|