@@ 12-91 (lines=80) @@ | ||
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.adnxs.com/auth'; |
|
18 | ||
19 | const CACHE_NAMESPACE = 'adnx_auth_token'; |
|
20 | const TOKEN_EXPIRATION = 110; |
|
21 | ||
22 | /** @var Cache */ |
|
23 | protected $cache; |
|
24 | ||
25 | /** @var ClientInterface */ |
|
26 | protected $client; |
|
27 | ||
28 | /** |
|
29 | * @param ClientInterface $clientInterface |
|
30 | * @param Cache $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 $username |
|
40 | * @param string $password |
|
41 | * @param bool|true $cache |
|
42 | * |
|
43 | * @return mixed |
|
44 | * @throws AuthException |
|
45 | */ |
|
46 | public function authenticate($username, $password, $cache = true) |
|
47 | { |
|
48 | ||
49 | $cacheKey = self::CACHE_NAMESPACE.sha1($username.$password.self::BASE_URL); |
|
50 | ||
51 | if ($cache) { |
|
52 | if ($this->cache->contains($cacheKey)) { |
|
53 | return $this->cache->fetch($cacheKey); |
|
54 | } |
|
55 | } |
|
56 | ||
57 | $payload = [ |
|
58 | 'auth' => [ |
|
59 | 'username' => $username, |
|
60 | 'password' => $password, |
|
61 | ], |
|
62 | ]; |
|
63 | ||
64 | $response = $this->client->request('POST', self::BASE_URL, ['body' => json_encode($payload)]); |
|
65 | ||
66 | $content = $response->getBody()->getContents(); |
|
67 | $response->getBody()->rewind(); |
|
68 | ||
69 | $contentArray = json_decode($content, true); |
|
70 | ||
71 | if (!isset($contentArray["response"]["token"])) { |
|
72 | throw new AuthException($content); |
|
73 | } |
|
74 | ||
75 | $token = $contentArray["response"]["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 |
@@ 12-91 (lines=80) @@ | ||
9 | /** |
|
10 | * Class AppnexusStrategy |
|
11 | */ |
|
12 | class AppnexusStrategy implements AuthStrategyInterface |
|
13 | { |
|
14 | ||
15 | const NAME = 'adnx_auth_strategy'; |
|
16 | ||
17 | const BASE_URL = 'https://api.appnexus.com/auth'; |
|
18 | ||
19 | const CACHE_NAMESPACE = 'appnexus_auth_token'; |
|
20 | const TOKEN_EXPIRATION = 110; |
|
21 | ||
22 | /** @var Cache */ |
|
23 | protected $cache; |
|
24 | ||
25 | /** @var ClientInterface */ |
|
26 | protected $client; |
|
27 | ||
28 | /** |
|
29 | * @param ClientInterface $clientInterface |
|
30 | * @param Cache $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 $username |
|
40 | * @param string $password |
|
41 | * @param bool|true $cache |
|
42 | * |
|
43 | * @return mixed |
|
44 | * @throws AuthException |
|
45 | */ |
|
46 | public function authenticate($username, $password, $cache = true) |
|
47 | { |
|
48 | ||
49 | $cacheKey = self::CACHE_NAMESPACE.sha1($username.$password.self::BASE_URL); |
|
50 | ||
51 | if ($cache) { |
|
52 | if ($this->cache->contains($cacheKey)) { |
|
53 | return $this->cache->fetch($cacheKey); |
|
54 | } |
|
55 | } |
|
56 | ||
57 | $payload = [ |
|
58 | 'auth' => [ |
|
59 | 'username' => $username, |
|
60 | 'password' => $password, |
|
61 | ], |
|
62 | ]; |
|
63 | ||
64 | $response = $this->client->request('POST', self::BASE_URL, ['body' => json_encode($payload)]); |
|
65 | ||
66 | $content = $response->getBody()->getContents(); |
|
67 | $response->getBody()->rewind(); |
|
68 | ||
69 | $contentArray = json_decode($content, true); |
|
70 | ||
71 | if (!isset($contentArray["response"]["token"])) { |
|
72 | throw new AuthException($content); |
|
73 | } |
|
74 | ||
75 | $token = $contentArray["response"]["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 |
@@ 12-91 (lines=80) @@ | ||
9 | /** |
|
10 | * Class SandboxStrategy |
|
11 | */ |
|
12 | class SandboxStrategy implements AuthStrategyInterface |
|
13 | { |
|
14 | ||
15 | const NAME = 'sandbox_auth_strategy'; |
|
16 | ||
17 | const BASE_URL = 'http://api-test.adnxs.com/auth'; |
|
18 | ||
19 | const CACHE_NAMESPACE = 'adnx_auth_token'; |
|
20 | const TOKEN_EXPIRATION = 110; |
|
21 | ||
22 | /** @var Cache */ |
|
23 | protected $cache; |
|
24 | ||
25 | /** @var ClientInterface */ |
|
26 | protected $client; |
|
27 | ||
28 | /** |
|
29 | * @param ClientInterface $clientInterface |
|
30 | * @param Cache $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 $username |
|
40 | * @param string $password |
|
41 | * @param bool|true $cache |
|
42 | * |
|
43 | * @return mixed |
|
44 | * @throws AuthException |
|
45 | */ |
|
46 | public function authenticate($username, $password, $cache = true) |
|
47 | { |
|
48 | ||
49 | $cacheKey = self::CACHE_NAMESPACE.sha1($username.$password.self::BASE_URL); |
|
50 | ||
51 | if ($cache) { |
|
52 | if ($this->cache->contains($cacheKey)) { |
|
53 | return $this->cache->fetch($cacheKey); |
|
54 | } |
|
55 | } |
|
56 | ||
57 | $payload = [ |
|
58 | 'auth' => [ |
|
59 | 'username' => $username, |
|
60 | 'password' => $password, |
|
61 | ], |
|
62 | ]; |
|
63 | ||
64 | $response = $this->client->request('POST', self::BASE_URL, ['body' => json_encode($payload)]); |
|
65 | ||
66 | $content = $response->getBody()->getContents(); |
|
67 | $response->getBody()->rewind(); |
|
68 | ||
69 | $contentArray = json_decode($content, true); |
|
70 | ||
71 | if (!isset($contentArray["response"]["token"])) { |
|
72 | throw new AuthException($content); |
|
73 | } |
|
74 | ||
75 | $token = $contentArray["response"]["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 |