forked from wondeltd/Socialite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.php
executable file
·175 lines (161 loc) · 4.49 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace SocialiteProviders\Wonde;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'WONDE';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(\env('WONDE_SSO_URL', 'https://edu.wonde.com/').'oauth/authorize', $state).'&mode='.\env('WONDE_SSO_MODE');
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return \env('WONDE_API_URL', 'https://api.wonde.com/').'oauth/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$query = $this->getUserQuery();
$response = $this->getHttpClient()->get(\env('WONDE_API_URL', 'https://api.wonde.com/').'graphql/me?query=' . $query, [
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* @param array $user
*/
protected function getName(array $user)
{
return $user['data']['Me']['Person']['forename'] . ' ' . $user['data']['Me']['Person']['surname'];
}
/**
* Get the users email address.
*
* @param array $user
*
* @return string|null
*/
protected function getEmail(array $user)
{
if (!empty($user['data']['Me']['Person']['ContactDetails']['email'])) {
return $user['data']['Me']['Person']['ContactDetails']['email'];
} else {
return null;
}
}
/**
* Get the users mobile.
*
* @param array $user
*
* @return string|null
*/
protected function getMobile(array $user)
{
if (!empty($user['data']['Me']['Person']['ContactDetails']['telephone_mobile'])) {
return $user['data']['Me']['Person']['ContactDetails']['telephone_mobile'];
} else {
return null;
}
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['data']['Me']['Person']['id'] ?? null,
'nickname' => null,
'name' => $this->getName($user),
'email' => $this->getEmail($user),
'mobile' => $this->getMobile($user),
'avatar' => null,
'school' => $user['data']['Me']['Person']['School'] ?? [],
]);
}
/**
* Returns the url encoded qraphql query for the current user
* @return string
*/
protected function getUserQuery()
{
$query = <<<'GRAPHQL'
{
Me{
Person{
__typename
...on Employee {
id
title
surname
forename
School {
id
name
establishment_number
la_code
urn
address_line_1
address_line_2
address_town
address_postcode
country
}
ContactDetails {
email
telephone_mobile
}
},
...on Student {
id
surname
forename
School {
id
name
establishment_number
la_code
urn
address_line_1
address_line_2
address_town
address_postcode
country
}
}
...on Contact {
id
surname
forename
}
}
}
}
GRAPHQL;
return urlencode($query);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
}