|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Reddit |
| 4 | + * |
| 5 | + * @created 09.04.2024 |
| 6 | + * @author smiley <smiley@chillerlan.net> |
| 7 | + * @copyright 2024 smiley |
| 8 | + * @license MIT |
| 9 | + * |
| 10 | + * @noinspection PhpUnused |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace chillerlan\OAuth\Providers; |
| 15 | + |
| 16 | +use chillerlan\HTTP\Utils\QueryUtil; |
| 17 | +use chillerlan\OAuth\Core\{ |
| 18 | + AccessToken, AuthenticatedUser, ClientCredentials, CSRFToken, OAuth2Interface, |
| 19 | + OAuth2Provider, TokenInvalidate, TokenRefresh, UserInfo |
| 20 | +}; |
| 21 | +use Psr\Http\Message\ResponseInterface; |
| 22 | +use function sodium_bin2base64, sprintf; |
| 23 | +use const PHP_QUERY_RFC1738, SODIUM_BASE64_VARIANT_ORIGINAL; |
| 24 | + |
| 25 | +/** |
| 26 | + * @see https://github.com/reddit-archive/reddit/wiki/OAuth2 |
| 27 | + * @see https://github.com/reddit-archive/reddit/wiki/API |
| 28 | + * @see https://support.reddithelp.com/hc/en-us/articles/16160319875092-Reddit-Data-API-Wiki |
| 29 | + * @see https://www.reddit.com/dev/api |
| 30 | + */ |
| 31 | +class Reddit extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenRefresh, TokenInvalidate, UserInfo{ |
| 32 | + |
| 33 | + public const SCOPE_ACCOUNT = 'account'; |
| 34 | + public const SCOPE_CREDDITS = 'creddits'; |
| 35 | + public const SCOPE_EDIT = 'edit'; |
| 36 | + public const SCOPE_FLAIR = 'flair'; |
| 37 | + public const SCOPE_HISTORY = 'history'; |
| 38 | + public const SCOPE_IDENTITY = 'identity'; |
| 39 | + public const SCOPE_LIVEMANAGE = 'livemanage'; |
| 40 | + public const SCOPE_MODCONFIG = 'modconfig'; |
| 41 | + public const SCOPE_MODCONTRIBUTORS = 'modcontributors'; |
| 42 | + public const SCOPE_MODFLAIR = 'modflair'; |
| 43 | + public const SCOPE_MODLOG = 'modlog'; |
| 44 | + public const SCOPE_MODMAIL = 'modmail'; |
| 45 | + public const SCOPE_MODNOTE = 'modnote'; |
| 46 | + public const SCOPE_MODOTHERS = 'modothers'; |
| 47 | + public const SCOPE_MODPOSTS = 'modposts'; |
| 48 | + public const SCOPE_MODSELF = 'modself'; |
| 49 | + public const SCOPE_MODTRAFFIC = 'modtraffic'; |
| 50 | + public const SCOPE_MODWIKI = 'modwiki'; |
| 51 | + public const SCOPE_MYSUBREDDITS = 'mysubreddits'; |
| 52 | + public const SCOPE_PRIVATEMESSAGES = 'privatemessages'; |
| 53 | + public const SCOPE_READ = 'read'; |
| 54 | + public const SCOPE_REPORT = 'report'; |
| 55 | + public const SCOPE_SAVE = 'save'; |
| 56 | + public const SCOPE_STRUCTUREDSTYLES = 'structuredstyles'; |
| 57 | + public const SCOPE_SUBMIT = 'submit'; |
| 58 | + public const SCOPE_SUBSCRIBE = 'subscribe'; |
| 59 | + public const SCOPE_VOTE = 'vote'; |
| 60 | + public const SCOPE_WIKIEDIT = 'wikiedit'; |
| 61 | + public const SCOPE_WIKIREAD = 'wikiread'; |
| 62 | + |
| 63 | + public const DEFAULT_SCOPES = [ |
| 64 | + self::SCOPE_ACCOUNT, |
| 65 | + self::SCOPE_IDENTITY, |
| 66 | + self::SCOPE_READ, |
| 67 | + ]; |
| 68 | + |
| 69 | + public const USER_AGENT = OAuth2Interface::USER_AGENT.' (by /u/chillerlan)'; |
| 70 | + |
| 71 | + public const HEADERS_AUTH = [ |
| 72 | + 'User-Agent' => self::USER_AGENT, |
| 73 | + ]; |
| 74 | + |
| 75 | + public const HEADERS_API = [ |
| 76 | + 'User-Agent' => self::USER_AGENT, |
| 77 | + ]; |
| 78 | + |
| 79 | + protected string $authorizationURL = 'https://www.reddit.com/api/v1/authorize'; |
| 80 | + protected string $accessTokenURL = 'https://www.reddit.com/api/v1/access_token'; |
| 81 | + protected string $apiURL = 'https://oauth.reddit.com/api'; |
| 82 | + protected string $revokeURL = 'https://www.reddit.com/api/v1/revoke_token'; |
| 83 | + protected string|null $apiDocs = 'https://www.reddit.com/dev/api'; |
| 84 | + protected string|null $applicationURL = 'https://www.reddit.com/prefs/apps/'; |
| 85 | + protected string|null $userRevokeURL = 'https://www.reddit.com/settings/privacy'; |
| 86 | + |
| 87 | + /** |
| 88 | + * @inheritDoc |
| 89 | + */ |
| 90 | + protected function getAccessTokenRequestBodyParams(string $code):array{ |
| 91 | + return [ |
| 92 | + 'code' => $code, |
| 93 | + 'grant_type' => 'authorization_code', |
| 94 | + 'redirect_uri' => $this->options->callbackURL, |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @inheritDoc |
| 100 | + */ |
| 101 | + protected function sendAccessTokenRequest(string $url, array $body):ResponseInterface{ |
| 102 | + $auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL); |
| 103 | + |
| 104 | + $request = $this->requestFactory |
| 105 | + ->createRequest('POST', $url) |
| 106 | + ->withHeader('Accept-Encoding', 'identity') |
| 107 | + ->withHeader('Authorization', sprintf('Basic %s', $auth)) |
| 108 | + ->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
| 109 | + ->withBody($this->streamFactory->createStream(QueryUtil::build($body, PHP_QUERY_RFC1738))) |
| 110 | + ; |
| 111 | + |
| 112 | + return $this->http->sendRequest($request); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @inheritDoc |
| 117 | + * @codeCoverageIgnore |
| 118 | + */ |
| 119 | + public function me():AuthenticatedUser{ |
| 120 | + $json = $this->getMeResponseData('/v1/me'); |
| 121 | + |
| 122 | + $userdata = [ |
| 123 | + 'data' => $json, |
| 124 | + 'avatar' => $json['subreddit']['icon_img'], |
| 125 | + 'handle' => $json['name'], |
| 126 | + 'displayName' => $json['subreddit']['title'], |
| 127 | + 'id' => $json['id'], |
| 128 | + 'url' => sprintf('https://www.reddit.com%s', $json['subreddit']['url']), |
| 129 | + ]; |
| 130 | + |
| 131 | + return new AuthenticatedUser($userdata); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @see https://github.com/reddit-archive/reddit/wiki/OAuth2#manually-revoking-a-token |
| 136 | + * @inheritDoc |
| 137 | + */ |
| 138 | + public function invalidateAccessToken(AccessToken $token = null):bool{ |
| 139 | + $tokenToInvalidate = ($token ?? $this->storage->getAccessToken($this->name)); |
| 140 | + |
| 141 | + $bodyParams = [ |
| 142 | + 'token' => $tokenToInvalidate->accessToken, |
| 143 | + 'token_type_hint' => 'access_token', |
| 144 | + ]; |
| 145 | + |
| 146 | + $auth = sodium_bin2base64(sprintf('%s:%s', $this->options->key, $this->options->secret), SODIUM_BASE64_VARIANT_ORIGINAL); |
| 147 | + |
| 148 | + $request = $this->requestFactory |
| 149 | + ->createRequest('POST', $this->revokeURL) |
| 150 | + ->withHeader('Authorization', sprintf('Basic %s', $auth)) |
| 151 | + ->withHeader('Content-Type', 'application/x-www-form-urlencoded') |
| 152 | + ; |
| 153 | + |
| 154 | + $request = $this->setRequestBody($bodyParams, $request); |
| 155 | + $response = $this->http->sendRequest($request); |
| 156 | + |
| 157 | + if($response->getStatusCode() === 204){ |
| 158 | + |
| 159 | + if($token === null){ |
| 160 | + $this->storage->clearAccessToken($this->name); |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments