Completed
Pull Request — master (#15)
by Peter
24:50 queued 14:44
created

TokenPool::requestToken()   B

Complexity

Conditions 3
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0146

Importance

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