Skip to content

Add Battle.Net Services #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/OAuth2/Service/BattleNetBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetBase extends AbstractService
{
/**
* Scopes
*
* @var string
*/

const SCOPE_WOW_PROFILE = 'wow.profile';
const SCOPE_SC2_PROFILE = 'sc2.profile';

/**
* BattleNet Region
*
* @var string
*/
protected $region;

public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
if ( null === $baseApiUri ) {
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}

/**
* @return \OAuth\Common\Http\Uri\UriInterface
*/
public function getAuthorizationEndpoint()
{
return new Uri('https://' . $this->region . '.battle.net/oauth/authorize');
}

/**
* @return \OAuth\Common\Http\Uri\UriInterface
*/
public function getAccessTokenEndpoint()
{
return new Uri('https://' . $this->region . '.battle.net/oauth/token');
}

/**
* @param string $responseBody
* @return \OAuth\Common\Token\TokenInterface|\OAuth\OAuth2\Token\StdOAuth2Token
* @throws \OAuth\Common\Http\Exception\TokenResponseException
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode( $responseBody, true );

if( null === $data || !is_array($data) ) {
throw new TokenResponseException('Unable to parse response.');
} elseif( isset($data['error'] ) ) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();

$token->setAccessToken( $data['access_token'] );
// I'm invincible!!!
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
unset( $data['access_token'] );
$token->setExtraParams( $data );

return $token;
}

/**
* @return int
*/
protected function getAuthorizationMethod()
{
return static::AUTHORIZATION_METHOD_QUERY_STRING;
}
}
18 changes: 18 additions & 0 deletions src/OAuth2/Service/BattleNetCN.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace OAuth\OAuth2\Service;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetCN extends BattleNetBase
{
public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
$this->region = 'cn';
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}
18 changes: 18 additions & 0 deletions src/OAuth2/Service/BattleNetEU.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace OAuth\OAuth2\Service;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetEU extends BattleNetBase
{
public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
$this->region = 'eu';
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}
18 changes: 18 additions & 0 deletions src/OAuth2/Service/BattleNetKR.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace OAuth\OAuth2\Service;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetKR extends BattleNetBase
{
public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
$this->region = 'kr';
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}
18 changes: 18 additions & 0 deletions src/OAuth2/Service/BattleNetTW.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace OAuth\OAuth2\Service;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetTW extends BattleNetBase
{
public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
$this->region = 'tw';
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}
18 changes: 18 additions & 0 deletions src/OAuth2/Service/BattleNetUS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace OAuth\OAuth2\Service;

use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;

class BattleNetUS extends BattleNetBase
{
public function __construct(Credentials $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
{
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
$this->region = 'us';
$this->baseApiUri = new Uri('https://' . $this->region . '.api.battle.net/');
}
}