Passed
Pull Request — 1.11.x (#5763)
by Angel Fernando Quiroz
08:35
created

AzureCommand::generateOrRefreshToken()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 4
nc 2
nop 1
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
6
use League\OAuth2\Client\Token\AccessTokenInterface;
7
use TheNetworg\OAuth2\Client\Provider\Azure;
8
9
abstract class AzureCommand
10
{
11
    /**
12
     * @var AzureActiveDirectory
13
     */
14
    protected $plugin;
15
    /**
16
     * @var Azure
17
     */
18
    protected $provider;
19
20
    public function __construct()
21
    {
22
        $this->plugin = AzureActiveDirectory::create();
23
        $this->plugin->get_settings(true);
24
        $this->provider = $this->plugin->getProviderForApiGraph();
25
    }
26
27
    /**
28
     * @throws IdentityProviderException
29
     */
30
    protected function generateOrRefreshToken(?AccessTokenInterface &$token)
31
    {
32
        if (!$token || ($token->getExpires() && !$token->getRefreshToken())) {
33
            $token = $this->provider->getAccessToken(
34
                'client_credentials',
35
                ['resource' => $this->provider->resource]
36
            );
37
        }
38
    }
39
40
    /**
41
     * @throws Exception
42
     *
43
     * @return Generator<int, array<string, string>>
44
     */
45
    protected function getAzureUsers(): Generator
46
    {
47
        $userFields = [
48
            'givenName',
49
            'surname',
50
            'mail',
51
            'userPrincipalName',
52
            'businessPhones',
53
            'mobilePhone',
54
            'accountEnabled',
55
            'mailNickname',
56
            'id',
57
        ];
58
59
        $query = sprintf(
60
            '$top=%d&$select=%s',
61
            AzureActiveDirectory::API_PAGE_SIZE,
62
            implode(',', $userFields)
63
        );
64
65
        $token = null;
66
67
        do {
68
            $this->generateOrRefreshToken($token);
69
70
            try {
71
                $azureUsersRequest = $this->provider->request(
72
                    'get',
73
                    "users?$query",
74
                    $token
75
                );
76
            } catch (Exception $e) {
77
                throw new Exception('Exception when requesting users from Azure: '.$e->getMessage());
78
            }
79
80
            $azureUsersInfo = $azureUsersRequest['value'] ?? [];
81
82
            foreach ($azureUsersInfo as $azureUserInfo) {
83
                yield $azureUserInfo;
84
            }
85
86
            $hasNextLink = false;
87
88
            if (!empty($azureUsersRequest['@odata.nextLink'])) {
89
                $hasNextLink = true;
90
                $query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY);
91
            }
92
        } while ($hasNextLink);
93
    }
94
95
    /**
96
     * @throws Exception
97
     *
98
     * @return Generator<int, array<string, string>>
99
     */
100
    protected function getAzureGroups(): Generator
101
    {
102
        $groupFields = [
103
            'id',
104
            'displayName',
105
            'description',
106
        ];
107
108
        $query = sprintf(
109
            '$top=%d&$select=%s',
110
            AzureActiveDirectory::API_PAGE_SIZE,
111
            implode(',', $groupFields)
112
        );
113
114
        $token = null;
115
116
        do {
117
            $this->generateOrRefreshToken($token);
118
119
            try {
120
                $azureGroupsRequest = $this->provider->request('get', "groups?$query", $token);
121
            } catch (Exception $e) {
122
                throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage());
123
            }
124
125
            $azureGroupsInfo = $azureGroupsRequest['value'] ?? [];
126
127
            foreach ($azureGroupsInfo as $azureGroupInfo) {
128
                yield $azureGroupInfo;
129
            }
130
131
            $hasNextLink = false;
132
133
            if (!empty($azureGroupsRequest['@odata.nextLink'])) {
134
                $hasNextLink = true;
135
                $query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY);
136
            }
137
        } while ($hasNextLink);
138
    }
139
140
    /**
141
     * @throws Exception
142
     *
143
     * @return Generator<int, array<string, string>>
144
     */
145
    protected function getAzureGroupMembers(string $groupUid): Generator
146
    {
147
        $userFields = [
148
            'mail',
149
            'mailNickname',
150
            'id',
151
        ];
152
153
        $query = sprintf(
154
            '$top=%d&$select=%s',
155
            AzureActiveDirectory::API_PAGE_SIZE,
156
            implode(',', $userFields)
157
        );
158
159
        $token = null;
160
161
        do {
162
            $this->generateOrRefreshToken($token);
163
164
            try {
165
                $azureGroupMembersRequest = $this->provider->request(
166
                    'get',
167
                    "groups/$groupUid/members?$query",
168
                    $token
169
                );
170
            } catch (Exception $e) {
171
                throw new Exception('Exception when requesting group members from Azure: '.$e->getMessage());
172
            }
173
174
            $azureGroupMembers = $azureGroupMembersRequest['value'] ?? [];
175
176
            foreach ($azureGroupMembers as $azureGroupMember) {
177
                yield $azureGroupMember;
178
            }
179
180
            $hasNextLink = false;
181
182
            if (!empty($azureGroupMembersRequest['@odata.nextLink'])) {
183
                $hasNextLink = true;
184
                $query = parse_url($azureGroupMembersRequest['@odata.nextLink'], PHP_URL_QUERY);
185
            }
186
        } while ($hasNextLink);
187
    }
188
}
189