|
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
|
|
|
|