Completed
Push — master ( cc74a3...da64f9 )
by Peter
14:57
created

TokenPool::getEndpointUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 2
cts 3
cp 0.6667
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.1481
1
<?php
2
3
namespace TreeHouse\Keystone\Client;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\RequestOptions;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use TreeHouse\Cache\CacheInterface;
12
use TreeHouse\Keystone\Client\Exception\TokenException;
13
use TreeHouse\Keystone\Client\Model\Tenant;
14
use TreeHouse\Keystone\Client\Model\Token;
15
16
class TokenPool
17
{
18
    const TOKEN_KEY_FORMAT = 'keystone_token_3_%s';
19
20
    /**
21
     * @var Tenant
22
     */
23
    protected $tenant;
24
25
    /**
26
     * The cache where tokens are stored.
27
     *
28
     * @var CacheInterface
29
     */
30
    protected $cache;
31
32
    /**
33
     * Optional logger.
34
     *
35
     * @var LoggerInterface
36
     */
37
    protected $logger;
38
39
    /**
40
     * Internal client to request a new token with.
41
     *
42
     * @var ClientInterface
43
     */
44
    protected $client;
45
46
    /**
47
     * The endpoint url that was obtained via the token.
48
     *
49
     * @var string
50
     */
51
    protected $endpointUrl;
52
53
    /**
54
     * Cached copy of the token id.
55
     *
56
     * @var string
57
     */
58
    protected $tokenId;
59
60
    /**
61
     * @param Tenant          $tenant
62
     * @param CacheInterface  $cache
63
     * @param LoggerInterface $logger
64
     */
65 14
    public function __construct(Tenant $tenant, CacheInterface $cache, LoggerInterface $logger = null)
66
    {
67 14
        $this->tenant = $tenant;
68 14
        $this->cache = $cache;
69 14
        $this->logger = $logger ?: new NullLogger();
70 14
        $this->client = new Client();
71 14
    }
72
73
    /**
74
     * @deprecated Please use getEndpointUrl() in favor of this
75
     * @return string
76 9
     */
77
    public function getPublicUrl() {
78 9
        return $this->getEndpointUrl();
79 5
    }
80 5
81
    /**
82 9
     * @return string
83
     */
84
    public function getEndpointUrl()
85
    {
86
        if (null === $this->endpointUrl) {
87
            $this->getToken();
88 9
        }
89
90 9
        return $this->endpointUrl;
91
    }
92
93
    /**
94 9
     * @return string
95
     */
96
    public function getTokenId()
97
    {
98
        if (null === $this->tokenId) {
99
            $this->getToken();
100
        }
101
102
        return $this->tokenId;
103
    }
104
105 10
    /**
106
     * Returns a token to use for the keystone service. Uses cached instance
107 10
     * whenever possible.
108
     *
109
     * @param bool $forceNew Whether to force creating a new token
110 10
     *
111 7
     * @return Token
112 7
     */
113 6
    public function getToken($forceNew = false)
114
    {
115 9
        $tokenName = $this->getCacheKey();
116
117
        // see if token is in cache
118 7
        if (!$forceNew && $cachedToken = $this->cache->get($tokenName)) {
119 6
            $this->logger->debug('Obtained token from cache');
120 6
            $token = $this->createToken($cachedToken);
121
        }
122
123 9
        if (!isset($token) || !($token instanceof Token) || $token->isExpired()) {
124 9
            // cache the token and set it to expire 5 seconds before the
125
            // expiration date, to avoid any concurrence errors.
126 9
            $token = $this->requestToken();
127
            $this->cache->set($tokenName, json_encode($token), $token->getExpirationDate()->getTimestamp() - time() - 5);
128
        }
129
130
        // cache token properties
131
        $this->endpointUrl = $this->getEndpointUrlFromToken($token);
132
        $this->tokenId = $token->getId();
133
134 4
        return $token;
135
    }
136 4
137
    /**
138
     * @throws TokenException
139
     *
140
     * @return Token
141 4
     */
142 4
    protected function requestToken()
143 4
    {
144 4
        $this->logger->debug('Requesting a new token');
145 4
146
        $data = [
147 4
            'auth' => [
148
                'passwordCredentials' => [
149
                    'password' => $this->tenant->getPassword(),
150
                    'username' => $this->tenant->getUsername(),
151
                ],
152 4
            ],
153 4
        ];
154 4
155
        if ($name = $this->tenant->getTenantName()) {
156 3
            $data['auth']['tenantName'] = $name;
157 1
        }
158 1
159
        try {
160 1
            $response = $this->client->request('POST', $this->tenant->getTokenUrl(), [
161
                RequestOptions::JSON => $data,
162
            ]);
163
164
            return $this->createToken($response->getBody()->getContents());
165
        } catch (RequestException $e) {
166
            $this->logger->error(sprintf('Error requesting token: %s', $e->getMessage()));
167
168
            throw new TokenException($e->getMessage(), $e->getCode(), $e);
169
        }
170
    }
171 8
172
    /**
173 8
     * @param string $jsonData
174
     *
175 8
     * @throws TokenException
176 1
     *
177
     * @return Token
178
     */
179 7
    private function createToken($jsonData)
180
    {
181
        $content = json_decode($jsonData, true);
182
183
        if (!is_array($content)) {
184
            throw new TokenException(sprintf('Could not decode JSON string: "%s"', $jsonData));
185
        }
186
187
        return Token::create($content);
188
    }
189 9
190
    /**
191 9
     * @param Token $token
192
     *
193
     * @throws TokenException
194 9
     *
195 9
     * @return string
196 9
     */
197 9
    private function getEndpointUrlFromToken(Token $token)
198
    {
199
        $catalog = $token->getServiceCatalog($this->tenant->getServiceType(), $this->tenant->getServiceName());
200
201
        // use the first endpoint that has was requested
202
        $endpointType = $this->tenant->getServiceEndpoint() . 'url';
203
        foreach ($catalog as $endpoint) {
204
            $endpoint = array_change_key_case($endpoint, CASE_LOWER);
205
            if (array_key_exists($endpointType, $endpoint)) {
206
                return $endpoint[$endpointType];
207 10
            }
208
        }
209 10
210
        throw new TokenException('No endpoint with a ' . $this->tenant->getServiceEndpoint() . ' url found');
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    private function getCacheKey()
217
    {
218
        return sprintf(self::TOKEN_KEY_FORMAT, rawurlencode($this->tenant->getTokenUrl()));
219
    }
220
}
221