1+ <?php
2+ namespace OAuth \OAuth2 \Service ;
3+
4+ use OAuth \OAuth2 \Token \StdOAuth2Token ;
5+ use OAuth \Common \Http \Exception \TokenResponseException ;
6+ use OAuth \Common \Http \Uri \Uri ;
7+ use OAuth \Common \Consumer \Credentials ;
8+ use OAuth \Common \Http \Client \ClientInterface ;
9+ use OAuth \Common \Storage \TokenStorageInterface ;
10+ use OAuth \Common \Http \Uri \UriInterface ;
11+
12+ class BattleNetBase extends AbstractService
13+ {
14+ /**
15+ * Scopes
16+ *
17+ * @var string
18+ */
19+
20+ const SCOPE_WOW_PROFILE = 'wow.profile ' ;
21+ const SCOPE_SC2_PROFILE = 'sc2.profile ' ;
22+
23+ /**
24+ * BattleNet Region
25+ *
26+ * @var string
27+ */
28+ protected $ region ;
29+
30+ public function __construct (Credentials $ credentials , ClientInterface $ httpClient , TokenStorageInterface $ storage , $ scopes = array (), UriInterface $ baseApiUri = null )
31+ {
32+ parent ::__construct ($ credentials , $ httpClient , $ storage , $ scopes , $ baseApiUri );
33+ if ( null === $ baseApiUri ) {
34+ $ this ->baseApiUri = new Uri ('https:// ' . $ this ->region . '.api.battle.net/ ' );
35+ }
36+ }
37+
38+ /**
39+ * @return \OAuth\Common\Http\Uri\UriInterface
40+ */
41+ public function getAuthorizationEndpoint ()
42+ {
43+ return new Uri ('https:// ' . $ this ->region . '.battle.net/oauth/authorize ' );
44+ }
45+
46+ /**
47+ * @return \OAuth\Common\Http\Uri\UriInterface
48+ */
49+ public function getAccessTokenEndpoint ()
50+ {
51+ return new Uri ('https:// ' . $ this ->region . '.battle.net/oauth/token ' );
52+ }
53+
54+ /**
55+ * @param string $responseBody
56+ * @return \OAuth\Common\Token\TokenInterface|\OAuth\OAuth2\Token\StdOAuth2Token
57+ * @throws \OAuth\Common\Http\Exception\TokenResponseException
58+ */
59+ protected function parseAccessTokenResponse ($ responseBody )
60+ {
61+ $ data = json_decode ( $ responseBody , true );
62+
63+ if ( null === $ data || !is_array ($ data ) ) {
64+ throw new TokenResponseException ('Unable to parse response. ' );
65+ } elseif ( isset ($ data ['error ' ] ) ) {
66+ throw new TokenResponseException ('Error in retrieving token: " ' . $ data ['error ' ] . '" ' );
67+ }
68+
69+ $ token = new StdOAuth2Token ();
70+
71+ $ token ->setAccessToken ( $ data ['access_token ' ] );
72+ // I'm invincible!!!
73+ $ token ->setEndOfLife (StdOAuth2Token::EOL_NEVER_EXPIRES );
74+ unset( $ data ['access_token ' ] );
75+ $ token ->setExtraParams ( $ data );
76+
77+ return $ token ;
78+ }
79+
80+ /**
81+ * @return int
82+ */
83+ protected function getAuthorizationMethod ()
84+ {
85+ return static ::AUTHORIZATION_METHOD_QUERY_STRING ;
86+ }
87+ }
0 commit comments