WordPress.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\Common\Consumer\CredentialsInterface;
  4. use OAuth\Common\Http\Client\ClientInterface;
  5. use OAuth\Common\Http\Uri\UriInterface;
  6. use OAuth\Common\Storage\TokenStorageInterface;
  7. use OAuth\OAuth2\Token\StdOAuth2Token;
  8. use OAuth\Common\Http\Exception\TokenResponseException;
  9. use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
  10. use OAuth\Common\Http\Uri\Uri;
  11. /**
  12. * Class For WordPress OAuth
  13. */
  14. class WordPress extends AbstractService
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $accessType = 'online';
  20. /**
  21. * Construct
  22. *
  23. * @param CredentialsInterface $credentials credentials
  24. * @param ClientInterface $httpClient httpClient
  25. * @param TokenStorageInterface $storage storage
  26. * @param $scopes scope
  27. * @param UriInterface|null $baseApiUri baseApiUri
  28. * @throws Exception\InvalidScopeException
  29. */
  30. public function __construct(CredentialsInterface $credentials, ClientInterface $httpClient, TokenStorageInterface $storage, $scopes = array(), UriInterface $baseApiUri = null)
  31. {
  32. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  33. if (null === $baseApiUri) {
  34. $this->baseApiUri = new Uri('https://addresse_de_votre_site_wordpress');
  35. }
  36. }
  37. /*
  38. // 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"
  39. public $approvalPrompt='auto';
  40. public function setApprouvalPrompt($prompt)
  41. {
  42. if (!in_array($prompt, array('auto', 'force'), true)) {
  43. // @todo Maybe could we rename this exception
  44. throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
  45. }
  46. $this->approvalPrompt = $prompt;
  47. }*/
  48. /**
  49. * @return Uri
  50. */
  51. public function getAuthorizationEndpoint()
  52. {
  53. return new Uri(sprintf('%s/oauth/authorize', $this->baseApiUri));
  54. }
  55. /**
  56. * @return Uri
  57. */
  58. public function getAccessTokenEndpoint()
  59. {
  60. return new Uri(sprintf('%s/oauth/token', $this->baseApiUri));
  61. }
  62. /**
  63. * @return int
  64. */
  65. protected function getAuthorizationMethod()
  66. {
  67. global $conf;
  68. return empty($conf->global->OAUTH_WORDPRESS_AUTHORIZATION_METHOD_QUERY_STRING) ? static::AUTHORIZATION_METHOD_HEADER_BEARER : static::AUTHORIZATION_METHOD_QUERY_STRING;
  69. }
  70. /**
  71. * @param $responseBody responseBody
  72. * @return StdOAuth2Token
  73. * @throws TokenResponseException
  74. */
  75. protected function parseAccessTokenResponse($responseBody)
  76. {
  77. $data = json_decode($responseBody, true);
  78. if (null === $data || !is_array($data)) {
  79. throw new TokenResponseException('Unable to parse response: "'.(isset($responseBody)?$responseBody:'NULL').'"');
  80. } elseif (isset($data['error'])) {
  81. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '" : "'.$data['error_description'].'"');
  82. }
  83. $token = new StdOAuth2Token();
  84. $token->setAccessToken($data['access_token']);
  85. $token->setLifetime($data['expires_in']);
  86. if (isset($data['refresh_token'])) {
  87. $token->setRefreshToken($data['refresh_token']);
  88. unset($data['refresh_token']);
  89. }
  90. unset($data['access_token']);
  91. unset($data['expires_in']);
  92. $token->setExtraParams($data);
  93. return $token;
  94. }
  95. }