Skip to content

Instantly share code, notes, and snippets.

@darthmaim
Created March 14, 2015 10:29
Show Gist options
  • Save darthmaim/acfad8518b05412fd796 to your computer and use it in GitHub Desktop.
Save darthmaim/acfad8518b05412fd796 to your computer and use it in GitHub Desktop.
GuildWars2 OAuth2 serviceclass for https://github.com/Lusitanian/PHPoAuthLib
<?php
namespace GW2Treasures\GW2Api\OAuth;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\OAuth2\Service\AbstractService;
use OAuth\OAuth2\Token\StdOAuth2Token;
class GuildWarsOAuth2Service extends AbstractService {
const SCOPE_ACCOUNT = 'account';
const SCOPE_OFFLINE = 'offline';
/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse( $responseBody ) {
// example valid response: {"access_token":"xxx","token_type":"Bearer","scope":"account","refresh_token":"xxx"}
// example error response: {"error":"invalid_grant","error_description":"Grant not found, revoked, or already consumed","text":"ErrSessionNotFound"}
$data = json_decode( $responseBody, true );
if( is_null( $data ) || !is_array( $data )) {
throw new TokenResponseException( 'Unable to parse response.' );
} elseif( isset( $data['error_description'] )) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
} elseif( isset( $data['error'] )) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}
$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
// TODO: not supported by guildwars2 yet
// $token->setLifeTime($data['expires_in']);
if( isset( $data['refresh_token'] )) {
$token->setRefreshToken( $data['refresh_token'] );
unset( $data['refresh_token'] );
}
unset( $data['access_token'] );
unset( $data['expires_in'] );
$token->setExtraParams( $data );
return $token;
}
/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint() {
return new Uri('https://account.guildwars2.com/oauth2/authorization');
}
/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint() {
return new Uri('https://account.guildwars2.com/oauth2/token');
}
/**
* {@inheritdoc}
*/
protected function getAuthorizationMethod() {
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment