Use OAUTH and fetch access_token automatically

This commit is contained in:
Leon Haag-Fank 2023-11-10 16:16:39 +01:00
parent 711f1a1a08
commit af83043ef0
3 changed files with 66 additions and 11 deletions

View file

@ -22,9 +22,14 @@ class WallabagExtension extends Minz_Extension {
}
public function handleConfigureAction() {
$this->registerTranslates();
if (Minz_Request::isPost()) {
FreshRSS_Context::$user_conf->wallabag_endpoint = Minz_Request::param('wallabag_endpoint');
FreshRSS_Context::$user_conf->wallabag_token = Minz_Request::param('wallabag_token');
FreshRSS_Context::$user_conf->wallabag_client_id = Minz_Request::param('wallabag_client_id');
FreshRSS_Context::$user_conf->wallabag_client_secret = Minz_Request::param('wallabag_client_secret');
FreshRSS_Context::$user_conf->wallabag_username = Minz_Request::param('wallabag_username');
FreshRSS_Context::$user_conf->wallabag_password = Minz_Request::param('wallabag_password');
FreshRSS_Context::$user_conf->wallabag_feeds = Minz_Request::param('wallabag_feeds', '');
FreshRSS_Context::$user_conf->wallabag_use_tags = Minz_Request::param('wallabag_use_tags', '');
FreshRSS_Context::$user_conf->wallabag_add_tags = Minz_Request::param('wallabag_add_tags', '');
@ -32,17 +37,46 @@ class WallabagExtension extends Minz_Extension {
}
}
public function oauth($refresh_token = null) {
$endpoint = FreshRSS_Context::$user_conf->wallabag_endpoint;
$client_id = FreshRSS_Context::$user_conf->wallabag_client_id;
$client_secret = FreshRSS_Context::$user_conf->wallabag_client_secret;
$username = FreshRSS_Context::$user_conf->wallabag_username;
$password = FreshRSS_Context::$user_conf->wallabag_password;
if (is_null($endpoint) || is_null($client_id) || is_null($client_secret) || is_null($password) || is_null($username)) {
return null;
}
if (is_null($refresh_token)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, join_path($endpoint, 'oauth/v2/token'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'password',
'client_id' => $client_id,
'client_secret' => $client_secret,
'username' => $username,
'password' => $password,
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return(json_decode($result, true));
}
}
public function wallabag(FreshRSS_Entry $entry): FreshRSS_Entry {
$endpoint = FreshRSS_Context::$user_conf->wallabag_endpoint;
$token = FreshRSS_Context::$user_conf->wallabag_token;
if (is_null($endpoint)) {
return $entry;
}
$feeds = explode(',', FreshRSS_Context::$user_conf->wallabag_feeds);
$use_tags = FreshRSS_Context::$user_conf->wallabag_use_tags;
$add_tags = explode(',', FreshRSS_Context::$user_conf->wallabag_add_tags);
if (is_null($endpoint) || is_null($token)) {
return $entry;
}
if (in_array($entry->feedId(), $feeds)) {
$token = $this->oauth()['access_token'];
$tags = $add_tags;
if ($use_tags) {
$tags = array_merge($tags, $entry->tags());
@ -53,11 +87,11 @@ class WallabagExtension extends Minz_Extension {
curl_setopt($ch, CURLOPT_URL, join_path($endpoint, 'api/entries'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization:Bearer $token",
"Authorization:Bearer $token",
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'url' => $link,
'tags' => join(',', $tags),
'url' => $link,
'tags' => join(',', $tags),
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);