Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit cd32082

Browse files
committed
✨ BattleNet
1 parent 19491ed commit cd32082

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ For documentation see [`chillerlan/php-oauth-core`](https://github.com/chillerla
3636
Provider | API keys | revoke access
3737
----------|----------|---------------
3838
[Amazon](https://login.amazon.com/) | [link](https://sellercentral.amazon.com/hz/home) |
39+
[BattleNet](https://develop.battle.net/documentation) | [link](https://develop.battle.net/access/clients) | [link](https://account.blizzard.com/connections)
3940
[BigCartel](https://developers.bigcartel.com/api/v1) | [link](https://bigcartel.wufoo.com/forms/big-cartel-api-application/) | [link](https://my.bigcartel.com/account)
4041
[Bitbucket](https://developer.atlassian.com/bitbucket/api/2/reference/) | [link](https://developer.atlassian.com/apps/) |
4142
[Deezer](https://developers.deezer.com/api) | [link](http://developers.deezer.com/myapps) | [link](https://www.deezer.com/account/apps)

config/.env_example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ AMAZON_KEY=
99
AMAZON_SECRET=
1010
AMAZON_CALLBACK_URL=
1111

12+
# https://develop.battle.net/access/clients
13+
BATTLENET_KEY=
14+
BATTLENET_SECRET=
15+
BATTLENET_CALLBACK_URL=
16+
#BATTLENET_TESTUSER=
17+
1218
# https://bigcartel.wufoo.com/confirm/big-cartel-api-application/
1319
BIGCARTEL_KEY=
1420
BIGCARTEL_SECRET=

examples/get-token/BattleNet.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* @link https://develop.battle.net/documentation/guides/using-oauth
4+
*
5+
* @filesource BattleNet.php
6+
* @created 02.08.2019
7+
* @author smiley <smiley@chillerlan.net>
8+
* @copyright 2019 smiley
9+
* @license MIT
10+
*/
11+
12+
use chillerlan\HTTP\Psr7;
13+
use chillerlan\OAuth\Providers\BattleNet\BattleNet;
14+
15+
$ENVVAR = 'BATTLENET';
16+
17+
/** @var \chillerlan\Settings\SettingsContainerInterface $options */
18+
$options = null;
19+
20+
/** @var \Psr\Log\LoggerInterface $logger */
21+
$logger = null;
22+
23+
/** @var \Psr\Http\Client\ClientInterface $http */
24+
$http = null;
25+
26+
/** @var \chillerlan\OAuth\Storage\OAuthStorageInterface $storage */
27+
$storage = null;
28+
29+
require_once __DIR__.'/../provider-example-common.php';
30+
31+
$battlenet = new BattleNet($http, $storage, $options, $logger);
32+
33+
$servicename = $battlenet->serviceName;
34+
35+
$scopes = [
36+
BattleNet::SCOPE_OPENID,
37+
BattleNet::SCOPE_PROFILE_D3,
38+
BattleNet::SCOPE_PROFILE_SC2,
39+
BattleNet::SCOPE_PROFILE_WOW,
40+
];
41+
42+
// step 2: redirect to the provider's login screen
43+
if(isset($_GET['login']) && $_GET['login'] === $servicename){
44+
header('Location: '.$battlenet->getAuthURL(null, $scopes));
45+
}
46+
// step 3: receive the access token
47+
elseif(isset($_GET['code']) && isset($_GET['state'])){
48+
$token = $battlenet->getAccessToken($_GET['code'], $_GET['state']);
49+
50+
// save the token [...]
51+
52+
// access granted, redirect
53+
header('Location: ?granted='.$servicename);
54+
}
55+
// step 4: verify the token and use the API
56+
elseif(isset($_GET['granted']) && $_GET['granted'] === $servicename){
57+
echo '<pre>'.print_r(Psr7\get_json($battlenet->userinfo()), true).'</pre>';
58+
}
59+
// step 1 (optional): display a login link
60+
else{
61+
echo '<a href="?login='.$servicename.'">connect with '.$servicename.'!</a>';
62+
}
63+
64+
exit;

src/BattleNet/BattleNet.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Class BattleNet
4+
*
5+
* @link https://develop.battle.net/documentation
6+
*
7+
* @filesource BattleNet.php
8+
* @created 02.08.2019
9+
* @package chillerlan\OAuth\Providers\BattleNet
10+
* @author smiley <smiley@chillerlan.net>
11+
* @copyright 2019 smiley
12+
* @license MIT
13+
*/
14+
15+
namespace chillerlan\OAuth\Providers\BattleNet;
16+
17+
use chillerlan\OAuth\Storage\OAuthStorageInterface;
18+
use chillerlan\Settings\SettingsContainerInterface;
19+
use chillerlan\OAuth\Core\{ClientCredentials, CSRFToken, OAuth2Provider, ProviderException, TokenExpires};
20+
use Psr\Http\Client\ClientInterface;
21+
use Psr\Log\LoggerInterface;
22+
23+
/**
24+
* @method \Psr\Http\Message\ResponseInterface userinfo(array $params = ['access_token'])
25+
*/
26+
class BattleNet extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenExpires{
27+
28+
public const SCOPE_OPENID = 'openid';
29+
public const SCOPE_PROFILE_D3 = 'd3.profile';
30+
public const SCOPE_PROFILE_SC2 = 'sc2.profile';
31+
public const SCOPE_PROFILE_WOW = 'wow.profile';
32+
33+
protected $apiDocs = 'https://develop.battle.net/documentation';
34+
protected $applicationURL = 'https://develop.battle.net/access/clients';
35+
protected $userRevokeURL = 'https://account.blizzard.com/connections';
36+
protected $endpointMap = BattleNetEndpoints::class;
37+
38+
/**
39+
* BattleNet constructor.
40+
*
41+
* @param \Psr\Http\Client\ClientInterface $http
42+
* @param \chillerlan\OAuth\Storage\OAuthStorageInterface $storage
43+
* @param \chillerlan\Settings\SettingsContainerInterface $options
44+
* @param \Psr\Log\LoggerInterface|null $logger
45+
*/
46+
public function __construct(ClientInterface $http, OAuthStorageInterface $storage, SettingsContainerInterface $options, LoggerInterface $logger = null){
47+
parent::__construct($http, $storage, $options, $logger);
48+
49+
$this->setRegion('eu');
50+
}
51+
52+
/**
53+
* @param string $region
54+
*
55+
* @return \chillerlan\OAuth\Providers\BattleNet\BattleNet
56+
* @throws \chillerlan\OAuth\Core\ProviderException
57+
*/
58+
public function setRegion(string $region):BattleNet{
59+
$region = \strtolower($region);
60+
61+
if(!\in_array($region, ['apac', 'cn', 'eu', 'us'], true)){
62+
throw new ProviderException('invalid region: '.$region);
63+
}
64+
65+
$url = 'https://'.($region === 'cn' ? 'www.battlenet.com.cn' : $region.'.battle.net');
66+
67+
$this->apiURL = $url;
68+
$this->authURL = $url.'/oauth/authorize';
69+
$this->accessTokenURL = $url.'/oauth/token';
70+
71+
return $this;
72+
}
73+
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Class BattleNetEndpoints
4+
*
5+
* @filesource BattleNetEndpoints.php
6+
* @created 02.08.2019
7+
* @package chillerlan\OAuth\Providers\BattleNet
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2019 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuth\Providers\BattleNet;
14+
15+
use chillerlan\HTTP\MagicAPI\EndpointMap;
16+
17+
/**
18+
* @link https://develop.battle.net/documentation/api-reference
19+
*/
20+
class BattleNetEndpoints extends EndpointMap{
21+
22+
protected $userinfo = [
23+
'path' => '/oauth/userinfo',
24+
'method' => 'GET',
25+
'query' => ['access_token'],
26+
'path_elements' => null,
27+
'body' => null,
28+
'headers' => null,
29+
];
30+
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Class BattleNetAPITest
4+
*
5+
* @filesource BattleNetAPITest.php
6+
* @created 03.08.2019
7+
* @package chillerlan\OAuthTest\Providers\BattleNet
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2019 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\Providers\BattleNet;
14+
15+
use chillerlan\OAuth\Providers\BattleNet\BattleNet;
16+
use chillerlan\OAuthTest\Providers\OAuth2APITest;
17+
18+
/**
19+
* @property \chillerlan\OAuth\Providers\BattleNet\BattleNet $provider
20+
*/
21+
class BattleNetAPITest extends OAuth2APITest{
22+
23+
protected $FQN = BattleNet::class;
24+
protected $ENV = 'BATTLENET';
25+
26+
public function testGetUserinfo(){
27+
$r = $this->provider->userinfo();
28+
29+
$this->assertSame($this->testuser, explode('#', $this->responseJson($r)->battletag)[0]);
30+
}
31+
32+
}

tests/BattleNet/BattleNetTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Class BattleNetTest
4+
*
5+
* @filesource BattleNetTest.php
6+
* @created 02.08.2019
7+
* @package chillerlan\OAuthTest\Providers\BattleNet
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2019 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\Providers\BattleNet;
14+
15+
use chillerlan\OAuth\Core\ProviderException;
16+
use chillerlan\OAuth\Providers\BattleNet\BattleNet;
17+
use chillerlan\OAuthTest\Providers\OAuth2ProviderTest;
18+
19+
/**
20+
* @property \chillerlan\OAuth\Providers\BattleNet\BattleNet $provider
21+
*/
22+
class BattleNetTest extends OAuth2ProviderTest{
23+
24+
protected $FQN = BattleNet::class;
25+
26+
public function testSetRegion(){
27+
$this->provider->setRegion('cn');
28+
$this->assertSame('https://www.battlenet.com.cn', $this->provider->apiURL);
29+
30+
$this->provider->setRegion('us');
31+
$this->assertSame('https://us.battle.net', $this->provider->apiURL);
32+
}
33+
34+
public function testSetRegionException(){
35+
$this->expectException(ProviderException::class);
36+
$this->expectExceptionMessage('invalid region: foo');
37+
38+
$this->provider->setRegion('foo');
39+
}
40+
41+
}

0 commit comments

Comments
 (0)