Completed
Push — 4.0 ( da64f9 )
by Jeroen
04:55
created

TokenPool::getEndpointUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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
     */
77
    public function getPublicUrl() {
78
        return $this->getEndpointUrl();
79
    }
80
81
    /**
82
     * @return string
83
     */
84 9
    public function getEndpointUrl()
85
    {
86 9
        if (null === $this->endpointUrl) {
87 5
            $this->getToken();
88 5
        }
89
90 9
        return $this->endpointUrl;
91
    }
92
93
    /**
94
     * @return string
95
     */
96 9
    public function getTokenId()
97
    {
98 9
        if (null === $this->tokenId) {
99
            $this->getToken();
100
        }
101
102 9
        return $this->tokenId;
103
    }
104
105
    /**
106
     * Returns a token to use for the keystone service. Uses cached instance
107
     * whenever possible.
108
     *
109
     * @param bool $forceNew Whether to force creating a new token
110
     *
111
     * @return Token
112
     */
113 10
    public function getToken($forceNew = false)
114
    {
115 10
        $tokenName = $this->getCacheKey();
116
117
        // see if token is in cache
118 10
        if (!$forceNew && $cachedToken = $this->cache->get($tokenName)) {
119 7
            $this->logger->debug('Obtained token from cache');
120 7
            $token = $this->createToken($cachedToken);
121 6
        }
122
123 9
        if (!isset($token) || !($token instanceof Token) || $token->isExpired()) {
124
            // cache the token and set it to expire 5 seconds before the
125
            // expiration date, to avoid any concurrence errors.
126 7
            $token = $this->requestToken();
127 6
            $this->cache->set($tokenName, json_encode($token), $token->getExpirationDate()->getTimestamp() - time() - 5);
128 6
        }
129
130
        // cache token properties
131 9
        $this->endpointUrl = $this->getEndpointUrlFromToken($token);
132 9
        $this->tokenId = $token->getId();
133
134 9
        return $token;
135
    }
136
137
    /**
138
     * @throws TokenException
139
     *
140
     * @return Token
141
     */
142 4
    protected function requestToken()
143
    {
144 4
        $this->logger->debug('Requesting a new token');
145
146
        $data = [
147
            'auth' => [
148
                'passwordCredentials' => [
149 4
                    'password' => $this->tenant->getPassword(),
150 4
                    'username' => $this->tenant->getUsername(),
151 4
                ],
152 4
            ],
153 4
        ];
154
155 4
        if ($name = $this->tenant->getTenantName()) {
156
            $data['auth']['tenantName'] = $name;
157
        }
158
159
        try {
160 4
            $response = $this->client->request('POST', $this->tenant->getTokenUrl(), [
161 4
                RequestOptions::JSON => $data,
162 4
            ]);
163
164 3
            return $this->createToken($response->getBody()->getContents());
165 1
        } catch (RequestException $e) {
166 1
            $this->logger->error(sprintf('Error requesting token: %s', $e->getMessage()));
167
168 1
            throw new TokenException($e->getMessage(), $e->getCode(), $e);
169
        }
170
    }
171
172
    /**
173
     * @param string $jsonData
174
     *
175
     * @throws TokenException
176
     *
177
     * @return Token
178
     */
179 8
    private function createToken($jsonData)
180
    {
181 8
        $content = json_decode($jsonData, true);
182
183 8
        if (!is_array($content)) {
184 1
            throw new TokenException(sprintf('Could not decode JSON string: "%s"', $jsonData));
185
        }
186
187 7
        return Token::create($content);
188
    }
189
190
    /**
191
     * @param Token $token
192
     *
193
     * @throws TokenException
194
     *
195
     * @return string
196
     */
197 9
    private function getEndpointUrlFromToken(Token $token)
198
    {
199 9
        $catalog = $token->getServiceCatalog($this->tenant->getServiceType(), $this->tenant->getServiceName());
200
201
        // use the first endpoint that has was requested
202 9
        $endpointType = $this->tenant->getServiceEndpoint() . 'url';
203 9
        foreach ($catalog as $endpoint) {
204 9
            $endpoint = array_change_key_case($endpoint, CASE_LOWER);
205 9
            if (array_key_exists($endpointType, $endpoint)) {
206 9
                return $endpoint[$endpointType];
207
            }
208
        }
209
210
        throw new TokenException('No endpoint with a ' . $this->tenant->getServiceEndpoint() . ' url found');
211
    }
212
213
    /**
214
     * @return string
215
     */
216 10
    private function getCacheKey()
217
    {
218 10
        return sprintf(self::TOKEN_KEY_FORMAT, rawurlencode($this->tenant->getTokenUrl()));
219
    }
220
}
221