Passed
Pull Request — master (#822)
by Stefano
02:53
created

ApiResolver::find()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 44
rs 8.5706
cc 7
nc 9
nop 2
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Identifier\Resolver;
14
15
use Authentication\Identifier\IdentifierInterface;
16
use Authentication\Identifier\Resolver\ResolverInterface;
17
use BEdita\SDK\BEditaClientException;
18
use BEdita\WebTools\ApiClientProvider;
19
use Cake\Log\Log;
20
use Cake\Utility\Hash;
21
22
/**
23
 * Resolver class that uses an API to obtain the user identity.
24
 */
25
class ApiResolver implements ResolverInterface
26
{
27
    /**
28
     * @inheritDoc
29
     */
30
    public function find(array $conditions, $type = self::TYPE_AND)
31
    {
32
        $apiClient = ApiClientProvider::getApiClient();
33
        if (isset($conditions[IdentifierInterface::CREDENTIAL_USERNAME], $conditions[IdentifierInterface::CREDENTIAL_PASSWORD])) {
34
            // Authenticate with credentials
35
            try {
36
                $result = $apiClient->authenticate($conditions[IdentifierInterface::CREDENTIAL_USERNAME], $conditions[IdentifierInterface::CREDENTIAL_PASSWORD]);
37
            } catch (BEditaClientException $e) {
38
                Log::info(sprintf('Login failed - %s', $e->getMessage()));
39
40
                return null;
41
            }
42
43
            if (empty($result['meta'])) {
44
                Log::info('Missing meta from authentication response');
45
46
                return null;
47
            }
48
49
            $apiClient->setupTokens($result['meta']);
50
        } elseif (isset($conditions[IdentifierInterface::CREDENTIAL_TOKEN])) {
51
            // Authenticate with renew token
52
            $apiClient->setupTokens(['renew' => $conditions[IdentifierInterface::CREDENTIAL_TOKEN]]);
53
            try {
54
                $apiClient->refreshTokens();
55
            } catch (BEditaClientException $e) {
56
                Log::info(sprintf('Failed renewing token - %s', $e->getMessage()));
57
58
                return null;
59
            }
60
        }
61
62
        try {
63
            $result = $apiClient->get('/auth/user');
64
        } catch (BEditaClientException $e) {
65
            Log::info(sprintf('Failed retrieving user data - %s', $e->getMessage()));
66
67
            return null;
68
        }
69
70
        $roles = Hash::extract($result, 'included.{n}.attributes.name');
71
        $tokens = $apiClient->getTokens();
72
73
        return $result['data'] + compact('tokens') + compact('roles');
74
    }
75
}
76