Skip to content

Commit

Permalink
Merge pull request #473 from mukunda-/master
Browse files Browse the repository at this point in the history
Add Battle.net service.
  • Loading branch information
David Desberg authored Jul 12, 2016
2 parents c69bbe3 + f45f586 commit 09f4af3
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
93 changes: 93 additions & 0 deletions examples/battlenet.php
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&region=us">USA</a>
<a href="?go&region=eu">Europe</a>
<a href="?go&region=kr">Korea</a>
<a href="?go&region=tw">Taiwan</a>
<a href="?go&region=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" );

}
4 changes: 4 additions & 0 deletions examples/init.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
'key' => '',
'secret' => '',
),
'battlenet' => array(
'key' => '',
'secret' => '',
),
'bitbucket' => array(
'key' => '',
'secret' => '',
Expand Down
116 changes: 116 additions & 0 deletions src/OAuth/OAuth2/Service/BattleNet.php
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;
}
}

0 comments on commit 09f4af3

Please sign in to comment.