friendship ended with social-app. php is my new best friend
1<?php 2/** 3 * Class BigCartel 4 * 5 * @created 10.04.2018 6 * @author smiley <smiley@chillerlan.net> 7 * @copyright 2018 Smiley 8 * @license MIT 9 */ 10declare(strict_types=1); 11 12namespace chillerlan\OAuth\Providers; 13 14use chillerlan\OAuth\Core\{ 15 AccessToken, AuthenticatedUser, CSRFToken, OAuth2Provider, TokenInvalidate, TokenInvalidateTrait, UserInfo, 16}; 17use function sprintf; 18 19/** 20 * BigCartel OAuth2 21 * 22 * @link https://developers.bigcartel.com/api/v1 23 * @link https://bigcartel.wufoo.com/confirm/big-cartel-api-application/ 24 */ 25class BigCartel extends OAuth2Provider implements CSRFToken, TokenInvalidate, UserInfo{ 26 use TokenInvalidateTrait; 27 28 public const IDENTIFIER = 'BIGCARTEL'; 29 30 public const HEADERS_API = [ 31 'Accept' => 'application/vnd.api+json', 32 ]; 33 34 protected string $authorizationURL = 'https://my.bigcartel.com/oauth/authorize'; 35 protected string $accessTokenURL = 'https://api.bigcartel.com/oauth/token'; 36 protected string $revokeURL = 'https://api.bigcartel.com/oauth/deauthorize'; 37 protected string $apiURL = 'https://api.bigcartel.com/v1'; 38 protected string|null $userRevokeURL = 'https://my.bigcartel.com/account'; 39 protected string|null $apiDocs = 'https://developers.bigcartel.com/api/v1'; 40 protected string|null $applicationURL = 'https://bigcartel.wufoo.com/forms/big-cartel-api-application/'; 41 42 /** @codeCoverageIgnore */ 43 public function me():AuthenticatedUser{ 44 $json = $this->getMeResponseData('/accounts'); 45 46 $userdata = [ 47 'data' => $json, 48 'email' => $json['data'][0]['attributes']['contact_email'], 49 'handle' => $json['data'][0]['attributes']['subdomain'], 50 'id' => $json['data'][0]['id'], 51 ]; 52 53 return new AuthenticatedUser($userdata); 54 } 55 56 public function invalidateAccessToken(AccessToken|null $token = null, string|null $type = null):bool{ 57 $tokenToInvalidate = ($token ?? $this->storage->getAccessToken($this->name)); 58 59 $request = $this->requestFactory 60 ->createRequest('POST', sprintf('%s/%s', $this->revokeURL, $this->getAccountID($tokenToInvalidate))) 61 ; 62 63 $request = $this->addBasicAuthHeader($request); 64 $response = $this->http->sendRequest($request); 65 66 if($response->getStatusCode() === 204){ 67 68 if($token === null){ 69 $this->storage->clearAccessToken($this->name); 70 } 71 72 return true; 73 } 74 75 return false; // @codeCoverageIgnore 76 } 77 78 /** 79 * Try to get the user ID from either the token or the `me()` endpoint 80 * 81 * @throws \chillerlan\OAuth\Providers\ProviderException 82 */ 83 protected function getAccountID(AccessToken $token):string{ 84 85 if(isset($token->extraParams['account_id'])){ 86 return (string)$token->extraParams['account_id']; 87 } 88 89 $me = $this->me(); 90 91 if($me->id !== null){ 92 return (string)$me->id; 93 } 94 95 throw new ProviderException('cannot determine account id'); 96 } 97 98}