BattleNet.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. //-----------------------------------------------------------------------------
  4. use OAuth\OAuth2\Token\StdOAuth2Token;
  5. use OAuth\Common\Http\Exception\TokenResponseException;
  6. use OAuth\Common\Http\Uri\Uri;
  7. use OAuth\Common\Consumer\CredentialsInterface;
  8. use OAuth\Common\Http\Client\ClientInterface;
  9. use OAuth\Common\Storage\TokenStorageInterface;
  10. use OAuth\Common\Http\Uri\UriInterface;
  11. //-----------------------------------------------------------------------------
  12. class BattleNet extends AbstractService {
  13. /** -----------------------------------------------------------------------
  14. * Defined scopes.
  15. *
  16. * @link https://dev.battle.net/docs
  17. */
  18. const SCOPE_WOW_PROFILE = "wow.profile";
  19. const SCOPE_SC2_PROFILE = "sc2.profile";
  20. /** -----------------------------------------------------------------------
  21. * Defined API URIs.
  22. *
  23. * @link https://dev.battle.net/docs
  24. */
  25. const API_URI_US = 'https://us.api.battle.net/';
  26. const API_URI_EU = 'https://eu.api.battle.net/';
  27. const API_URI_KR = 'https://kr.api.battle.net/';
  28. const API_URI_TW = 'https://tw.api.battle.net/';
  29. const API_URI_CN = 'https://api.battlenet.com.cn/';
  30. const API_URI_SEA = 'https://sea.api.battle.net/';
  31. public function __construct( CredentialsInterface $credentials,
  32. ClientInterface $httpClient,
  33. TokenStorageInterface $storage,
  34. $scopes = array(),
  35. UriInterface $baseApiUri = null ) {
  36. parent::__construct( $credentials, $httpClient, $storage,
  37. $scopes, $baseApiUri );
  38. if( $baseApiUri === null ) {
  39. $this->baseApiUri = new Uri( self::API_URI_US );
  40. }
  41. }
  42. /** -----------------------------------------------------------------------
  43. * Translates the current base API URI into an OAuth base URI.
  44. *
  45. * @returns string Base URI of oauth services.
  46. */
  47. private function GetOAuthBaseUri() {
  48. // i love china
  49. switch( $this->baseApiUri ) {
  50. case self::API_URI_US: return 'https://us.battle.net/oauth/';
  51. case self::API_URI_EU: return 'https://eu.battle.net/oauth/';
  52. case self::API_URI_KR: return 'https://kr.battle.net/oauth/';
  53. case self::API_URI_TW: return 'https://tw.battle.net/oauth/';
  54. case self::API_URI_CN: return 'https://www.battlenet.com.cn/oauth/';
  55. case self::API_URI_SEA: return 'https://sea.battle.net/oauth/';
  56. }
  57. }
  58. /** -----------------------------------------------------------------------
  59. * {@inheritdoc}
  60. */
  61. public function getAuthorizationEndpoint() {
  62. return new Uri( $this->GetOAuthBaseUri() . 'authorize' );
  63. }
  64. /** -----------------------------------------------------------------------
  65. * {@inheritdoc}
  66. */
  67. public function getAccessTokenEndpoint() {
  68. return new Uri( $this->GetOAuthBaseUri() . 'token' );
  69. }
  70. /** -----------------------------------------------------------------------
  71. * {@inheritdoc}
  72. */
  73. protected function getAuthorizationMethod()
  74. {
  75. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  76. }
  77. /** -----------------------------------------------------------------------
  78. * {@inheritdoc}
  79. */
  80. protected function parseAccessTokenResponse( $responseBody )
  81. {
  82. $data = json_decode($responseBody, true);
  83. if( $data === null || !is_array($data) ) {
  84. throw new TokenResponseException( 'Unable to parse response.' );
  85. } elseif( isset($data['error']) ) {
  86. $err = $data['error'];
  87. throw new TokenResponseException(
  88. "Error in retrieving token: \"$err\"" );
  89. }
  90. $token = new StdOAuth2Token( $data['access_token'], null,
  91. $data['expires_in'] );
  92. unset( $data['access_token'] );
  93. unset( $data['expires_in'] );
  94. $token->setExtraParams( $data );
  95. return $token;
  96. }
  97. }