2021-12-17 12:07:04 +01:00
< ? php
namespace OAuth\OAuth2\Service ;
use OAuth\Common\Consumer\CredentialsInterface ;
use OAuth\Common\Http\Client\ClientInterface ;
use OAuth\Common\Http\Uri\UriInterface ;
use OAuth\Common\Storage\TokenStorageInterface ;
use OAuth\OAuth2\Token\StdOAuth2Token ;
use OAuth\Common\Http\Exception\TokenResponseException ;
use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException ;
use OAuth\Common\Http\Uri\Uri ;
2022-01-19 13:31:51 +01:00
/**
* Class For WordPress OAuth
*/
2021-12-17 12:07:04 +01:00
class WordPress extends AbstractService
{
2022-01-19 13:31:51 +01:00
/**
* @ var string
*/
protected $accessType = 'online' ;
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
/**
* Construct
*
* @ param CredentialsInterface $credentials credentials
* @ param ClientInterface $httpClient httpClient
* @ param TokenStorageInterface $storage storage
* @ param $scopes scope
* @ param UriInterface | null $baseApiUri baseApiUri
* @ throws Exception\InvalidScopeException
*/
public function __construct ( CredentialsInterface $credentials , ClientInterface $httpClient , TokenStorageInterface $storage , $scopes = array (), UriInterface $baseApiUri = null )
{
parent :: __construct ( $credentials , $httpClient , $storage , $scopes , $baseApiUri , true );
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
if ( null === $baseApiUri ) {
$this -> baseApiUri = new Uri ( 'https://addresse_de_votre_site_wordpress' );
}
}
/*
// LDR CHANGE Add approval_prompt to force the prompt if value is set to 'force' so it force return of a "refresh token" in addition to "standard token"
public $approvalPrompt = 'auto' ;
public function setApprouvalPrompt ( $prompt )
{
if ( ! in_array ( $prompt , array ( 'auto' , 'force' ), true )) {
// @todo Maybe could we rename this exception
throw new InvalidAccessTypeException ( 'Invalid approuvalPrompt, expected either auto or force.' );
}
$this -> approvalPrompt = $prompt ;
} */
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
/**
* @ return Uri
*/
public function getAuthorizationEndpoint ()
{
return new Uri ( sprintf ( '%s/oauth/authorize' , $this -> baseApiUri ));
}
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
/**
* @ return Uri
*/
public function getAccessTokenEndpoint ()
{
return new Uri ( sprintf ( '%s/oauth/token' , $this -> baseApiUri ));
}
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
/**
* @ return int
*/
protected function getAuthorizationMethod ()
{
global $conf ;
return empty ( $conf -> global -> OAUTH_WORDPRESS_AUTHORIZATION_METHOD_QUERY_STRING ) ? static :: AUTHORIZATION_METHOD_HEADER_BEARER : static :: AUTHORIZATION_METHOD_QUERY_STRING ;
}
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
/**
* @ param $responseBody responseBody
* @ return StdOAuth2Token
* @ throws TokenResponseException
*/
protected function parseAccessTokenResponse ( $responseBody )
{
$data = json_decode ( $responseBody , true );
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
if ( null === $data || ! is_array ( $data )) {
throw new TokenResponseException ( 'Unable to parse response: "' . ( isset ( $responseBody ) ? $responseBody : 'NULL' ) . '"' );
} elseif ( isset ( $data [ 'error' ])) {
throw new TokenResponseException ( 'Error in retrieving token: "' . $data [ 'error' ] . '" : "' . $data [ 'error_description' ] . '"' );
}
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
$token = new StdOAuth2Token ();
$token -> setAccessToken ( $data [ 'access_token' ]);
$token -> setLifetime ( $data [ 'expires_in' ]);
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
if ( isset ( $data [ 'refresh_token' ])) {
$token -> setRefreshToken ( $data [ 'refresh_token' ]);
unset ( $data [ 'refresh_token' ]);
}
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
unset ( $data [ 'access_token' ]);
unset ( $data [ 'expires_in' ]);
2021-12-17 12:07:04 +01:00
2022-01-19 13:31:51 +01:00
$token -> setExtraParams ( $data );
return $token ;
}
2021-12-17 12:07:04 +01:00
}