friendica-addons/tumblr/library/tumblroauth.php

191 lines
5.3 KiB
PHP
Raw Normal View History

<?php
/*
* Abraham Williams (abraham@abrah.am) http://abrah.am
*
* The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)
*/
2023-04-18 05:56:32 +00:00
use Friendica\Core\Logger;
2023-04-06 19:10:32 +00:00
use Friendica\DI;
use Friendica\Security\OAuth1\OAuthConsumer;
use Friendica\Security\OAuth1\OAuthRequest;
2020-09-30 09:26:52 +00:00
use Friendica\Security\OAuth1\Signature\OAuthSignatureMethod_HMAC_SHA1;
use Friendica\Security\OAuth1\OAuthToken;
use Friendica\Security\OAuth1\OAuthUtil;
2023-04-18 05:56:32 +00:00
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use Psr\Http\Message\ResponseInterface;
/**
* Tumblr OAuth class
*/
class TumblrOAuth
{
2023-04-18 05:56:32 +00:00
private $consumer_key;
private $consumer_secret;
private $oauth_token;
private $oauth_token_secret;
2023-04-18 05:56:32 +00:00
/** @var GuzzleHttp\Client */
private $client;
2023-04-06 19:10:32 +00:00
// API URLs
const accessTokenURL = 'https://www.tumblr.com/oauth/access_token';
const authorizeURL = 'https://www.tumblr.com/oauth/authorize';
const requestTokenURL = 'https://www.tumblr.com/oauth/request_token';
2023-04-18 05:56:32 +00:00
function __construct(string $consumer_key, string $consumer_secret, string $oauth_token = '', string $oauth_token_secret = '')
{
2023-04-18 05:56:32 +00:00
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->oauth_token = $oauth_token;
$this->oauth_token_secret = $oauth_token_secret;
if (empty($this->oauth_token) || empty($this->oauth_token_secret)) {
return;
}
$stack = HandlerStack::create();
$middleware = new Oauth1([
'consumer_key' => $this->consumer_key,
'consumer_secret' => $this->consumer_secret,
'token' => $this->oauth_token,
'token_secret' => $this->oauth_token_secret
]);
$stack->push($middleware);
$this->client = new Client([
'base_uri' => 'https://api.tumblr.com/v2/',
'handler' => $stack
]);
}
/**
* Get a request_token from Tumblr
*
2023-04-06 19:10:32 +00:00
* @param string $oauth_callback
* @return array
*/
2023-04-06 19:10:32 +00:00
function getRequestToken(string $oauth_callback): array
{
2023-04-06 19:10:32 +00:00
$request = $this->oAuthRequest(self::requestTokenURL, ['oauth_callback' => $oauth_callback]);
2023-04-18 05:56:32 +00:00
if (empty($request)) {
return [];
}
2023-04-06 19:10:32 +00:00
return OAuthUtil::parse_parameters($request);
}
/**
* Get the authorize URL
*
2023-04-06 19:10:32 +00:00
* @param string $oauth_token
* @return string
*/
2023-04-06 19:10:32 +00:00
function getAuthorizeURL(string $oauth_token): string
{
2023-04-06 19:10:32 +00:00
return self::authorizeURL . "?oauth_token={$oauth_token}";
}
/**
* Exchange request token and secret for an access token and
* secret, to sign API calls.
*
2023-04-06 19:10:32 +00:00
* @param string $oauth_verifier
* @param string $request_token
* @param string $request_token_secret
* @return array ("oauth_token" => "the-access-token",
* "oauth_token_secret" => "the-access-secret",
* "user_id" => "9436992",
* "screen_name" => "abraham")
*/
2023-04-06 19:10:32 +00:00
function getAccessToken(string $oauth_verifier, string $request_token, string $request_token_secret): array
{
2023-04-06 19:10:32 +00:00
$token = new OAuthToken($request_token, $request_token_secret);
$parameters = [];
if (!empty($oauth_verifier)) {
$parameters['oauth_verifier'] = $oauth_verifier;
}
2023-04-06 19:10:32 +00:00
$request = $this->oAuthRequest(self::accessTokenURL, $parameters, $token);
2023-04-18 05:56:32 +00:00
if (empty($request)) {
return [];
}
2023-04-06 19:10:32 +00:00
return OAuthUtil::parse_parameters($request);
}
/**
* Format and sign an OAuth / API request
*
2023-04-06 19:10:32 +00:00
* @param string $url
* @param array $parameters
* @param OAuthToken $token $name
* @return string
*/
2023-04-06 19:10:32 +00:00
private function oAuthRequest(string $url, array $parameters, OAuthToken $token = null): string
{
2023-04-18 05:56:32 +00:00
$consumer = new OAuthConsumer($this->consumer_key, $this->consumer_secret);
$sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$request = OAuthRequest::from_consumer_and_token($consumer, 'GET', $url, $parameters, $token);
$request->sign_request($sha1_method, $consumer, $token);
2023-04-06 19:10:32 +00:00
$curlResult = DI::httpClient()->get($request->to_url());
if ($curlResult->isSuccess()) {
return $curlResult->getBody();
}
2023-04-06 19:10:32 +00:00
return '';
}
2023-04-18 05:56:32 +00:00
public function get(string $url, array $parameters = []): stdClass
{
if (!empty($parameters)) {
$url .= '?' . http_build_query($parameters);
}
try {
$response = $this->client->get($url, ['auth' => 'oauth']);
} catch (RequestException $exception) {
$response = $exception->getResponse();
Logger::notice('Get failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
}
return $this->formatResponse($response);
}
public function post(string $url, array $parameter): stdClass
{
try {
$response = $this->client->post($url, ['auth' => 'oauth', 'json' => $parameter]);
} catch (RequestException $exception) {
$response = $exception->getResponse();
Logger::notice('Post failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
}
return $this->formatResponse($response);
}
private function formatResponse(ResponseInterface $response = null): stdClass
{
if (!is_null($response)) {
$content = $response->getBody()->getContents();
if (!empty($content)) {
$result = json_decode($content);
}
}
if (empty($result) || empty($result->meta)) {
$result = new stdClass;
$result->meta = new stdClass;
$result->meta->status = 500;
$result->meta->msg = '';
$result->response = [];
$result->errors = [];
}
return $result;
}
}