-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatreon.php
91 lines (77 loc) · 3.09 KB
/
Patreon.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
<?php
/**
* Class Patreon
*
* @created 04.03.2019
* @author smiley <smiley@chillerlan.net>
* @copyright 2019 smiley
* @license MIT
*
* @noinspection PhpUnused
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Providers;
use chillerlan\OAuth\Core\{AuthenticatedUser, CSRFToken, OAuth2Provider, TokenRefresh, UserInfo};
use function in_array;
/**
* Patreon v2 OAuth2
*
* @link https://docs.patreon.com/
* @link https://docs.patreon.com/#oauth
* @link https://docs.patreon.com/#apiv2-oauth
*/
class Patreon extends OAuth2Provider implements CSRFToken, TokenRefresh, UserInfo{
public const IDENTIFIER = 'PATREON';
public const SCOPE_V1_USERS = 'users';
public const SCOPE_V1_PLEDGES_TO_ME = 'pledges-to-me';
public const SCOPE_V1_MY_CAMPAIGN = 'my-campaign';
// wow, consistency...
public const SCOPE_V2_IDENTITY = 'identity';
public const SCOPE_V2_IDENTITY_EMAIL = 'identity[email]';
public const SCOPE_V2_IDENTITY_MEMBERSHIPS = 'identity.memberships';
public const SCOPE_V2_CAMPAIGNS = 'campaigns';
public const SCOPE_V2_CAMPAIGNS_WEBHOOK = 'w:campaigns.webhook';
public const SCOPE_V2_CAMPAIGNS_MEMBERS = 'campaigns.members';
public const SCOPE_V2_CAMPAIGNS_MEMBERS_EMAIL = 'campaigns.members[email]';
public const SCOPE_V2_CAMPAIGNS_MEMBERS_ADDRESS = 'campaigns.members.address';
public const DEFAULT_SCOPES = [
self::SCOPE_V2_IDENTITY,
self::SCOPE_V2_IDENTITY_EMAIL,
self::SCOPE_V2_IDENTITY_MEMBERSHIPS,
self::SCOPE_V2_CAMPAIGNS,
self::SCOPE_V2_CAMPAIGNS_MEMBERS,
];
protected string $authorizationURL = 'https://www.patreon.com/oauth2/authorize';
protected string $accessTokenURL = 'https://www.patreon.com/api/oauth2/token';
protected string $apiURL = 'https://www.patreon.com/api/oauth2';
protected string|null $apiDocs = 'https://docs.patreon.com/';
protected string|null $applicationURL = 'https://www.patreon.com/portal/registration/register-clients';
public function me():AuthenticatedUser{
$token = $this->storage->getAccessToken($this->name);
if(in_array($this::SCOPE_V2_IDENTITY, $token->scopes, true)){
$endpoint = '/v2/identity';
$params = [
'fields[user]' => 'about,created,email,first_name,full_name,image_url,'.
'last_name,social_connections,thumb_url,url,vanity',
];
}
elseif(in_array($this::SCOPE_V1_USERS, $token->scopes, true)){
$endpoint = '/api/current_user';
$params = [];
}
else{
throw new ProviderException('invalid scopes for the identity endpoint');
}
$json = $this->getMeResponseData($endpoint, $params);
$userdata = [
'data' => $json,
'handle' => $json['data']['attributes']['vanity'],
'avatar' => $json['data']['attributes']['image_url'],
'displayName' => $json['data']['attributes']['full_name'],
'email' => $json['data']['attributes']['email'],
'id' => $json['data']['id'],
'url' => $json['data']['attributes']['url'],
];
return new AuthenticatedUser($userdata);
}
}