-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from mukunda-/master
Add Battle.net service.
- Loading branch information
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/** --------------------------------------------------------------------------- | ||
* Example of using the Battle.net service. | ||
* | ||
* PHP version 5.4 | ||
* | ||
* @author Mukunda Johnson (mukunda.com) | ||
* @copyright Copyright (c) 2012 The authors | ||
* @license http://www.opensource.org/licenses/mit-license.html MIT License | ||
*/ | ||
|
||
use OAuth\OAuth2\Service\BattleNet; | ||
use OAuth\Common\Storage\Session; | ||
use OAuth\Common\Consumer\Credentials; | ||
use OAuth\Common\Http\Uri\Uri; | ||
|
||
/** --------------------------------------------------------------------------- | ||
* Bootstrap the example | ||
*/ | ||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
if( empty( $_GET['code'] ) && !isset($_GET['go'] )) { | ||
|
||
// Empty query; show the startup page. | ||
|
||
echo ' | ||
<p>Sign-in using Battle.net. Please pick your region:</p> | ||
<p> | ||
<a href="?go®ion=us">USA</a> | ||
<a href="?go®ion=eu">Europe</a> | ||
<a href="?go®ion=kr">Korea</a> | ||
<a href="?go®ion=tw">Taiwan</a> | ||
<a href="?go®ion=cn">China</a> | ||
</p> | ||
'; | ||
|
||
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" ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace OAuth\OAuth2\Service; | ||
|
||
//----------------------------------------------------------------------------- | ||
use OAuth\OAuth2\Token\StdOAuth2Token; | ||
use OAuth\Common\Http\Exception\TokenResponseException; | ||
use OAuth\Common\Http\Uri\Uri; | ||
use OAuth\Common\Consumer\CredentialsInterface; | ||
use OAuth\Common\Http\Client\ClientInterface; | ||
use OAuth\Common\Storage\TokenStorageInterface; | ||
use OAuth\Common\Http\Uri\UriInterface; | ||
|
||
//----------------------------------------------------------------------------- | ||
class BattleNet extends AbstractService { | ||
|
||
/** ----------------------------------------------------------------------- | ||
* Defined scopes. | ||
* | ||
* @link https://dev.battle.net/docs | ||
*/ | ||
const SCOPE_WOW_PROFILE = "wow.profile"; | ||
const SCOPE_SC2_PROFILE = "sc2.profile"; | ||
|
||
/** ----------------------------------------------------------------------- | ||
* Defined API URIs. | ||
* | ||
* @link https://dev.battle.net/docs | ||
*/ | ||
const API_URI_US = 'https://us.api.battle.net/'; | ||
const API_URI_EU = 'https://eu.api.battle.net/'; | ||
const API_URI_KR = 'https://kr.api.battle.net/'; | ||
const API_URI_TW = 'https://tw.api.battle.net/'; | ||
const API_URI_CN = 'https://api.battlenet.com.cn/'; | ||
const API_URI_SEA = 'https://sea.api.battle.net/'; | ||
|
||
public function __construct( CredentialsInterface $credentials, | ||
ClientInterface $httpClient, | ||
TokenStorageInterface $storage, | ||
$scopes = array(), | ||
UriInterface $baseApiUri = null ) { | ||
|
||
parent::__construct( $credentials, $httpClient, $storage, | ||
$scopes, $baseApiUri ); | ||
|
||
if( $baseApiUri === null ) { | ||
$this->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; | ||
} | ||
} |