1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
//----------------------------------------------------------------------------- |
6
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
7
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
8
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
9
|
|
|
use OAuth\Common\Http\Uri\Uri; |
10
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
11
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
12
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
13
|
|
|
|
14
|
|
|
//----------------------------------------------------------------------------- |
15
|
|
|
class BattleNet extends AbstractService |
16
|
|
|
{ |
17
|
|
|
/** ----------------------------------------------------------------------- |
18
|
|
|
* Defined scopes. |
19
|
|
|
* |
20
|
|
|
* @see https://dev.battle.net/docs |
21
|
|
|
*/ |
22
|
|
|
const SCOPE_WOW_PROFILE = 'wow.profile'; |
23
|
|
|
const SCOPE_SC2_PROFILE = 'sc2.profile'; |
24
|
|
|
|
25
|
|
|
/** ----------------------------------------------------------------------- |
26
|
|
|
* Defined API URIs. |
27
|
|
|
* |
28
|
|
|
* @see https://dev.battle.net/docs |
29
|
|
|
*/ |
30
|
|
|
const API_URI_US = 'https://us.api.battle.net/'; |
31
|
|
|
const API_URI_EU = 'https://eu.api.battle.net/'; |
32
|
|
|
const API_URI_KR = 'https://kr.api.battle.net/'; |
33
|
|
|
const API_URI_TW = 'https://tw.api.battle.net/'; |
34
|
|
|
const API_URI_CN = 'https://api.battlenet.com.cn/'; |
35
|
|
|
const API_URI_SEA = 'https://sea.api.battle.net/'; |
36
|
|
|
|
37
|
|
|
public function __construct(CredentialsInterface $credentials, |
38
|
|
|
ClientInterface $httpClient, |
39
|
|
|
TokenStorageInterface $storage, |
40
|
|
|
$scopes = [], |
41
|
|
|
?UriInterface $baseApiUri = null) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($credentials, $httpClient, $storage, |
44
|
|
|
$scopes, $baseApiUri); |
45
|
|
|
|
46
|
|
|
if ($baseApiUri === null) { |
47
|
|
|
$this->baseApiUri = new Uri(self::API_URI_US); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** ----------------------------------------------------------------------- |
52
|
|
|
* Translates the current base API URI into an OAuth base URI. |
53
|
|
|
* |
54
|
|
|
* @returns string Base URI of oauth services. |
55
|
|
|
*/ |
56
|
|
|
private function GetOAuthBaseUri() |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
// i love china |
60
|
|
|
switch ($this->baseApiUri) { |
61
|
|
|
case self::API_URI_US: return 'https://us.battle.net/oauth/'; |
62
|
|
|
case self::API_URI_EU: return 'https://eu.battle.net/oauth/'; |
63
|
|
|
case self::API_URI_KR: return 'https://kr.battle.net/oauth/'; |
64
|
|
|
case self::API_URI_TW: return 'https://tw.battle.net/oauth/'; |
65
|
|
|
case self::API_URI_CN: return 'https://www.battlenet.com.cn/oauth/'; |
66
|
|
|
case self::API_URI_SEA: return 'https://sea.battle.net/oauth/'; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** ----------------------------------------------------------------------- |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getAuthorizationEndpoint() |
74
|
|
|
{ |
75
|
|
|
return new Uri($this->GetOAuthBaseUri() . 'authorize'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** ----------------------------------------------------------------------- |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getAccessTokenEndpoint() |
82
|
|
|
{ |
83
|
|
|
return new Uri($this->GetOAuthBaseUri() . 'token'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** ----------------------------------------------------------------------- |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
protected function getAuthorizationMethod() |
90
|
|
|
{ |
91
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** ----------------------------------------------------------------------- |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
protected function parseAccessTokenResponse($responseBody) |
98
|
|
|
{ |
99
|
|
|
$data = json_decode($responseBody, true); |
100
|
|
|
if ($data === null || !is_array($data)) { |
101
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
102
|
|
|
} elseif (isset($data['error'])) { |
103
|
|
|
$err = $data['error']; |
104
|
|
|
|
105
|
|
|
throw new TokenResponseException( |
106
|
|
|
"Error in retrieving token: \"$err\""); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$token = new StdOAuth2Token($data['access_token'], null, |
110
|
|
|
$data['expires_in']); |
111
|
|
|
|
112
|
|
|
unset($data['access_token'] , $data['expires_in']); |
113
|
|
|
|
114
|
|
|
$token->setExtraParams($data); |
115
|
|
|
|
116
|
|
|
return $token; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|