|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Patreon |
|
4
|
|
|
* |
|
5
|
|
|
* @created 04.03.2019 |
|
6
|
|
|
* @author smiley <[email protected]> |
|
7
|
|
|
* @copyright 2019 smiley |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
|
12
|
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
|
14
|
|
|
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException, TokenRefresh}; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use function explode; |
|
17
|
|
|
use function in_array; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @see https://docs.patreon.com/ |
|
22
|
|
|
* @see https://docs.patreon.com/#oauth |
|
23
|
|
|
* @see https://docs.patreon.com/#apiv2-oauth |
|
24
|
|
|
*/ |
|
25
|
|
|
class Patreon extends OAuth2Provider implements CSRFToken, TokenRefresh{ |
|
26
|
|
|
|
|
27
|
|
|
public const SCOPE_V1_USERS = 'users'; |
|
28
|
|
|
public const SCOPE_V1_PLEDGES_TO_ME = 'pledges-to-me'; |
|
29
|
|
|
public const SCOPE_V1_MY_CAMPAIGN = 'my-campaign'; |
|
30
|
|
|
|
|
31
|
|
|
// wow, consistency... |
|
32
|
|
|
public const SCOPE_V2_IDENTITY = 'identity'; |
|
33
|
|
|
public const SCOPE_V2_IDENTITY_EMAIL = 'identity[email]'; |
|
34
|
|
|
public const SCOPE_V2_IDENTITY_MEMBERSHIPS = 'identity.memberships'; |
|
35
|
|
|
public const SCOPE_V2_CAMPAIGNS = 'campaigns'; |
|
36
|
|
|
public const SCOPE_V2_CAMPAIGNS_WEBHOOK = 'w:campaigns.webhook'; |
|
37
|
|
|
public const SCOPE_V2_CAMPAIGNS_MEMBERS = 'campaigns.members'; |
|
38
|
|
|
public const SCOPE_V2_CAMPAIGNS_MEMBERS_EMAIL = 'campaigns.members[email]'; |
|
39
|
|
|
public const SCOPE_V2_CAMPAIGNS_MEMBERS_ADDRESS = 'campaigns.members.address'; |
|
40
|
|
|
|
|
41
|
|
|
protected string $authURL = 'https://www.patreon.com/oauth2/authorize'; |
|
42
|
|
|
protected string $accessTokenURL = 'https://www.patreon.com/api/oauth2/token'; |
|
43
|
|
|
protected string $apiURL = 'https://www.patreon.com/api/oauth2'; |
|
44
|
|
|
protected ?string $apiDocs = 'https://docs.patreon.com/'; |
|
45
|
|
|
protected ?string $applicationURL = 'https://www.patreon.com/portal/registration/register-clients'; |
|
46
|
|
|
|
|
47
|
|
|
protected array $defaultScopes = [ |
|
48
|
|
|
self::SCOPE_V2_IDENTITY, |
|
49
|
|
|
self::SCOPE_V2_IDENTITY_EMAIL, |
|
50
|
|
|
self::SCOPE_V2_IDENTITY_MEMBERSHIPS, |
|
51
|
|
|
self::SCOPE_V2_CAMPAIGNS, |
|
52
|
|
|
self::SCOPE_V2_CAMPAIGNS_MEMBERS, |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function me():ResponseInterface{ |
|
59
|
|
|
$token = $this->storage->getAccessToken($this->serviceName); |
|
60
|
|
|
$scopes = explode(' ', $token->extraParams['scope']); |
|
61
|
|
|
|
|
62
|
|
|
if(in_array(self::SCOPE_V2_IDENTITY, $scopes)){ |
|
63
|
|
|
$endpoint = '/v2/identity'; |
|
64
|
|
|
$params = ['fields[user]' => 'about,created,email,first_name,full_name,image_url,last_name,social_connections,thumb_url,url,vanity']; |
|
65
|
|
|
} |
|
66
|
|
|
elseif(in_array(self::SCOPE_V1_USERS, $scopes)){ |
|
67
|
|
|
$endpoint = '/api/current_user'; |
|
68
|
|
|
$params = []; |
|
69
|
|
|
} |
|
70
|
|
|
else{ |
|
71
|
|
|
throw new ProviderException('invalid scopes for the identity endpoint'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$response = $this->request($endpoint, $params); |
|
75
|
|
|
$status = $response->getStatusCode(); |
|
76
|
|
|
|
|
77
|
|
|
if($status === 200){ |
|
78
|
|
|
return $response; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$json = MessageUtil::decodeJSON($response); |
|
82
|
|
|
|
|
83
|
|
|
if(isset($json->errors[0]->code_name)){ |
|
84
|
|
|
throw new ProviderException($json->errors[0]->code_name); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|