@@ 12-111 (lines=100) @@ | ||
9 | /** |
|
10 | * Class AdnxStrategy |
|
11 | */ |
|
12 | class AdnxStrategy implements AuthStrategyInterface |
|
13 | { |
|
14 | ||
15 | const NAME = 'adnx_auth_strategy'; |
|
16 | ||
17 | const BASE_URL = 'https://api.demdex.com/oauth/token'; |
|
18 | ||
19 | const CACHE_NAMESPACE = 'adnx_auth_token'; |
|
20 | const TOKEN_EXPIRATION = 110; |
|
21 | ||
22 | /** @var Cache */ |
|
23 | protected $cache; |
|
24 | ||
25 | /** |
|
26 | * AdnxStrategy 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 | /** |
|
38 | * @param string $username |
|
39 | * @param string $password |
|
40 | * @param bool $cache |
|
41 | * @param bool $refresh |
|
42 | * @return mixed |
|
43 | * @throws AuthException |
|
44 | */ |
|
45 | public function authenticate($clientId, $secretKey, $username, $password, $cache = true, $refresh = false) |
|
46 | { |
|
47 | $cacheKey = self::CACHE_NAMESPACE . sha1($username . $password . self::BASE_URL); |
|
48 | ||
49 | if ($cache) { |
|
50 | if ($this->cache->contains($cacheKey)) { |
|
51 | return $this->cache->fetch($cacheKey); |
|
52 | } |
|
53 | } |
|
54 | ||
55 | $headerAuth = base64_encode( |
|
56 | sprintf( |
|
57 | '%s:%s', |
|
58 | $clientId, |
|
59 | $secretKey |
|
60 | ) |
|
61 | ); |
|
62 | ||
63 | $response = $this->client->request( |
|
64 | 'POST', |
|
65 | self::BASE_URL, |
|
66 | [ |
|
67 | 'headers' => |
|
68 | [ |
|
69 | 'Authorization' => 'Basic ' . $headerAuth |
|
70 | ], |
|
71 | 'form_params' => |
|
72 | [ |
|
73 | 'grant_type' => $refresh ? 'refresh_token' : 'password', |
|
74 | 'username' => $username, |
|
75 | 'password' => $password, |
|
76 | ] |
|
77 | ] |
|
78 | ); |
|
79 | ||
80 | ||
81 | $content = $response->getBody()->getContents(); |
|
82 | $response->getBody()->rewind(); |
|
83 | ||
84 | $contentArray = json_decode($content, true); |
|
85 | ||
86 | if (!isset($contentArray["access_token"])) { |
|
87 | throw new \Exception(AuthException::authFailed('No field access token available in json response')); |
|
88 | } |
|
89 | ||
90 | $token = $contentArray["access_token"]; |
|
91 | ||
92 | if ($cache) { |
|
93 | $this->cache->save($cacheKey, $token, self::TOKEN_EXPIRATION); |
|
94 | } |
|
95 | ||
96 | return $token; |
|
97 | } |
|
98 | ||
99 | /** |
|
100 | * @return string |
|
101 | */ |
|
102 | public function getSlug() |
|
103 | { |
|
104 | return self::NAME; |
|
105 | } |
|
106 | ||
107 | public function authenticateJwtToken($clientId, $clientSecret, $jwtToken, $cache = true) |
|
108 | { |
|
109 | // TODO: Implement authenticateJwtToken() method. |
|
110 | } |
|
111 | } |
|
112 |
@@ 13-114 (lines=102) @@ | ||
10 | /** |
|
11 | * Class SandboxStrategy |
|
12 | */ |
|
13 | 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 |