-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.php
125 lines (107 loc) · 3.45 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
<?php
namespace SocialiteProviders\AutodeskAPS;
use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'AUTODESKAPS';
protected $scopeSeparator = ' ';
protected function getAuthUrl($state): string
{
return
$this->buildAuthUrlFromBase(
'https://developer.api.autodesk.com/authentication/v2/authorize',
$state
);
}
protected function getTokenUrl(): string
{
return 'https://developer.api.autodesk.com/authentication/v2/token';
}
/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator),
'response_type' => 'code',
];
if ($this->usesState()) {
$fields['state'] = $state;
}
if ($this->usesPKCE()) {
$fields['code_challenge'] = $this->getCodeChallenge();
$fields['code_challenge_method'] = $this->getCodeChallengeMethod();
}
return array_merge($fields, $this->parameters);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
$fields = [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $this->redirectUrl,
];
if ($this->usesPKCE()) {
$fields['code_verifier'] = $this->request->session()->pull('code_verifier');
}
return array_merge($fields, $this->parameters);
}
/**
* {@inheritdoc}
*/
protected function getTokenHeaders($code): array
{
$base64 = base64_encode("{$this->clientId}:{$this->clientSecret}");
return [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic '.$base64,
];
}
/**
* @param string $token
* @return array
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getUserByToken($token): array
{
$response = $this->getHttpClient()->get(
'https://api.userprofile.autodesk.com/userinfo',
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]
);
return json_decode((string) $response->getBody(), true);
}
/**
* @see https://aps.autodesk.com/en/docs/oauth/v2/reference/http/userinfo-GET/.
*
* {@inheritdoc}
*/
protected function mapUserToObject(array $user): User
{
return (new User)->setRaw($user)->map([
'id' => $user['sub'],
'email' => $user['email'],
'email_verified' => $user['email_verified'],
'username' => $user['preferred_username'],
'full_name' => $user['name'],
'first_name' => $user['given_name'],
'last_name' => $user['family_name'],
'language' => $user['locale'],
'image' => $user['picture'],
'website' => $user['profile'] ?? null,
]);
}
}