diff --git a/examples/battlenet.php b/examples/battlenet.php new file mode 100644 index 00000000..f480c17a --- /dev/null +++ b/examples/battlenet.php @@ -0,0 +1,93 @@ +Sign-in using Battle.net. Please pick your region:
+ + + + '; + + die(); +} + +////////////////////////////////////////////////////////////////////////////// +// Authorization and making a request: +/////////////////////////////////////////////////////////////////////////////// + +// Session storage +$storage = new Session(); + +// Set up the credentials for the requests +$credentials = new Credentials( + $servicesCredentials['battlenet']['key'], + $servicesCredentials['battlenet']['secret'], + $currentUri->getAbsoluteUri() +); + +$region = isset($_GET['region']) ? $_GET['region'] : ""; + +$region_map = array( + 'us' => BattleNet::API_URI_US, // USA - this is the default if you omit the base API URI. + 'eu' => BattleNet::API_URI_EU, // Europe + 'kr' => BattleNet::API_URI_KR, // Korea + 'tw' => BattleNet::API_URI_TW, // Taiwan + 'cn' => BattleNet::API_URI_CN, // China +); + +// Get base API URI from region. +$apiuri = isset( $region_map[$region] ) ? new Uri( $region_map[$region] ) : null; + +// Without any scopes, we can get their BattleTag. +$scopes = array(); + +$battlenetService = $serviceFactory->createService( + 'battlenet', $credentials, $storage, $scopes, $apiuri ); + +if( !empty($_GET['code']) ) { + // This was a callback request from Battle.net, get the token + $token = $battlenetService->requestAccessToken( $_GET['code'] ); + + // See https://dev.battle.net/io-docs for OAuth request types. + // + // Without any scopes specified, we can get their BattleTag. + $result = json_decode( $battlenetService->request('/account/user') ); + + echo "Your BattleTag is \"$result->battletag\"."; + +} elseif( isset($_GET['go']) ) { + + $url = $battlenetService->getAuthorizationUri(); + header( "Location: $url" ); + +} diff --git a/examples/init.example.php b/examples/init.example.php index 9acd59fc..6ab1cdb0 100644 --- a/examples/init.example.php +++ b/examples/init.example.php @@ -19,6 +19,10 @@ 'key' => '', 'secret' => '', ), + 'battlenet' => array( + 'key' => '', + 'secret' => '', + ), 'bitbucket' => array( 'key' => '', 'secret' => '', diff --git a/src/OAuth/OAuth2/Service/BattleNet.php b/src/OAuth/OAuth2/Service/BattleNet.php new file mode 100644 index 00000000..ec3fdad8 --- /dev/null +++ b/src/OAuth/OAuth2/Service/BattleNet.php @@ -0,0 +1,116 @@ +baseApiUri = new Uri( self::API_URI_US ); + } + } + + /** ----------------------------------------------------------------------- + * Translates the current base API URI into an OAuth base URI. + * + * @returns string Base URI of oauth services. + */ + private function GetOAuthBaseUri() { + + // i love china + switch( $this->baseApiUri ) { + case self::API_URI_US: return 'https://us.battle.net/oauth/'; + case self::API_URI_EU: return 'https://eu.battle.net/oauth/'; + case self::API_URI_KR: return 'https://kr.battle.net/oauth/'; + case self::API_URI_TW: return 'https://tw.battle.net/oauth/'; + case self::API_URI_CN: return 'https://www.battlenet.com.cn/oauth/'; + case self::API_URI_SEA: return 'https://sea.battle.net/oauth/'; + } + + } + + /** ----------------------------------------------------------------------- + * {@inheritdoc} + */ + public function getAuthorizationEndpoint() { + return new Uri( $this->GetOAuthBaseUri() . 'authorize' ); + } + + /** ----------------------------------------------------------------------- + * {@inheritdoc} + */ + public function getAccessTokenEndpoint() { + return new Uri( $this->GetOAuthBaseUri() . 'token' ); + } + + /** ----------------------------------------------------------------------- + * {@inheritdoc} + */ + protected function getAuthorizationMethod() + { + return static::AUTHORIZATION_METHOD_QUERY_STRING; + } + + /** ----------------------------------------------------------------------- + * {@inheritdoc} + */ + protected function parseAccessTokenResponse( $responseBody ) + { + $data = json_decode($responseBody, true); + if( $data === null || !is_array($data) ) { + throw new TokenResponseException( 'Unable to parse response.' ); + } elseif( isset($data['error']) ) { + $err = $data['error']; + throw new TokenResponseException( + "Error in retrieving token: \"$err\"" ); + } + + $token = new StdOAuth2Token( $data['access_token'], null, + $data['expires_in'] ); + + unset( $data['access_token'] ); + unset( $data['expires_in'] ); + + $token->setExtraParams( $data ); + + return $token; + } +}