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