Simplify token creation

This commit is contained in:
Michael 2023-05-21 19:25:57 +00:00
parent 77813a2acd
commit ea6e79448d
3 changed files with 44 additions and 37 deletions

View file

@ -46,7 +46,9 @@ function bluesky_settings(array &$data)
$host = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'host') ?: 'https://bsky.social'; $host = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'host') ?: 'https://bsky.social';
$handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle'); $handle = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'handle');
$did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did'); $did = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
$username = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'username'); $token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'bluesky', 'access_token');
$status = $token ? DI::l10n()->t("You are authenticated to Bluesky. For security reasons the password isn't stored.") : DI::l10n()->t('You are not authenticated. Please enter the app password.');
$t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/bluesky/'); $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/bluesky/');
$html = Renderer::replaceMacros($t, [ $html = Renderer::replaceMacros($t, [
@ -55,8 +57,8 @@ function bluesky_settings(array &$data)
'$host' => ['bluesky_host', DI::l10n()->t('Bluesky host'), $host, '', '', 'readonly'], '$host' => ['bluesky_host', DI::l10n()->t('Bluesky host'), $host, '', '', 'readonly'],
'$handle' => ['bluesky_handle', DI::l10n()->t('Bluesky handle'), $handle], '$handle' => ['bluesky_handle', DI::l10n()->t('Bluesky handle'), $handle],
'$did' => ['bluesky_did', DI::l10n()->t('Bluesky DID'), $did, DI::l10n()->t('This is the unique identifier. It will be fetched automatically, when the handle is entered.'), '', 'readonly'], '$did' => ['bluesky_did', DI::l10n()->t('Bluesky DID'), $did, DI::l10n()->t('This is the unique identifier. It will be fetched automatically, when the handle is entered.'), '', 'readonly'],
'$username' => ['bluesky_username', DI::l10n()->t('Bluesky app username'), $username, DI::l10n()->t("Please don't add your real username here, but instead create a specific app username and app password in the Bluesky settings.")], '$password' => ['bluesky_password', DI::l10n()->t('Bluesky app password'), '', DI::l10n()->t("Please don't add your real password here, but instead create a specific app password in the Bluesky settings.")],
'$password' => ['bluesky_password', DI::l10n()->t('Bluesky app password'), ''], '$status' => $status
]); ]);
$data = [ $data = [
@ -85,11 +87,6 @@ function bluesky_settings_post(array &$b)
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default', intval($_POST['bluesky_bydefault'])); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'post_by_default', intval($_POST['bluesky_bydefault']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'host', $host); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'host', $host);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'handle', $handle); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'handle', $handle);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'username', $_POST['bluesky_username']);
if (!empty($_POST['bluesky_password'])) {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'bluesky', 'app_password', $_POST['bluesky_password']);
}
if (!empty($host) && !empty($handle)) { if (!empty($host) && !empty($handle)) {
if (empty($old_did) || $old_host != $host || $old_handle != $handle) { if (empty($old_did) || $old_host != $host || $old_handle != $handle) {
@ -98,6 +95,11 @@ function bluesky_settings_post(array &$b)
} else { } else {
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'did'); DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'bluesky', 'did');
} }
if (!empty($_POST['bluesky_password'])) {
bluesky_create_token(DI::userSession()->getLocalUserId(), $_POST['bluesky_password']);
}
} }
function bluesky_jot_nets(array &$jotnets_fields) function bluesky_jot_nets(array &$jotnets_fields)
@ -301,7 +303,7 @@ function bluesky_get_token(int $uid): string
$token = DI::pConfig()->get($uid, 'bluesky', 'access_token'); $token = DI::pConfig()->get($uid, 'bluesky', 'access_token');
$created = DI::pConfig()->get($uid, 'bluesky', 'token_created'); $created = DI::pConfig()->get($uid, 'bluesky', 'token_created');
if (empty($token)) { if (empty($token)) {
return bluesky_create_token($uid); return '';
} }
if ($created + 300 < time()) { if ($created + 300 < time()) {
@ -326,10 +328,9 @@ function bluesky_refresh_token(int $uid): string
return $data->accessJwt; return $data->accessJwt;
} }
function bluesky_create_token(int $uid): string function bluesky_create_token(int $uid, string $password): string
{ {
$did = DI::pConfig()->get($uid, 'bluesky', 'did'); $did = DI::pConfig()->get($uid, 'bluesky', 'did');
$password = DI::pConfig()->get($uid, 'bluesky', 'app_password');
$data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']); $data = bluesky_post($uid, '/xrpc/com.atproto.server.createSession', json_encode(['identifier' => $did, 'password' => $password]), ['Content-type' => 'application/json']);
if (empty($data)) { if (empty($data)) {

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-05-21 16:55+0000\n" "POT-Creation-Date: 2023-05-21 19:24+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,50 +17,56 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: bluesky.php:52 #: bluesky.php:51
msgid "Enable Bluesky Post Addon" msgid ""
"You are authenticated to Bluesky. For security reasons the password isn't "
"stored."
msgstr "" msgstr ""
#: bluesky.php:53 #: bluesky.php:51
msgid "Post to Bluesky by default" msgid "You are not authenticated. Please enter the app password."
msgstr ""
#: bluesky.php:54
msgid "Bluesky host"
msgstr "" msgstr ""
#: bluesky.php:55 #: bluesky.php:55
msgid "Enable Bluesky Post Addon"
msgstr ""
#: bluesky.php:56
msgid "Post to Bluesky by default"
msgstr ""
#: bluesky.php:57
msgid "Bluesky host"
msgstr ""
#: bluesky.php:58
msgid "Bluesky handle" msgid "Bluesky handle"
msgstr "" msgstr ""
#: bluesky.php:56 #: bluesky.php:59
msgid "Bluesky DID" msgid "Bluesky DID"
msgstr "" msgstr ""
#: bluesky.php:56 #: bluesky.php:59
msgid "" msgid ""
"This is the unique identifier. It will be fetched automatically, when the " "This is the unique identifier. It will be fetched automatically, when the "
"handle is entered." "handle is entered."
msgstr "" msgstr ""
#: bluesky.php:57 #: bluesky.php:60
msgid "Bluesky app username"
msgstr ""
#: bluesky.php:57
msgid ""
"Please don't add your real username here, but instead create a specific app "
"username and app password in the Bluesky settings."
msgstr ""
#: bluesky.php:58
msgid "Bluesky app password" msgid "Bluesky app password"
msgstr "" msgstr ""
#: bluesky.php:63 #: bluesky.php:60
msgid ""
"Please don't add your real password here, but instead create a specific app "
"password in the Bluesky settings."
msgstr ""
#: bluesky.php:66
msgid "Bluesky Export" msgid "Bluesky Export"
msgstr "" msgstr ""
#: bluesky.php:113 #: bluesky.php:116
msgid "Post to Bluesky" msgid "Post to Bluesky"
msgstr "" msgstr ""

View file

@ -1,7 +1,7 @@
<p>{{$status}}</p>
{{include file="field_checkbox.tpl" field=$enable}} {{include file="field_checkbox.tpl" field=$enable}}
{{include file="field_checkbox.tpl" field=$bydefault}} {{include file="field_checkbox.tpl" field=$bydefault}}
{{include file="field_input.tpl" field=$host}} {{include file="field_input.tpl" field=$host}}
{{include file="field_input.tpl" field=$handle}} {{include file="field_input.tpl" field=$handle}}
{{include file="field_input.tpl" field=$did}} {{include file="field_input.tpl" field=$did}}
{{include file="field_input.tpl" field=$username}}
{{include file="field_input.tpl" field=$password}} {{include file="field_input.tpl" field=$password}}