Passed
Branch main (d68b9c)
by smiley
09:54
created

Twitch   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 45
c 3
b 0
f 0
dl 0
loc 68
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getClientCredentialsToken() 0 26 3
1
<?php
2
/**
3
 * Class Twitch
4
 *
5
 * @link https://dev.twitch.tv/docs/api/reference/
6
 * @link https://dev.twitch.tv/docs/authentication/
7
 *
8
 * @filesource   Twitch.php
9
 * @created      22.10.2017
10
 * @package      chillerlan\OAuth\Providers\Twitch
11
 * @author       Smiley <[email protected]>
12
 * @copyright    2017 Smiley
13
 * @license      MIT
14
 */
15
16
namespace chillerlan\OAuth\Providers\Twitch;
17
18
use chillerlan\OAuth\Core\{AccessToken, ClientCredentials, CSRFToken, OAuth2Provider, TokenRefresh};
19
20
use function http_build_query, implode;
21
22
use const PHP_QUERY_RFC1738;
23
24
/**
25
 * @method \Psr\Http\Message\ResponseInterface me()
26
 * @method \Psr\Http\Message\ResponseInterface user(string $username)
27
 */
28
class Twitch extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenRefresh{
29
30
	public const SCOPE_CHANNEL_CHECK_SUBSCRIPTION = 'channel_check_subscription';
31
	public const SCOPE_CHANNEL_COMMERCIAL         = 'channel_commercial';
32
	public const SCOPE_CHANNEL_EDITOR             = 'channel_editor';
33
	public const SCOPE_CHANNEL_FEED_EDIT          = 'channel_feed_edit';
34
	public const SCOPE_CHANNEL_FEED_READ          = 'channel_feed_read';
35
	public const SCOPE_CHANNEL_CHANNEL_READ       = 'channel_read';
36
	public const SCOPE_CHANNEL_CHANNEL_STREAM     = 'channel_stream';
37
	public const SCOPE_CHANNEL_SUBSCRIPTIONS      = 'channel_subscriptions';
38
	public const SCOPE_CHAT_LOGIN                 = 'chat_login';
39
	public const SCOPE_COLLECTIONS_EDIT           = 'collections_edit';
40
	public const SCOPE_COMMUNITIES_EDIT           = 'communities_edit';
41
	public const SCOPE_COMMUNITIES_MODERATE       = 'communities_moderate';
42
	public const SCOPE_OPENID                     = 'openid';
43
	public const SCOPE_USER_BLOCKS_EDIT           = 'user_blocks_edit';
44
	public const SCOPE_USER_BLOCKS_READ           = 'user_blocks_read';
45
	public const SCOPE_USER_FOLLOWS_EDIT          = 'user_follows_edit';
46
	public const SCOPE_USER_READ                  = 'user_read';
47
	public const SCOPE_USER_SUBSCRIPTIONS         = 'user_subscriptions';
48
	public const SCOPE_VIEWING_ACTIVITY_READ      = 'viewing_activity_read';
49
50
	protected string $authURL          = 'https://api.twitch.tv/kraken/oauth2/authorize';
51
	protected string $accessTokenURL   = 'https://api.twitch.tv/kraken/oauth2/token';
52
	protected ?string $apiURL          = 'https://api.twitch.tv/kraken';
53
	protected ?string $userRevokeURL   = 'https://www.twitch.tv/settings/connections';
54
	protected ?string $revokeURL       = 'https://api.twitch.tv/kraken/oauth2/revoke';
55
	protected ?string $endpointMap     = TwitchEndpoints::class;
56
	protected ?string $apiDocs         = 'https://dev.twitch.tv/docs/api/reference/';
57
	protected ?string $applicationURL  = 'https://dev.twitch.tv/console/apps/create';
58
	protected string $authMethodHeader = 'OAuth';  // -> https://api.twitch.tv/kraken
59
#	protected string $authMethodHeader = 'Bearer'; // -> https://api.twitch.tv/helix
60
	protected array $authHeaders       = ['Accept' => 'application/vnd.twitchtv.v5+json'];
61
	protected array $apiHeaders        = ['Accept' => 'application/vnd.twitchtv.v5+json'];
62
63
	/**
64
	 * @link https://dev.twitch.tv/docs/authentication#oauth-client-credentials-flow-app-access-tokens
65
	 *
66
	 * @param array|null $scopes
67
	 *
68
	 * @return \chillerlan\OAuth\Core\AccessToken
69
	 */
70
	public function getClientCredentialsToken(array $scopes = null):AccessToken{
71
		$params = [
72
			'client_id'     => $this->options->key,
73
			'client_secret' => $this->options->secret,
74
			'grant_type'    => 'client_credentials',
75
		];
76
77
		if($scopes !== null){
78
			$params['scope'] = implode($this->scopesDelimiter, $scopes);
79
		}
80
81
		$request = $this->requestFactory
82
			->createRequest('POST', $this->clientCredentialsTokenURL ?? $this->accessTokenURL)
83
			->withHeader('Content-Type', 'application/x-www-form-urlencoded')
84
			->withBody($this->streamFactory->createStream(http_build_query($params, '', '&', PHP_QUERY_RFC1738)))
85
		;
86
87
		foreach($this->authHeaders as $header => $value){
88
			$request = $request->withAddedHeader($header, $value);
89
		}
90
91
		$token = $this->parseTokenResponse($this->http->sendRequest($request));
92
93
		$this->storage->storeAccessToken($this->serviceName, $token);
94
95
		return $token;
96
	}
97
98
}
99