Skip to content

Commit 09f4af3

Browse files
author
David Desberg
authored
Merge pull request #473 from mukunda-/master
Add Battle.net service.
2 parents c69bbe3 + f45f586 commit 09f4af3

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed

examples/battlenet.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/** ---------------------------------------------------------------------------
4+
* Example of using the Battle.net service.
5+
*
6+
* PHP version 5.4
7+
*
8+
* @author Mukunda Johnson (mukunda.com)
9+
* @copyright Copyright (c) 2012 The authors
10+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
11+
*/
12+
13+
use OAuth\OAuth2\Service\BattleNet;
14+
use OAuth\Common\Storage\Session;
15+
use OAuth\Common\Consumer\Credentials;
16+
use OAuth\Common\Http\Uri\Uri;
17+
18+
/** ---------------------------------------------------------------------------
19+
* Bootstrap the example
20+
*/
21+
require_once __DIR__ . '/bootstrap.php';
22+
23+
if( empty( $_GET['code'] ) && !isset($_GET['go'] )) {
24+
25+
// Empty query; show the startup page.
26+
27+
echo '
28+
29+
<p>Sign-in using Battle.net. Please pick your region:</p>
30+
31+
<p>
32+
<a href="?go&region=us">USA</a>
33+
<a href="?go&region=eu">Europe</a>
34+
<a href="?go&region=kr">Korea</a>
35+
<a href="?go&region=tw">Taiwan</a>
36+
<a href="?go&region=cn">China</a>
37+
</p>
38+
39+
';
40+
41+
die();
42+
}
43+
44+
//////////////////////////////////////////////////////////////////////////////
45+
// Authorization and making a request:
46+
///////////////////////////////////////////////////////////////////////////////
47+
48+
// Session storage
49+
$storage = new Session();
50+
51+
// Set up the credentials for the requests
52+
$credentials = new Credentials(
53+
$servicesCredentials['battlenet']['key'],
54+
$servicesCredentials['battlenet']['secret'],
55+
$currentUri->getAbsoluteUri()
56+
);
57+
58+
$region = isset($_GET['region']) ? $_GET['region'] : "";
59+
60+
$region_map = array(
61+
'us' => BattleNet::API_URI_US, // USA - this is the default if you omit the base API URI.
62+
'eu' => BattleNet::API_URI_EU, // Europe
63+
'kr' => BattleNet::API_URI_KR, // Korea
64+
'tw' => BattleNet::API_URI_TW, // Taiwan
65+
'cn' => BattleNet::API_URI_CN, // China
66+
);
67+
68+
// Get base API URI from region.
69+
$apiuri = isset( $region_map[$region] ) ? new Uri( $region_map[$region] ) : null;
70+
71+
// Without any scopes, we can get their BattleTag.
72+
$scopes = array();
73+
74+
$battlenetService = $serviceFactory->createService(
75+
'battlenet', $credentials, $storage, $scopes, $apiuri );
76+
77+
if( !empty($_GET['code']) ) {
78+
// This was a callback request from Battle.net, get the token
79+
$token = $battlenetService->requestAccessToken( $_GET['code'] );
80+
81+
// See https://dev.battle.net/io-docs for OAuth request types.
82+
//
83+
// Without any scopes specified, we can get their BattleTag.
84+
$result = json_decode( $battlenetService->request('/account/user') );
85+
86+
echo "Your BattleTag is \"$result->battletag\".";
87+
88+
} elseif( isset($_GET['go']) ) {
89+
90+
$url = $battlenetService->getAuthorizationUri();
91+
header( "Location: $url" );
92+
93+
}

examples/init.example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
'key' => '',
2020
'secret' => '',
2121
),
22+
'battlenet' => array(
23+
'key' => '',
24+
'secret' => '',
25+
),
2226
'bitbucket' => array(
2327
'key' => '',
2428
'secret' => '',
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace OAuth\OAuth2\Service;
4+
5+
//-----------------------------------------------------------------------------
6+
use OAuth\OAuth2\Token\StdOAuth2Token;
7+
use OAuth\Common\Http\Exception\TokenResponseException;
8+
use OAuth\Common\Http\Uri\Uri;
9+
use OAuth\Common\Consumer\CredentialsInterface;
10+
use OAuth\Common\Http\Client\ClientInterface;
11+
use OAuth\Common\Storage\TokenStorageInterface;
12+
use OAuth\Common\Http\Uri\UriInterface;
13+
14+
//-----------------------------------------------------------------------------
15+
class 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+
}

0 commit comments

Comments
 (0)