1<?php
2
3namespace OAuth\OAuth2\Service;
4
5//-----------------------------------------------------------------------------
6use OAuth\OAuth2\Token\StdOAuth2Token;
7use OAuth\Common\Http\Exception\TokenResponseException;
8use OAuth\Common\Http\Uri\Uri;
9use OAuth\Common\Consumer\CredentialsInterface;
10use OAuth\Common\Http\Client\ClientInterface;
11use OAuth\Common\Storage\TokenStorageInterface;
12use OAuth\Common\Http\Uri\UriInterface;
13
14//-----------------------------------------------------------------------------
15class BattleNet extends AbstractService {
16
17    /** -----------------------------------------------------------------------
18     * Defined scopes.
19     *
20     * @link 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     * @link 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 = array(),
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        // i love china
59        switch( $this->baseApiUri ) {
60            case self::API_URI_US:  return 'https://us.battle.net/oauth/';
61            case self::API_URI_EU:  return 'https://eu.battle.net/oauth/';
62            case self::API_URI_KR:  return 'https://kr.battle.net/oauth/';
63            case self::API_URI_TW:  return 'https://tw.battle.net/oauth/';
64            case self::API_URI_CN:  return 'https://www.battlenet.com.cn/oauth/';
65            case self::API_URI_SEA: return 'https://sea.battle.net/oauth/';
66        }
67
68    }
69
70    /** -----------------------------------------------------------------------
71     * {@inheritdoc}
72     */
73    public function getAuthorizationEndpoint() {
74        return new Uri( $this->GetOAuthBaseUri() . 'authorize' );
75    }
76
77    /** -----------------------------------------------------------------------
78     * {@inheritdoc}
79     */
80    public function getAccessTokenEndpoint() {
81        return new Uri( $this->GetOAuthBaseUri() . 'token' );
82    }
83
84    /** -----------------------------------------------------------------------
85     * {@inheritdoc}
86     */
87    protected function getAuthorizationMethod()
88    {
89        return static::AUTHORIZATION_METHOD_QUERY_STRING;
90    }
91
92    /** -----------------------------------------------------------------------
93     * {@inheritdoc}
94     */
95    protected function parseAccessTokenResponse( $responseBody )
96    {
97        $data = json_decode($responseBody, true);
98        if( $data === null || !is_array($data) ) {
99            throw new TokenResponseException( 'Unable to parse response.' );
100        } elseif( isset($data['error']) ) {
101            $err = $data['error'];
102            throw new TokenResponseException(
103                                "Error in retrieving token: \"$err\"" );
104        }
105
106        $token = new StdOAuth2Token( $data['access_token'], null,
107                                     $data['expires_in'] );
108
109        unset( $data['access_token'] );
110        unset( $data['expires_in'] );
111
112        $token->setExtraParams( $data );
113
114        return $token;
115    }
116}
117