|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class TikTok |
| 4 | + * |
| 5 | + * @created 11.04.2024 |
| 6 | + * @author smiley <[email protected]> |
| 7 | + * @copyright 2024 smiley |
| 8 | + * @license MIT |
| 9 | + * |
| 10 | + * @noinspection PhpUnused |
| 11 | + */ |
| 12 | + |
| 13 | +namespace chillerlan\OAuth\Providers; |
| 14 | + |
| 15 | +use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, PKCE, TokenRefresh}; |
| 16 | +use function array_merge, implode; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see https://developers.tiktok.com/doc/login-kit-web/ |
| 20 | + * @see https://developers.tiktok.com/doc/oauth-user-access-token-management/ |
| 21 | + */ |
| 22 | +class TikTok extends OAuth2Provider implements CSRFToken, PKCE, TokenRefresh{ |
| 23 | + |
| 24 | + public const IDENTIFIER = 'TIKTOK'; |
| 25 | + |
| 26 | + public const SCOPE_VIDEO_UPLOAD = 'video.upload'; |
| 27 | + public const SCOPE_VIDEO_LIST = 'video.list'; |
| 28 | + public const SCOPE_VIDEO_PUBLISH = 'video.publish'; |
| 29 | + public const SCOPE_USER_INFO_BASIC = 'user.info.basic'; |
| 30 | + public const SCOPE_USER_INFO_PROFILE = 'user.info.profile'; |
| 31 | + public const SCOPE_USER_INFO_STATS = 'user.info.stats'; |
| 32 | + public const SCOPE_PORTABILITY_PPOSTPROFILE_ONGOING = 'portability.postsandprofile.ongoing'; |
| 33 | + public const SCOPE_PORTABILITY_PPOSTPROFILE_SINGLE = 'portability.postsandprofile.single'; |
| 34 | + public const SCOPE_PORTABILITY_ALL_ONGOING = 'portability.all.ongoing'; |
| 35 | + public const SCOPE_PORTABILITY_ALL_SINGLE = 'portability.all.single'; |
| 36 | + public const SCOPE_PORTABILITY_DIRECTMESSAGES_ONGOING = 'portability.directmessages.ongoing'; |
| 37 | + public const SCOPE_PORTABILITY_DIRECTMESSAGES_SINGLE = 'portability.directmessages.single'; |
| 38 | + public const SCOPE_PORTABILITY_ACTIVITY_ONGOING = 'portability.activity.ongoing'; |
| 39 | + public const SCOPE_PORTABILITY_ACTIVITY_SINGLE = 'portability.activity.single'; |
| 40 | + |
| 41 | + public const DEFAULT_SCOPES = [ |
| 42 | + self::SCOPE_USER_INFO_BASIC, |
| 43 | + ]; |
| 44 | + |
| 45 | + protected string $authorizationURL = 'https://www.tiktok.com/v2/auth/authorize/'; |
| 46 | + protected string $accessTokenURL = 'https://open.tiktokapis.com/v2/oauth/token/'; |
| 47 | + protected string $revokeURL = 'https://open.tiktokapis.com/v2/oauth/revoke/'; |
| 48 | + protected string $apiURL = 'https://open.tiktokapis.com'; |
| 49 | + protected string|null $apiDocs = 'https://developers.tiktok.com/doc/overview/'; |
| 50 | + protected string|null $applicationURL = 'https://developers.tiktok.com/apps/'; |
| 51 | + protected string|null $userRevokeURL = 'https://example.com/user/settings/connections'; |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritDoc |
| 55 | + */ |
| 56 | + protected function getAuthorizationURLRequestParams(array $params, array $scopes):array{ |
| 57 | + |
| 58 | + unset($params['client_secret']); |
| 59 | + |
| 60 | + $params = array_merge($params, [ |
| 61 | + 'client_key' => $this->options->key, |
| 62 | + 'redirect_uri' => $this->options->callbackURL, |
| 63 | + 'response_type' => 'code', |
| 64 | + ]); |
| 65 | + |
| 66 | + if(!empty($scopes)){ |
| 67 | + $params['scope'] = implode($this::SCOPES_DELIMITER, $scopes); |
| 68 | + } |
| 69 | + |
| 70 | + $params = $this->setCodeChallenge($params, PKCE::CHALLENGE_METHOD_S256); |
| 71 | + |
| 72 | + return $this->setState($params); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritDoc |
| 77 | + */ |
| 78 | + protected function getAccessTokenRequestBodyParams(string $code):array{ |
| 79 | + |
| 80 | + $params = [ |
| 81 | + 'client_key' => $this->options->key, |
| 82 | + 'client_secret' => $this->options->secret, |
| 83 | + 'code' => $code, |
| 84 | + 'grant_type' => 'authorization_code', |
| 85 | + 'redirect_uri' => $this->options->callbackURL, |
| 86 | + ]; |
| 87 | + |
| 88 | + return $this->setCodeVerifier($params); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @inheritDoc |
| 93 | + */ |
| 94 | + protected function getRefreshAccessTokenRequestBodyParams(string $refreshToken):array{ |
| 95 | + return [ |
| 96 | + 'client_key' => $this->options->key, |
| 97 | + 'client_secret' => $this->options->secret, |
| 98 | + 'grant_type' => 'refresh_token', |
| 99 | + 'refresh_token' => $refreshToken, |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments