1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\AdobeClient\Authentication; |
4
|
|
|
|
5
|
|
|
use Audiens\AdobeClient\Exception\AuthException; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class JwtStrategy |
11
|
|
|
*/ |
12
|
|
|
class JwtStrategy implements AuthStrategyInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const NAME = 'jwt_auth_strategy'; |
16
|
|
|
|
17
|
|
|
const BASE_URL = 'https://ims-na1.adobelogin.com/ims/exchange/jwt/'; |
18
|
|
|
|
19
|
|
|
const CACHE_NAMESPACE = 'jwt_auth_token'; |
20
|
|
|
const TOKEN_EXPIRATION = 110; |
21
|
|
|
|
22
|
|
|
/** @var Cache */ |
23
|
|
|
protected $cache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* JwtStrategy constructor. |
27
|
|
|
* |
28
|
|
|
* @param ClientInterface $clientInterface |
29
|
|
|
* @param Cache|null $cache |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ClientInterface $clientInterface, Cache $cache) |
32
|
|
|
{ |
33
|
|
|
$this->cache = $cache; |
34
|
|
|
$this->client = $clientInterface; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function authenticate($clientId, $secretKey, $username, $password, $cache = true, $refresh = false) |
38
|
|
|
{ |
39
|
|
|
// TODO: Implement authenticate() method. |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function authenticateJwtToken($clientId, $secretKey, $jwtToken, $cache = true) |
43
|
|
|
{ |
44
|
|
|
$cacheKey = self::CACHE_NAMESPACE . sha1($clientId . $secretKey . $jwtToken . self::BASE_URL); |
45
|
|
|
|
46
|
|
|
if ($cache) { |
47
|
|
|
if ($this->cache->contains($cacheKey)) { |
48
|
|
|
return $this->cache->fetch($cacheKey); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$response = $this->client->request( |
53
|
|
|
'POST', |
54
|
|
|
self::BASE_URL, |
55
|
|
|
[ |
56
|
|
|
'form_params' => |
57
|
|
|
[ |
58
|
|
|
'client_id' => $clientId, |
59
|
|
|
'client_secret' => $secretKey, |
60
|
|
|
'jwt_token' => $jwtToken, |
61
|
|
|
] |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$content = $response->getBody()->getContents(); |
67
|
|
|
$response->getBody()->rewind(); |
68
|
|
|
|
69
|
|
|
$contentArray = \json_decode($content, true); |
70
|
|
|
|
71
|
|
|
if (!isset($contentArray["access_token"])) { |
72
|
|
|
throw new \Exception(AuthException::authFailed('No field access token available in json response')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$token = $contentArray["access_token"]; |
76
|
|
|
|
77
|
|
|
if ($cache) { |
78
|
|
|
$this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $token; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getSlug() |
88
|
|
|
{ |
89
|
|
|
return self::NAME; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: