friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class Patreon
4 *
5 * @created 04.03.2019
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2019 smiley
8 * @license MIT
9 *
10 * @noinspection PhpUnused
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Providers;
15
16use chillerlan\OAuth\Core\{AuthenticatedUser, CSRFToken, OAuth2Provider, TokenRefresh, UserInfo};
17use function in_array;
18
19/**
20 * Patreon v2 OAuth2
21 *
22 * @link https://docs.patreon.com/
23 * @link https://docs.patreon.com/#oauth
24 * @link https://docs.patreon.com/#apiv2-oauth
25 */
26class Patreon extends OAuth2Provider implements CSRFToken, TokenRefresh, UserInfo{
27
28 public const IDENTIFIER = 'PATREON';
29
30 public const SCOPE_V1_USERS = 'users';
31 public const SCOPE_V1_PLEDGES_TO_ME = 'pledges-to-me';
32 public const SCOPE_V1_MY_CAMPAIGN = 'my-campaign';
33
34 // wow, consistency...
35 public const SCOPE_V2_IDENTITY = 'identity';
36 public const SCOPE_V2_IDENTITY_EMAIL = 'identity[email]';
37 public const SCOPE_V2_IDENTITY_MEMBERSHIPS = 'identity.memberships';
38 public const SCOPE_V2_CAMPAIGNS = 'campaigns';
39 public const SCOPE_V2_CAMPAIGNS_WEBHOOK = 'w:campaigns.webhook';
40 public const SCOPE_V2_CAMPAIGNS_MEMBERS = 'campaigns.members';
41 public const SCOPE_V2_CAMPAIGNS_MEMBERS_EMAIL = 'campaigns.members[email]';
42 public const SCOPE_V2_CAMPAIGNS_MEMBERS_ADDRESS = 'campaigns.members.address';
43
44 public const DEFAULT_SCOPES = [
45 self::SCOPE_V2_IDENTITY,
46 self::SCOPE_V2_IDENTITY_EMAIL,
47 self::SCOPE_V2_IDENTITY_MEMBERSHIPS,
48 self::SCOPE_V2_CAMPAIGNS,
49 self::SCOPE_V2_CAMPAIGNS_MEMBERS,
50 ];
51
52 protected string $authorizationURL = 'https://www.patreon.com/oauth2/authorize';
53 protected string $accessTokenURL = 'https://www.patreon.com/api/oauth2/token';
54 protected string $apiURL = 'https://www.patreon.com/api/oauth2';
55 protected string|null $apiDocs = 'https://docs.patreon.com/';
56 protected string|null $applicationURL = 'https://www.patreon.com/portal/registration/register-clients';
57
58 public function me():AuthenticatedUser{
59 $token = $this->storage->getAccessToken($this->name);
60
61 if(in_array($this::SCOPE_V2_IDENTITY, $token->scopes, true)){
62 $endpoint = '/v2/identity';
63 $params = [
64 'fields[user]' => 'about,created,email,first_name,full_name,image_url,'.
65 'last_name,social_connections,thumb_url,url,vanity',
66 ];
67 }
68 elseif(in_array($this::SCOPE_V1_USERS, $token->scopes, true)){
69 $endpoint = '/api/current_user';
70 $params = [];
71 }
72 else{
73 throw new ProviderException('invalid scopes for the identity endpoint');
74 }
75
76 $json = $this->getMeResponseData($endpoint, $params);
77
78 $userdata = [
79 'data' => $json,
80 'handle' => $json['data']['attributes']['vanity'],
81 'avatar' => $json['data']['attributes']['image_url'],
82 'displayName' => $json['data']['attributes']['full_name'],
83 'email' => $json['data']['attributes']['email'],
84 'id' => $json['data']['id'],
85 'url' => $json['data']['attributes']['url'],
86 ];
87
88 return new AuthenticatedUser($userdata);
89 }
90
91}