@@ -17,109 +17,109 @@ |
||
17 | 17 | */ |
18 | 18 | class GitHub extends OAuth2 |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - protected $scope = 'user:email'; |
|
24 | - |
|
25 | - /** |
|
26 | - * {@inheritdoc} |
|
27 | - */ |
|
28 | - protected $apiBaseUrl = 'https://api.github.com/'; |
|
29 | - |
|
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected $authorizeUrl = 'https://github.com/login/oauth/authorize'; |
|
34 | - |
|
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - protected $accessTokenUrl = 'https://github.com/login/oauth/access_token'; |
|
39 | - |
|
40 | - /** |
|
41 | - * {@inheritdoc} |
|
42 | - */ |
|
43 | - protected $apiDocumentation = 'https://developer.github.com/v3/oauth/'; |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - protected function initialize() |
|
49 | - { |
|
50 | - parent::initialize(); |
|
51 | - |
|
52 | - if ($this->isRefreshTokenAvailable()) { |
|
53 | - $this->tokenRefreshParameters += [ |
|
54 | - 'client_id' => $this->clientId, |
|
55 | - 'client_secret' => $this->clientSecret |
|
56 | - ]; |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - public function getUserProfile() |
|
64 | - { |
|
65 | - $response = $this->apiRequest('user'); |
|
66 | - |
|
67 | - $data = new Data\Collection($response); |
|
68 | - |
|
69 | - if (!$data->exists('id')) { |
|
70 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
71 | - } |
|
72 | - |
|
73 | - $userProfile = new User\Profile(); |
|
74 | - |
|
75 | - $userProfile->identifier = $data->get('id'); |
|
76 | - $userProfile->displayName = $data->get('name'); |
|
77 | - $userProfile->description = $data->get('bio'); |
|
78 | - $userProfile->photoURL = $data->get('avatar_url'); |
|
79 | - $userProfile->profileURL = $data->get('html_url'); |
|
80 | - $userProfile->email = $data->get('email'); |
|
81 | - $userProfile->webSiteURL = $data->get('blog'); |
|
82 | - $userProfile->region = $data->get('location'); |
|
83 | - |
|
84 | - $userProfile->displayName = $userProfile->displayName ?: $data->get('login'); |
|
85 | - |
|
86 | - if (empty($userProfile->email) && strpos($this->scope, 'user:email') !== false) { |
|
87 | - try { |
|
88 | - // user email is not mandatory so keep it quite. |
|
89 | - $userProfile = $this->requestUserEmail($userProfile); |
|
90 | - } catch (\Exception $e) { |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - return $userProfile; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Request connected user email |
|
99 | - * |
|
100 | - * https://developer.github.com/v3/users/emails/ |
|
101 | - * @param User\Profile $userProfile |
|
102 | - * |
|
103 | - * @return User\Profile |
|
104 | - * |
|
105 | - * @throws \Exception |
|
106 | - */ |
|
107 | - protected function requestUserEmail(User\Profile $userProfile) |
|
108 | - { |
|
109 | - $response = $this->apiRequest('user/emails'); |
|
110 | - |
|
111 | - foreach ($response as $idx => $item) { |
|
112 | - if (!empty($item->primary) && $item->primary == 1) { |
|
113 | - $userProfile->email = $item->email; |
|
114 | - |
|
115 | - if (!empty($item->verified) && $item->verified == 1) { |
|
116 | - $userProfile->emailVerified = $userProfile->email; |
|
117 | - } |
|
118 | - |
|
119 | - break; |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - return $userProfile; |
|
124 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + protected $scope = 'user:email'; |
|
24 | + |
|
25 | + /** |
|
26 | + * {@inheritdoc} |
|
27 | + */ |
|
28 | + protected $apiBaseUrl = 'https://api.github.com/'; |
|
29 | + |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected $authorizeUrl = 'https://github.com/login/oauth/authorize'; |
|
34 | + |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + protected $accessTokenUrl = 'https://github.com/login/oauth/access_token'; |
|
39 | + |
|
40 | + /** |
|
41 | + * {@inheritdoc} |
|
42 | + */ |
|
43 | + protected $apiDocumentation = 'https://developer.github.com/v3/oauth/'; |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + protected function initialize() |
|
49 | + { |
|
50 | + parent::initialize(); |
|
51 | + |
|
52 | + if ($this->isRefreshTokenAvailable()) { |
|
53 | + $this->tokenRefreshParameters += [ |
|
54 | + 'client_id' => $this->clientId, |
|
55 | + 'client_secret' => $this->clientSecret |
|
56 | + ]; |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + public function getUserProfile() |
|
64 | + { |
|
65 | + $response = $this->apiRequest('user'); |
|
66 | + |
|
67 | + $data = new Data\Collection($response); |
|
68 | + |
|
69 | + if (!$data->exists('id')) { |
|
70 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
71 | + } |
|
72 | + |
|
73 | + $userProfile = new User\Profile(); |
|
74 | + |
|
75 | + $userProfile->identifier = $data->get('id'); |
|
76 | + $userProfile->displayName = $data->get('name'); |
|
77 | + $userProfile->description = $data->get('bio'); |
|
78 | + $userProfile->photoURL = $data->get('avatar_url'); |
|
79 | + $userProfile->profileURL = $data->get('html_url'); |
|
80 | + $userProfile->email = $data->get('email'); |
|
81 | + $userProfile->webSiteURL = $data->get('blog'); |
|
82 | + $userProfile->region = $data->get('location'); |
|
83 | + |
|
84 | + $userProfile->displayName = $userProfile->displayName ?: $data->get('login'); |
|
85 | + |
|
86 | + if (empty($userProfile->email) && strpos($this->scope, 'user:email') !== false) { |
|
87 | + try { |
|
88 | + // user email is not mandatory so keep it quite. |
|
89 | + $userProfile = $this->requestUserEmail($userProfile); |
|
90 | + } catch (\Exception $e) { |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + return $userProfile; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Request connected user email |
|
99 | + * |
|
100 | + * https://developer.github.com/v3/users/emails/ |
|
101 | + * @param User\Profile $userProfile |
|
102 | + * |
|
103 | + * @return User\Profile |
|
104 | + * |
|
105 | + * @throws \Exception |
|
106 | + */ |
|
107 | + protected function requestUserEmail(User\Profile $userProfile) |
|
108 | + { |
|
109 | + $response = $this->apiRequest('user/emails'); |
|
110 | + |
|
111 | + foreach ($response as $idx => $item) { |
|
112 | + if (!empty($item->primary) && $item->primary == 1) { |
|
113 | + $userProfile->email = $item->email; |
|
114 | + |
|
115 | + if (!empty($item->verified) && $item->verified == 1) { |
|
116 | + $userProfile->emailVerified = $userProfile->email; |
|
117 | + } |
|
118 | + |
|
119 | + break; |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + return $userProfile; |
|
124 | + } |
|
125 | 125 | } |
@@ -43,138 +43,138 @@ |
||
43 | 43 | */ |
44 | 44 | class MicrosoftGraph extends OAuth2 |
45 | 45 | { |
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - protected $scope = 'openid user.read contacts.read offline_access'; |
|
50 | - |
|
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected $apiBaseUrl = 'https://graph.microsoft.com/v1.0/'; |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - protected $authorizeUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'; |
|
60 | - |
|
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - protected $accessTokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected $apiDocumentation = 'https://developer.microsoft.com/en-us/graph/docs/concepts/php'; |
|
70 | - |
|
71 | - /** |
|
72 | - * {@inheritdoc} |
|
73 | - */ |
|
74 | - protected function initialize() |
|
75 | - { |
|
76 | - parent::initialize(); |
|
77 | - |
|
78 | - $this->AuthorizeUrlParameters += [ |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + protected $scope = 'openid user.read contacts.read offline_access'; |
|
50 | + |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected $apiBaseUrl = 'https://graph.microsoft.com/v1.0/'; |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + protected $authorizeUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'; |
|
60 | + |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + protected $accessTokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'; |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected $apiDocumentation = 'https://developer.microsoft.com/en-us/graph/docs/concepts/php'; |
|
70 | + |
|
71 | + /** |
|
72 | + * {@inheritdoc} |
|
73 | + */ |
|
74 | + protected function initialize() |
|
75 | + { |
|
76 | + parent::initialize(); |
|
77 | + |
|
78 | + $this->AuthorizeUrlParameters += [ |
|
79 | 79 | 'prompt' => 'consent', |
80 | - ]; |
|
81 | - |
|
82 | - $tenant = $this->config->get('tenant'); |
|
83 | - if (!empty($tenant)) { |
|
84 | - $adjustedEndpoints = [ |
|
85 | - 'authorize_url' => str_replace('/common/', '/' . $tenant . '/', $this->authorizeUrl), |
|
86 | - 'access_token_url' => str_replace('/common/', '/' . $tenant . '/', $this->accessTokenUrl), |
|
87 | - ]; |
|
88 | - |
|
89 | - $this->setApiEndpoints($adjustedEndpoints); |
|
90 | - } |
|
91 | - |
|
92 | - if ($this->isRefreshTokenAvailable()) { |
|
93 | - $this->tokenRefreshParameters += [ |
|
94 | - 'client_id' => $this->clientId, |
|
95 | - 'client_secret' => $this->clientSecret, |
|
96 | - ]; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * {@inheritdoc} |
|
102 | - */ |
|
103 | - public function getUserProfile() |
|
104 | - { |
|
105 | - $response = $this->apiRequest('me'); |
|
106 | - |
|
107 | - $data = new Data\Collection($response); |
|
108 | - |
|
109 | - if (!$data->exists('id')) { |
|
110 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
111 | - } |
|
112 | - |
|
113 | - $userProfile = new User\Profile(); |
|
114 | - |
|
115 | - $userProfile->identifier = $data->get('id'); |
|
116 | - $userProfile->displayName = $data->get('displayName'); |
|
117 | - $userProfile->firstName = $data->get('givenName'); |
|
118 | - $userProfile->lastName = $data->get('surname'); |
|
119 | - $userProfile->language = $data->get('preferredLanguage'); |
|
120 | - |
|
121 | - $userProfile->phone = $data->get('mobilePhone'); |
|
122 | - if (empty($userProfile->phone)) { |
|
123 | - $businessPhones = $data->get('businessPhones'); |
|
124 | - if (isset($businessPhones[0])) { |
|
125 | - $userProfile->phone = $businessPhones[0]; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - $userProfile->email = $data->get('mail'); |
|
130 | - if (empty($userProfile->email)) { |
|
131 | - $email = $data->get('userPrincipalName'); |
|
132 | - if (strpos($email, '@') !== false) { |
|
133 | - $userProfile->email = $email; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - return $userProfile; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * {@inheritdoc} |
|
142 | - */ |
|
143 | - public function getUserContacts() |
|
144 | - { |
|
145 | - $apiUrl = 'me/contacts?$top=50'; |
|
146 | - $contacts = []; |
|
147 | - |
|
148 | - do { |
|
149 | - $response = $this->apiRequest($apiUrl); |
|
150 | - $data = new Data\Collection($response); |
|
151 | - if (!$data->exists('value')) { |
|
152 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
153 | - } |
|
154 | - foreach ($data->filter('value')->toArray() as $entry) { |
|
155 | - $entry = new Data\Collection($entry); |
|
156 | - $userContact = new User\Contact(); |
|
157 | - $userContact->identifier = $entry->get('id'); |
|
158 | - $userContact->displayName = $entry->get('displayName'); |
|
159 | - $emailAddresses = $entry->get('emailAddresses'); |
|
160 | - if (!empty($emailAddresses)) { |
|
161 | - $userContact->email = $emailAddresses[0]->address; |
|
162 | - } |
|
163 | - // only add to collection if we have usefull data |
|
164 | - if (!empty($userContact->displayName) || !empty($userContact->email)) { |
|
165 | - $contacts[] = $userContact; |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - if ($data->exists('@odata.nextLink')) { |
|
170 | - $apiUrl = $data->get('@odata.nextLink'); |
|
171 | - |
|
172 | - $pagedList = true; |
|
173 | - } else { |
|
174 | - $pagedList = false; |
|
175 | - } |
|
176 | - } while ($pagedList); |
|
177 | - |
|
178 | - return $contacts; |
|
179 | - } |
|
80 | + ]; |
|
81 | + |
|
82 | + $tenant = $this->config->get('tenant'); |
|
83 | + if (!empty($tenant)) { |
|
84 | + $adjustedEndpoints = [ |
|
85 | + 'authorize_url' => str_replace('/common/', '/' . $tenant . '/', $this->authorizeUrl), |
|
86 | + 'access_token_url' => str_replace('/common/', '/' . $tenant . '/', $this->accessTokenUrl), |
|
87 | + ]; |
|
88 | + |
|
89 | + $this->setApiEndpoints($adjustedEndpoints); |
|
90 | + } |
|
91 | + |
|
92 | + if ($this->isRefreshTokenAvailable()) { |
|
93 | + $this->tokenRefreshParameters += [ |
|
94 | + 'client_id' => $this->clientId, |
|
95 | + 'client_secret' => $this->clientSecret, |
|
96 | + ]; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * {@inheritdoc} |
|
102 | + */ |
|
103 | + public function getUserProfile() |
|
104 | + { |
|
105 | + $response = $this->apiRequest('me'); |
|
106 | + |
|
107 | + $data = new Data\Collection($response); |
|
108 | + |
|
109 | + if (!$data->exists('id')) { |
|
110 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
111 | + } |
|
112 | + |
|
113 | + $userProfile = new User\Profile(); |
|
114 | + |
|
115 | + $userProfile->identifier = $data->get('id'); |
|
116 | + $userProfile->displayName = $data->get('displayName'); |
|
117 | + $userProfile->firstName = $data->get('givenName'); |
|
118 | + $userProfile->lastName = $data->get('surname'); |
|
119 | + $userProfile->language = $data->get('preferredLanguage'); |
|
120 | + |
|
121 | + $userProfile->phone = $data->get('mobilePhone'); |
|
122 | + if (empty($userProfile->phone)) { |
|
123 | + $businessPhones = $data->get('businessPhones'); |
|
124 | + if (isset($businessPhones[0])) { |
|
125 | + $userProfile->phone = $businessPhones[0]; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + $userProfile->email = $data->get('mail'); |
|
130 | + if (empty($userProfile->email)) { |
|
131 | + $email = $data->get('userPrincipalName'); |
|
132 | + if (strpos($email, '@') !== false) { |
|
133 | + $userProfile->email = $email; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + return $userProfile; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * {@inheritdoc} |
|
142 | + */ |
|
143 | + public function getUserContacts() |
|
144 | + { |
|
145 | + $apiUrl = 'me/contacts?$top=50'; |
|
146 | + $contacts = []; |
|
147 | + |
|
148 | + do { |
|
149 | + $response = $this->apiRequest($apiUrl); |
|
150 | + $data = new Data\Collection($response); |
|
151 | + if (!$data->exists('value')) { |
|
152 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
153 | + } |
|
154 | + foreach ($data->filter('value')->toArray() as $entry) { |
|
155 | + $entry = new Data\Collection($entry); |
|
156 | + $userContact = new User\Contact(); |
|
157 | + $userContact->identifier = $entry->get('id'); |
|
158 | + $userContact->displayName = $entry->get('displayName'); |
|
159 | + $emailAddresses = $entry->get('emailAddresses'); |
|
160 | + if (!empty($emailAddresses)) { |
|
161 | + $userContact->email = $emailAddresses[0]->address; |
|
162 | + } |
|
163 | + // only add to collection if we have usefull data |
|
164 | + if (!empty($userContact->displayName) || !empty($userContact->email)) { |
|
165 | + $contacts[] = $userContact; |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + if ($data->exists('@odata.nextLink')) { |
|
170 | + $apiUrl = $data->get('@odata.nextLink'); |
|
171 | + |
|
172 | + $pagedList = true; |
|
173 | + } else { |
|
174 | + $pagedList = false; |
|
175 | + } |
|
176 | + } while ($pagedList); |
|
177 | + |
|
178 | + return $contacts; |
|
179 | + } |
|
180 | 180 | } |
@@ -46,155 +46,155 @@ |
||
46 | 46 | */ |
47 | 47 | class Google extends OAuth2 |
48 | 48 | { |
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - // phpcs:ignore |
|
53 | - protected $scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - protected $apiBaseUrl = 'https://www.googleapis.com/'; |
|
59 | - |
|
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected $authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; |
|
64 | - |
|
65 | - /** |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - protected $accessTokenUrl = 'https://oauth2.googleapis.com/token'; |
|
69 | - |
|
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - protected $apiDocumentation = 'https://developers.google.com/identity/protocols/OAuth2'; |
|
74 | - |
|
75 | - /** |
|
76 | - * {@inheritdoc} |
|
77 | - */ |
|
78 | - protected function initialize() |
|
79 | - { |
|
80 | - parent::initialize(); |
|
81 | - |
|
82 | - $this->AuthorizeUrlParameters += [ |
|
83 | - 'access_type' => 'offline', |
|
84 | - 'prompt' => 'consent' |
|
85 | - ]; |
|
86 | - |
|
87 | - if ($this->isRefreshTokenAvailable()) { |
|
88 | - $this->tokenRefreshParameters += [ |
|
89 | - 'client_id' => $this->clientId, |
|
90 | - 'client_secret' => $this->clientSecret, |
|
91 | - ]; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * {@inheritdoc} |
|
97 | - * |
|
98 | - * See: https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo |
|
99 | - */ |
|
100 | - public function getUserProfile() |
|
101 | - { |
|
102 | - $response = $this->apiRequest('oauth2/v3/userinfo'); |
|
103 | - |
|
104 | - $data = new Data\Collection($response); |
|
105 | - |
|
106 | - if (!$data->exists('sub')) { |
|
107 | - throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
108 | - } |
|
109 | - |
|
110 | - $userProfile = new User\Profile(); |
|
111 | - |
|
112 | - $userProfile->identifier = $data->get('sub'); |
|
113 | - $userProfile->firstName = $data->get('given_name'); |
|
114 | - $userProfile->lastName = $data->get('family_name'); |
|
115 | - $userProfile->displayName = $data->get('name'); |
|
116 | - $userProfile->photoURL = $data->get('picture'); |
|
117 | - $userProfile->profileURL = $data->get('profile'); |
|
118 | - $userProfile->gender = $data->get('gender'); |
|
119 | - $userProfile->language = $data->get('locale'); |
|
120 | - $userProfile->email = $data->get('email'); |
|
121 | - |
|
122 | - $userProfile->emailVerified = $data->get('email_verified') ? $userProfile->email : ''; |
|
123 | - |
|
124 | - if ($this->config->get('photo_size')) { |
|
125 | - $userProfile->photoURL .= '?sz=' . $this->config->get('photo_size'); |
|
126 | - } |
|
127 | - |
|
128 | - return $userProfile; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * {@inheritdoc} |
|
133 | - */ |
|
134 | - public function getUserContacts($parameters = []) |
|
135 | - { |
|
136 | - $parameters = ['max-results' => 500] + $parameters; |
|
137 | - |
|
138 | - // Google Gmail and Android contacts |
|
139 | - if (false !== strpos($this->scope, '/m8/feeds/') || false !== strpos($this->scope, '/auth/contacts.readonly')) { |
|
140 | - return $this->getGmailContacts($parameters); |
|
141 | - } |
|
142 | - |
|
143 | - return []; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Retrieve Gmail contacts |
|
148 | - * |
|
149 | - * @param array $parameters |
|
150 | - * |
|
151 | - * @return array |
|
152 | - * |
|
153 | - * @throws \Exception |
|
154 | - */ |
|
155 | - protected function getGmailContacts($parameters = []) |
|
156 | - { |
|
157 | - $url = 'https://www.google.com/m8/feeds/contacts/default/full?' |
|
158 | - . http_build_query(array_replace(['alt' => 'json', 'v' => '3.0'], (array)$parameters)); |
|
159 | - |
|
160 | - $response = $this->apiRequest($url); |
|
161 | - |
|
162 | - if (!$response) { |
|
163 | - return []; |
|
164 | - } |
|
165 | - |
|
166 | - $contacts = []; |
|
167 | - |
|
168 | - if (isset($response->feed->entry)) { |
|
169 | - foreach ($response->feed->entry as $idx => $entry) { |
|
170 | - $uc = new User\Contact(); |
|
171 | - |
|
172 | - $uc->email = isset($entry->{'gd$email'}[0]->address) |
|
173 | - ? (string)$entry->{'gd$email'}[0]->address |
|
174 | - : ''; |
|
175 | - |
|
176 | - $uc->displayName = isset($entry->title->{'$t'}) ? (string)$entry->title->{'$t'} : ''; |
|
177 | - $uc->identifier = ($uc->email != '') ? $uc->email : ''; |
|
178 | - $uc->description = ''; |
|
179 | - |
|
180 | - if (property_exists($response, 'website')) { |
|
181 | - if (is_array($response->website)) { |
|
182 | - foreach ($response->website as $w) { |
|
183 | - if ($w->primary == true) { |
|
184 | - $uc->webSiteURL = $w->value; |
|
185 | - } |
|
186 | - } |
|
187 | - } else { |
|
188 | - $uc->webSiteURL = $response->website->value; |
|
189 | - } |
|
190 | - } else { |
|
191 | - $uc->webSiteURL = ''; |
|
192 | - } |
|
193 | - |
|
194 | - $contacts[] = $uc; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - return $contacts; |
|
199 | - } |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + // phpcs:ignore |
|
53 | + protected $scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'; |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + protected $apiBaseUrl = 'https://www.googleapis.com/'; |
|
59 | + |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected $authorizeUrl = 'https://accounts.google.com/o/oauth2/v2/auth'; |
|
64 | + |
|
65 | + /** |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + protected $accessTokenUrl = 'https://oauth2.googleapis.com/token'; |
|
69 | + |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + protected $apiDocumentation = 'https://developers.google.com/identity/protocols/OAuth2'; |
|
74 | + |
|
75 | + /** |
|
76 | + * {@inheritdoc} |
|
77 | + */ |
|
78 | + protected function initialize() |
|
79 | + { |
|
80 | + parent::initialize(); |
|
81 | + |
|
82 | + $this->AuthorizeUrlParameters += [ |
|
83 | + 'access_type' => 'offline', |
|
84 | + 'prompt' => 'consent' |
|
85 | + ]; |
|
86 | + |
|
87 | + if ($this->isRefreshTokenAvailable()) { |
|
88 | + $this->tokenRefreshParameters += [ |
|
89 | + 'client_id' => $this->clientId, |
|
90 | + 'client_secret' => $this->clientSecret, |
|
91 | + ]; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * {@inheritdoc} |
|
97 | + * |
|
98 | + * See: https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo |
|
99 | + */ |
|
100 | + public function getUserProfile() |
|
101 | + { |
|
102 | + $response = $this->apiRequest('oauth2/v3/userinfo'); |
|
103 | + |
|
104 | + $data = new Data\Collection($response); |
|
105 | + |
|
106 | + if (!$data->exists('sub')) { |
|
107 | + throw new UnexpectedApiResponseException('Provider API returned an unexpected response.'); |
|
108 | + } |
|
109 | + |
|
110 | + $userProfile = new User\Profile(); |
|
111 | + |
|
112 | + $userProfile->identifier = $data->get('sub'); |
|
113 | + $userProfile->firstName = $data->get('given_name'); |
|
114 | + $userProfile->lastName = $data->get('family_name'); |
|
115 | + $userProfile->displayName = $data->get('name'); |
|
116 | + $userProfile->photoURL = $data->get('picture'); |
|
117 | + $userProfile->profileURL = $data->get('profile'); |
|
118 | + $userProfile->gender = $data->get('gender'); |
|
119 | + $userProfile->language = $data->get('locale'); |
|
120 | + $userProfile->email = $data->get('email'); |
|
121 | + |
|
122 | + $userProfile->emailVerified = $data->get('email_verified') ? $userProfile->email : ''; |
|
123 | + |
|
124 | + if ($this->config->get('photo_size')) { |
|
125 | + $userProfile->photoURL .= '?sz=' . $this->config->get('photo_size'); |
|
126 | + } |
|
127 | + |
|
128 | + return $userProfile; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * {@inheritdoc} |
|
133 | + */ |
|
134 | + public function getUserContacts($parameters = []) |
|
135 | + { |
|
136 | + $parameters = ['max-results' => 500] + $parameters; |
|
137 | + |
|
138 | + // Google Gmail and Android contacts |
|
139 | + if (false !== strpos($this->scope, '/m8/feeds/') || false !== strpos($this->scope, '/auth/contacts.readonly')) { |
|
140 | + return $this->getGmailContacts($parameters); |
|
141 | + } |
|
142 | + |
|
143 | + return []; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Retrieve Gmail contacts |
|
148 | + * |
|
149 | + * @param array $parameters |
|
150 | + * |
|
151 | + * @return array |
|
152 | + * |
|
153 | + * @throws \Exception |
|
154 | + */ |
|
155 | + protected function getGmailContacts($parameters = []) |
|
156 | + { |
|
157 | + $url = 'https://www.google.com/m8/feeds/contacts/default/full?' |
|
158 | + . http_build_query(array_replace(['alt' => 'json', 'v' => '3.0'], (array)$parameters)); |
|
159 | + |
|
160 | + $response = $this->apiRequest($url); |
|
161 | + |
|
162 | + if (!$response) { |
|
163 | + return []; |
|
164 | + } |
|
165 | + |
|
166 | + $contacts = []; |
|
167 | + |
|
168 | + if (isset($response->feed->entry)) { |
|
169 | + foreach ($response->feed->entry as $idx => $entry) { |
|
170 | + $uc = new User\Contact(); |
|
171 | + |
|
172 | + $uc->email = isset($entry->{'gd$email'}[0]->address) |
|
173 | + ? (string)$entry->{'gd$email'}[0]->address |
|
174 | + : ''; |
|
175 | + |
|
176 | + $uc->displayName = isset($entry->title->{'$t'}) ? (string)$entry->title->{'$t'} : ''; |
|
177 | + $uc->identifier = ($uc->email != '') ? $uc->email : ''; |
|
178 | + $uc->description = ''; |
|
179 | + |
|
180 | + if (property_exists($response, 'website')) { |
|
181 | + if (is_array($response->website)) { |
|
182 | + foreach ($response->website as $w) { |
|
183 | + if ($w->primary == true) { |
|
184 | + $uc->webSiteURL = $w->value; |
|
185 | + } |
|
186 | + } |
|
187 | + } else { |
|
188 | + $uc->webSiteURL = $response->website->value; |
|
189 | + } |
|
190 | + } else { |
|
191 | + $uc->webSiteURL = ''; |
|
192 | + } |
|
193 | + |
|
194 | + $contacts[] = $uc; |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + return $contacts; |
|
199 | + } |
|
200 | 200 | } |
@@ -24,246 +24,246 @@ |
||
24 | 24 | */ |
25 | 25 | class Hybridauth |
26 | 26 | { |
27 | - /** |
|
28 | - * Hybridauth config. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $config; |
|
33 | - |
|
34 | - /** |
|
35 | - * Storage. |
|
36 | - * |
|
37 | - * @var StorageInterface |
|
38 | - */ |
|
39 | - protected $storage; |
|
40 | - |
|
41 | - /** |
|
42 | - * HttpClient. |
|
43 | - * |
|
44 | - * @var HttpClientInterface |
|
45 | - */ |
|
46 | - protected $httpClient; |
|
47 | - |
|
48 | - /** |
|
49 | - * Logger. |
|
50 | - * |
|
51 | - * @var LoggerInterface |
|
52 | - */ |
|
53 | - protected $logger; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param array|string $config Array with configuration or Path to PHP file that will return array |
|
57 | - * @param HttpClientInterface $httpClient |
|
58 | - * @param StorageInterface $storage |
|
59 | - * @param LoggerInterface $logger |
|
60 | - * |
|
61 | - * @throws InvalidArgumentException |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - $config, |
|
65 | - HttpClientInterface $httpClient = null, |
|
66 | - StorageInterface $storage = null, |
|
67 | - LoggerInterface $logger = null |
|
68 | - ) { |
|
69 | - if (is_string($config) && file_exists($config)) { |
|
70 | - $config = include $config; |
|
71 | - } elseif (!is_array($config)) { |
|
72 | - throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
|
73 | - } |
|
74 | - |
|
75 | - $this->config = $config + [ |
|
76 | - 'debug_mode' => Logger::NONE, |
|
77 | - 'debug_file' => '', |
|
78 | - 'curl_options' => null, |
|
79 | - 'providers' => [] |
|
80 | - ]; |
|
81 | - $this->storage = $storage; |
|
82 | - $this->logger = $logger; |
|
83 | - $this->httpClient = $httpClient; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Instantiate the given provider and authentication or authorization protocol. |
|
88 | - * |
|
89 | - * If not authenticated yet, the user will be redirected to the provider's site for |
|
90 | - * authentication/authorisation, otherwise it will simply return an instance of |
|
91 | - * provider's adapter. |
|
92 | - * |
|
93 | - * @param string $name adapter's name (case insensitive) |
|
94 | - * |
|
95 | - * @return \Hybridauth\Adapter\AdapterInterface |
|
96 | - * @throws InvalidArgumentException |
|
97 | - * @throws UnexpectedValueException |
|
98 | - */ |
|
99 | - public function authenticate($name) |
|
100 | - { |
|
101 | - $adapter = $this->getAdapter($name); |
|
102 | - |
|
103 | - $adapter->authenticate(); |
|
104 | - |
|
105 | - return $adapter; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns a new instance of a provider's adapter by name |
|
110 | - * |
|
111 | - * @param string $name adapter's name (case insensitive) |
|
112 | - * |
|
113 | - * @return \Hybridauth\Adapter\AdapterInterface |
|
114 | - * @throws InvalidArgumentException |
|
115 | - * @throws UnexpectedValueException |
|
116 | - */ |
|
117 | - public function getAdapter($name) |
|
118 | - { |
|
119 | - $config = $this->getProviderConfig($name); |
|
120 | - |
|
121 | - $adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
|
122 | - |
|
123 | - if (!class_exists($adapter)) { |
|
124 | - $unexistingConfiguredAdapter = $adapter; |
|
125 | - $adapter = null; |
|
126 | - $fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
|
127 | - /** @var \SplFileInfo $file */ |
|
128 | - foreach ($fs as $file) { |
|
129 | - if (!$file->isDir()) { |
|
130 | - $provider = strtok($file->getFilename(), '.'); |
|
131 | - if (mb_strtolower($name) === mb_strtolower($provider)) { |
|
132 | - $adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
|
133 | - break; |
|
134 | - } |
|
135 | - } |
|
136 | - } |
|
137 | - if ($adapter === null) { |
|
138 | - throw new InvalidArgumentException("Unknown Provider (name: $name / configured: $unexistingConfiguredAdapter)."); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - return new $adapter($config, $this->httpClient, $this->storage, $this->logger); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get provider config by name. |
|
147 | - * |
|
148 | - * @param string $name adapter's name (case insensitive) |
|
149 | - * |
|
150 | - * @throws UnexpectedValueException |
|
151 | - * @throws InvalidArgumentException |
|
152 | - * |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public function getProviderConfig($name) |
|
156 | - { |
|
157 | - $name = strtolower($name); |
|
158 | - |
|
159 | - $providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
|
160 | - |
|
161 | - if (!isset($providersConfig[$name])) { |
|
162 | - throw new InvalidArgumentException('Unknown Provider.'); |
|
163 | - } |
|
164 | - |
|
165 | - if (!$providersConfig[$name]['enabled']) { |
|
166 | - throw new UnexpectedValueException('Disabled Provider.'); |
|
167 | - } |
|
168 | - |
|
169 | - $config = $providersConfig[$name]; |
|
170 | - $config += [ |
|
171 | - 'debug_mode' => $this->config['debug_mode'], |
|
172 | - 'debug_file' => $this->config['debug_file'], |
|
173 | - ]; |
|
174 | - |
|
175 | - if (!isset($config['callback']) && isset($this->config['callback'])) { |
|
176 | - $config['callback'] = $this->config['callback']; |
|
177 | - } |
|
178 | - |
|
179 | - return $config; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Returns a boolean of whether the user is connected with a provider |
|
184 | - * |
|
185 | - * @param string $name adapter's name (case insensitive) |
|
186 | - * |
|
187 | - * @return bool |
|
188 | - * @throws InvalidArgumentException |
|
189 | - * @throws UnexpectedValueException |
|
190 | - */ |
|
191 | - public function isConnectedWith($name) |
|
192 | - { |
|
193 | - return $this->getAdapter($name)->isConnected(); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Returns a list of enabled adapters names |
|
198 | - * |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - public function getProviders() |
|
202 | - { |
|
203 | - $providers = []; |
|
204 | - |
|
205 | - foreach ($this->config['providers'] as $name => $config) { |
|
206 | - if ($config['enabled']) { |
|
207 | - $providers[] = $name; |
|
208 | - } |
|
209 | - } |
|
210 | - |
|
211 | - return $providers; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns a list of currently connected adapters names |
|
216 | - * |
|
217 | - * @return array |
|
218 | - * @throws InvalidArgumentException |
|
219 | - * @throws UnexpectedValueException |
|
220 | - */ |
|
221 | - public function getConnectedProviders() |
|
222 | - { |
|
223 | - $providers = []; |
|
224 | - |
|
225 | - foreach ($this->getProviders() as $name) { |
|
226 | - if ($this->isConnectedWith($name)) { |
|
227 | - $providers[] = $name; |
|
228 | - } |
|
229 | - } |
|
230 | - |
|
231 | - return $providers; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Returns a list of new instances of currently connected adapters |
|
236 | - * |
|
237 | - * @return \Hybridauth\Adapter\AdapterInterface[] |
|
238 | - * @throws InvalidArgumentException |
|
239 | - * @throws UnexpectedValueException |
|
240 | - */ |
|
241 | - public function getConnectedAdapters() |
|
242 | - { |
|
243 | - $adapters = []; |
|
244 | - |
|
245 | - foreach ($this->getProviders() as $name) { |
|
246 | - $adapter = $this->getAdapter($name); |
|
247 | - |
|
248 | - if ($adapter->isConnected()) { |
|
249 | - $adapters[$name] = $adapter; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - return $adapters; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Disconnect all currently connected adapters at once |
|
258 | - */ |
|
259 | - public function disconnectAllAdapters() |
|
260 | - { |
|
261 | - foreach ($this->getProviders() as $name) { |
|
262 | - $adapter = $this->getAdapter($name); |
|
263 | - |
|
264 | - if ($adapter->isConnected()) { |
|
265 | - $adapter->disconnect(); |
|
266 | - } |
|
267 | - } |
|
268 | - } |
|
27 | + /** |
|
28 | + * Hybridauth config. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $config; |
|
33 | + |
|
34 | + /** |
|
35 | + * Storage. |
|
36 | + * |
|
37 | + * @var StorageInterface |
|
38 | + */ |
|
39 | + protected $storage; |
|
40 | + |
|
41 | + /** |
|
42 | + * HttpClient. |
|
43 | + * |
|
44 | + * @var HttpClientInterface |
|
45 | + */ |
|
46 | + protected $httpClient; |
|
47 | + |
|
48 | + /** |
|
49 | + * Logger. |
|
50 | + * |
|
51 | + * @var LoggerInterface |
|
52 | + */ |
|
53 | + protected $logger; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param array|string $config Array with configuration or Path to PHP file that will return array |
|
57 | + * @param HttpClientInterface $httpClient |
|
58 | + * @param StorageInterface $storage |
|
59 | + * @param LoggerInterface $logger |
|
60 | + * |
|
61 | + * @throws InvalidArgumentException |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + $config, |
|
65 | + HttpClientInterface $httpClient = null, |
|
66 | + StorageInterface $storage = null, |
|
67 | + LoggerInterface $logger = null |
|
68 | + ) { |
|
69 | + if (is_string($config) && file_exists($config)) { |
|
70 | + $config = include $config; |
|
71 | + } elseif (!is_array($config)) { |
|
72 | + throw new InvalidArgumentException('Hybridauth config does not exist on the given path.'); |
|
73 | + } |
|
74 | + |
|
75 | + $this->config = $config + [ |
|
76 | + 'debug_mode' => Logger::NONE, |
|
77 | + 'debug_file' => '', |
|
78 | + 'curl_options' => null, |
|
79 | + 'providers' => [] |
|
80 | + ]; |
|
81 | + $this->storage = $storage; |
|
82 | + $this->logger = $logger; |
|
83 | + $this->httpClient = $httpClient; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Instantiate the given provider and authentication or authorization protocol. |
|
88 | + * |
|
89 | + * If not authenticated yet, the user will be redirected to the provider's site for |
|
90 | + * authentication/authorisation, otherwise it will simply return an instance of |
|
91 | + * provider's adapter. |
|
92 | + * |
|
93 | + * @param string $name adapter's name (case insensitive) |
|
94 | + * |
|
95 | + * @return \Hybridauth\Adapter\AdapterInterface |
|
96 | + * @throws InvalidArgumentException |
|
97 | + * @throws UnexpectedValueException |
|
98 | + */ |
|
99 | + public function authenticate($name) |
|
100 | + { |
|
101 | + $adapter = $this->getAdapter($name); |
|
102 | + |
|
103 | + $adapter->authenticate(); |
|
104 | + |
|
105 | + return $adapter; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns a new instance of a provider's adapter by name |
|
110 | + * |
|
111 | + * @param string $name adapter's name (case insensitive) |
|
112 | + * |
|
113 | + * @return \Hybridauth\Adapter\AdapterInterface |
|
114 | + * @throws InvalidArgumentException |
|
115 | + * @throws UnexpectedValueException |
|
116 | + */ |
|
117 | + public function getAdapter($name) |
|
118 | + { |
|
119 | + $config = $this->getProviderConfig($name); |
|
120 | + |
|
121 | + $adapter = isset($config['adapter']) ? $config['adapter'] : sprintf('Hybridauth\\Provider\\%s', $name); |
|
122 | + |
|
123 | + if (!class_exists($adapter)) { |
|
124 | + $unexistingConfiguredAdapter = $adapter; |
|
125 | + $adapter = null; |
|
126 | + $fs = new \FilesystemIterator(__DIR__ . '/Provider/'); |
|
127 | + /** @var \SplFileInfo $file */ |
|
128 | + foreach ($fs as $file) { |
|
129 | + if (!$file->isDir()) { |
|
130 | + $provider = strtok($file->getFilename(), '.'); |
|
131 | + if (mb_strtolower($name) === mb_strtolower($provider)) { |
|
132 | + $adapter = sprintf('Hybridauth\\Provider\\%s', $provider); |
|
133 | + break; |
|
134 | + } |
|
135 | + } |
|
136 | + } |
|
137 | + if ($adapter === null) { |
|
138 | + throw new InvalidArgumentException("Unknown Provider (name: $name / configured: $unexistingConfiguredAdapter)."); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + return new $adapter($config, $this->httpClient, $this->storage, $this->logger); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get provider config by name. |
|
147 | + * |
|
148 | + * @param string $name adapter's name (case insensitive) |
|
149 | + * |
|
150 | + * @throws UnexpectedValueException |
|
151 | + * @throws InvalidArgumentException |
|
152 | + * |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public function getProviderConfig($name) |
|
156 | + { |
|
157 | + $name = strtolower($name); |
|
158 | + |
|
159 | + $providersConfig = array_change_key_case($this->config['providers'], CASE_LOWER); |
|
160 | + |
|
161 | + if (!isset($providersConfig[$name])) { |
|
162 | + throw new InvalidArgumentException('Unknown Provider.'); |
|
163 | + } |
|
164 | + |
|
165 | + if (!$providersConfig[$name]['enabled']) { |
|
166 | + throw new UnexpectedValueException('Disabled Provider.'); |
|
167 | + } |
|
168 | + |
|
169 | + $config = $providersConfig[$name]; |
|
170 | + $config += [ |
|
171 | + 'debug_mode' => $this->config['debug_mode'], |
|
172 | + 'debug_file' => $this->config['debug_file'], |
|
173 | + ]; |
|
174 | + |
|
175 | + if (!isset($config['callback']) && isset($this->config['callback'])) { |
|
176 | + $config['callback'] = $this->config['callback']; |
|
177 | + } |
|
178 | + |
|
179 | + return $config; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Returns a boolean of whether the user is connected with a provider |
|
184 | + * |
|
185 | + * @param string $name adapter's name (case insensitive) |
|
186 | + * |
|
187 | + * @return bool |
|
188 | + * @throws InvalidArgumentException |
|
189 | + * @throws UnexpectedValueException |
|
190 | + */ |
|
191 | + public function isConnectedWith($name) |
|
192 | + { |
|
193 | + return $this->getAdapter($name)->isConnected(); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Returns a list of enabled adapters names |
|
198 | + * |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + public function getProviders() |
|
202 | + { |
|
203 | + $providers = []; |
|
204 | + |
|
205 | + foreach ($this->config['providers'] as $name => $config) { |
|
206 | + if ($config['enabled']) { |
|
207 | + $providers[] = $name; |
|
208 | + } |
|
209 | + } |
|
210 | + |
|
211 | + return $providers; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns a list of currently connected adapters names |
|
216 | + * |
|
217 | + * @return array |
|
218 | + * @throws InvalidArgumentException |
|
219 | + * @throws UnexpectedValueException |
|
220 | + */ |
|
221 | + public function getConnectedProviders() |
|
222 | + { |
|
223 | + $providers = []; |
|
224 | + |
|
225 | + foreach ($this->getProviders() as $name) { |
|
226 | + if ($this->isConnectedWith($name)) { |
|
227 | + $providers[] = $name; |
|
228 | + } |
|
229 | + } |
|
230 | + |
|
231 | + return $providers; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Returns a list of new instances of currently connected adapters |
|
236 | + * |
|
237 | + * @return \Hybridauth\Adapter\AdapterInterface[] |
|
238 | + * @throws InvalidArgumentException |
|
239 | + * @throws UnexpectedValueException |
|
240 | + */ |
|
241 | + public function getConnectedAdapters() |
|
242 | + { |
|
243 | + $adapters = []; |
|
244 | + |
|
245 | + foreach ($this->getProviders() as $name) { |
|
246 | + $adapter = $this->getAdapter($name); |
|
247 | + |
|
248 | + if ($adapter->isConnected()) { |
|
249 | + $adapters[$name] = $adapter; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + return $adapters; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Disconnect all currently connected adapters at once |
|
258 | + */ |
|
259 | + public function disconnectAllAdapters() |
|
260 | + { |
|
261 | + foreach ($this->getProviders() as $name) { |
|
262 | + $adapter = $this->getAdapter($name); |
|
263 | + |
|
264 | + if ($adapter->isConnected()) { |
|
265 | + $adapter->disconnect(); |
|
266 | + } |
|
267 | + } |
|
268 | + } |
|
269 | 269 | } |