friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class BattleNet
4 *
5 * @created 02.08.2019
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2019 smiley
8 * @license MIT
9 */
10declare(strict_types=1);
11
12namespace chillerlan\OAuth\Providers;
13
14use chillerlan\OAuth\Core\{AuthenticatedUser, ClientCredentials, ClientCredentialsTrait, CSRFToken, OAuth2Provider, UserInfo};
15use function in_array, ltrim, rtrim, sprintf, strtolower;
16
17/**
18 * Battle.net OAuth2
19 *
20 * @link https://develop.battle.net/documentation/guides/using-oauth
21 */
22class BattleNet extends OAuth2Provider implements ClientCredentials, CSRFToken, UserInfo{
23 use ClientCredentialsTrait;
24
25 public const IDENTIFIER = 'BATTLENET';
26
27 public const SCOPE_OPENID = 'openid';
28 public const SCOPE_PROFILE_D3 = 'd3.profile';
29 public const SCOPE_PROFILE_SC2 = 'sc2.profile';
30 public const SCOPE_PROFILE_WOW = 'wow.profile';
31
32 public const DEFAULT_SCOPES = [
33 self::SCOPE_OPENID,
34 self::SCOPE_PROFILE_D3,
35 self::SCOPE_PROFILE_SC2,
36 self::SCOPE_PROFILE_WOW,
37 ];
38
39 protected string|null $apiDocs = 'https://develop.battle.net/documentation';
40 protected string|null $applicationURL = 'https://develop.battle.net/access/clients';
41 protected string|null $userRevokeURL = 'https://account.blizzard.com/connections';
42
43 // the URL for the "OAuth" endpoints
44 // @link https://develop.battle.net/documentation/battle-net/oauth-apis
45 protected string $battleNetOauth = 'https://oauth.battle.net';
46 protected string $region = 'eu';
47 // these URLs will be set dynamically, depending on the chose datacenter
48 protected string $apiURL = 'https://eu.api.blizzard.com';
49 protected string $authorizationURL = 'https://oauth.battle.net/authorize';
50 protected string $accessTokenURL = 'https://oauth.battle.net/token';
51
52 protected const KNOWN_DOMAINS = [
53 'oauth.battle.net',
54 'eu.api.blizzard.com',
55 'kr.api.blizzard.com',
56 'tw.api.blizzard.com',
57 'us.api.blizzard.com',
58 'gateway.battlenet.com.cn',
59 'oauth.battlenet.com.cn',
60 ];
61
62 protected function getRequestTarget(string $uri):string{
63 $parsedURL = $this->uriFactory->createUri($uri);
64 $parsedHost = $parsedURL->getHost();
65 $api = $this->uriFactory->createUri($this->apiURL);
66
67 if($parsedHost === ''){
68 $parsedPath = $parsedURL->getPath();
69 $apiURL = rtrim((string)$api, '/');
70
71 if($parsedPath === ''){
72 return $apiURL;
73 }
74
75 return sprintf('%s/%s', $apiURL, ltrim($parsedPath, '/'));
76 }
77
78 // for some reason we were given a host name
79
80 // shortcut here for the known domains
81 if(in_array($parsedHost, $this::KNOWN_DOMAINS, true)){
82 // we explicitly ignore any existing parameters here
83 return (string)$parsedURL->withScheme('https')->withQuery('')->withFragment('');
84 }
85
86 // back out if it doesn't match
87 throw new ProviderException(sprintf('given host (%s) does not match provider (%s)', $parsedHost, $api->getHost()));
88 }
89
90 /**
91 * Set the datacenter URLs for the given region
92 *
93 * @throws \chillerlan\OAuth\Providers\ProviderException
94 */
95 public function setRegion(string $region):static{
96 $region = strtolower($region);
97
98 if(!in_array($region, ['cn', 'eu', 'kr', 'tw', 'us'], true)){
99 throw new ProviderException('invalid region: '.$region);
100 }
101
102 $this->region = $region;
103 $this->apiURL = sprintf('https://%s.api.blizzard.com', $this->region);
104 $this->battleNetOauth = 'https://oauth.battle.net';
105
106 if($region === 'cn'){
107 $this->apiURL = 'https://gateway.battlenet.com.cn';
108 $this->battleNetOauth = 'https://oauth.battlenet.com.cn';
109 }
110
111 $this->authorizationURL = $this->battleNetOauth.'/authorize';
112 $this->accessTokenURL = $this->battleNetOauth.'/token';
113
114 return $this;
115 }
116
117 /** @codeCoverageIgnore */
118 public function me():AuthenticatedUser{
119 $json = $this->getMeResponseData($this->battleNetOauth.'/oauth/userinfo');
120
121 $userdata = [
122 'data' => $json,
123 'handle' => $json['battletag'],
124 'id' => $json['id'],
125 ];
126
127 return new AuthenticatedUser($userdata);
128 }
129
130}