1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AdobeClient\Authentication; |
4
|
|
|
|
5
|
|
|
use Audiens\AdobeClient\Auth; |
6
|
|
|
use Audiens\AdobeClient\Exception\AuthException; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use GuzzleHttp\ClientInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class SandboxStrategy |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
class SandboxStrategy implements AuthStrategyInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
const NAME = 'adnx_auth_strategy'; |
17
|
|
|
|
18
|
|
|
const BASE_URL = 'https://api-beta.demdex.com/oauth/token'; |
19
|
|
|
|
20
|
|
|
const CACHE_NAMESPACE = 'adnx_auth_token'; |
21
|
|
|
const TOKEN_EXPIRATION = 110; |
22
|
|
|
|
23
|
|
|
/** @var Cache */ |
24
|
|
|
protected $cache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AdnxStrategy constructor. |
28
|
|
|
* |
29
|
|
|
* @param ClientInterface $clientInterface |
30
|
|
|
* @param Cache|null $cache |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ClientInterface $clientInterface, Cache $cache) |
33
|
|
|
{ |
34
|
|
|
$this->cache = $cache; |
35
|
|
|
$this->client = $clientInterface; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $clientId |
40
|
|
|
* @param string $secretKey |
41
|
|
|
* @param string $username |
42
|
|
|
* @param $password |
43
|
|
|
* @param bool $cache |
44
|
|
|
* @param bool $refresh |
45
|
|
|
* @return mixed |
46
|
|
|
* @throws AuthException |
47
|
|
|
*/ |
48
|
|
|
public function authenticate($clientId, $secretKey, $username, $password, $cache = true, $refresh = false) |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$cacheKey = self::CACHE_NAMESPACE . sha1($username . $password . self::BASE_URL); |
52
|
|
|
|
53
|
|
|
if ($cache) { |
54
|
|
|
if ($this->cache->contains($cacheKey)) { |
55
|
|
|
return $this->cache->fetch($cacheKey); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$headerAuth = base64_encode( |
60
|
|
|
sprintf( |
61
|
|
|
'%s:%s', |
62
|
|
|
$clientId, |
63
|
|
|
$secretKey |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$response = $this->client->request( |
68
|
|
|
'POST', |
69
|
|
|
self::BASE_URL, |
70
|
|
|
[ |
71
|
|
|
'headers' => |
72
|
|
|
[ |
73
|
|
|
'Authorization' => 'Basic ' . $headerAuth |
74
|
|
|
], |
75
|
|
|
'form_params' => |
76
|
|
|
[ |
77
|
|
|
'grant_type' => $refresh ? 'refresh_token' : 'password', |
78
|
|
|
'username' => $username, |
79
|
|
|
'password' => $password, |
80
|
|
|
] |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$content = $response->getBody()->getContents(); |
85
|
|
|
$response->getBody()->rewind(); |
86
|
|
|
|
87
|
|
|
$contentArray = json_decode($content, true); |
88
|
|
|
|
89
|
|
|
if (!isset($contentArray["access_token"])) { |
90
|
|
|
throw new \Exception(AuthException::authFailed('No field access token available in json response')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$token = $contentArray["access_token"]; |
94
|
|
|
|
95
|
|
|
if ($cache) { |
96
|
|
|
$this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $token; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getSlug() |
106
|
|
|
{ |
107
|
|
|
return self::NAME; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function authenticateJwtToken($clientId, $clientSecret, $jwtToken, $cache = true) |
111
|
|
|
{ |
112
|
|
|
// TODO: Implement authenticateJwtToken() method. |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.