From cc5ba8af0f27188ce055c7e38375834795cdb10b Mon Sep 17 00:00:00 2001 From: cutls Date: Tue, 19 Jan 2021 04:06:13 +0900 Subject: [PATCH] add Pleroma refreshable token --- app/css/acct.css | 3 + app/js/login/login.js | 91 +++++++---- app/js/login/manager.js | 133 +++++++++------- app/js/ui/layout.js | 1 + app/js/ui/settings.js | 1 + app/view/make/acct.sample.html | 267 +++++++++++++++------------------ 6 files changed, 268 insertions(+), 228 deletions(-) diff --git a/app/css/acct.css b/app/css/acct.css index 561b67b9..92889a34 100644 --- a/app/css/acct.css +++ b/app/css/acct.css @@ -88,4 +88,7 @@ i.left { #domain-list .title { font-size: 1.3rem; +} +.text-line-icon { + font-size: 1rem; } \ No newline at end of file diff --git a/app/js/login/login.js b/app/js/login/login.js index e39f788d..c422e327 100644 --- a/app/js/login/login.js +++ b/app/js/login/login.js @@ -32,7 +32,7 @@ function ck() { } else { var obj = JSON.parse(multi) var jp = false - Object.keys(obj).forEach(function(key) { + Object.keys(obj).forEach(function (key) { var acct = obj[key] if (acct.domain) { refresh(key, true) @@ -50,7 +50,7 @@ function ck() { let tipsName = localStorage.getItem('tips') const matchCID = /custom:([abcdef0-9]{8}-[abcdef0-9]{4}-4[abcdef0-9]{3}-[abcdef0-9]{4}-[abcdef0-9]{12})/ if (tipsName) { - if(tipsName.match(matchCID)) { + if (tipsName.match(matchCID)) { const id = tipsName.match(matchCID)[1] tips('custom', id) } else { @@ -84,7 +84,7 @@ function login(url) { website: 'https://thedesk.top' }) ) - httpreq.onreadystatechange = function() { + httpreq.onreadystatechange = function () { if (httpreq.readyState === 4) { var json = httpreq.response if (this.status !== 200) { @@ -149,20 +149,20 @@ function code(code, mode) { code: code }) }) - .then(function(response) { + .then(function (response) { if (!response.ok) { - response.text().then(function(text) { + response.text().then(function (text) { setLog(response.url, response.status, text) }) } return response.json() }) - .catch(function(error) { + .catch(function (error) { todo(error) setLog(start, 'JSON', error) console.error(error) }) - .then(function(json) { + .then(function (json) { todo(json) if (json['access_token']) { localStorage.setItem(url + '_at', json['access_token']) @@ -188,20 +188,20 @@ function getdata() { Authorization: 'Bearer ' + at } }) - .then(function(response) { + .then(function (response) { if (!response.ok) { - response.text().then(function(text) { + response.text().then(function (text) { setLog(response.url, response.status, text) }) } return response.json() }) - .catch(function(error) { + .catch(function (error) { todo(error) setLog(start, 'JSON', error) console.error(error) }) - .then(function(json) { + .then(function (json) { if (json.error) { console.error('Error:' + json.error) M.toast({ html: lang.lang_fatalerroroccured + 'Error:' + json.error, displayLength: 5000 }) @@ -246,20 +246,20 @@ function getdataAdv(domain, at) { Authorization: 'Bearer ' + at } }) - .then(function(response) { + .then(function (response) { if (!response.ok) { - response.text().then(function(text) { + response.text().then(function (text) { setLog(response.url, response.status, text) }) } return response.json() }) - .catch(function(error) { + .catch(function (error) { todo(error) setLog(start, 'JSON', error) console.error(error) }) - .then(function(json) { + .then(function (json) { if (json.error) { console.error('Error:' + json.error) M.toast({ html: lang.lang_fatalerroroccured + 'Error:' + json.error, displayLength: 5000 }) @@ -298,34 +298,40 @@ function getdataAdv(domain, at) { }) } //ユーザーデータ更新 -function refresh(target, loadskip) { +async function refresh(target, loadskip) { var multi = localStorage.getItem('multi') var obj = JSON.parse(multi) if (obj[target].mode == 'misskey') { return } + let at = obj[target].at + if (obj[target].rt) { + console.log('refresh access token') + at = await refreshPleromaAt(obj[target]) + localStorage.setItem(`acct_${target}_at`, at) + } var start = 'https://' + obj[target].domain + '/api/v1/accounts/verify_credentials' fetch(start, { method: 'GET', headers: { 'content-type': 'application/json', - Authorization: 'Bearer ' + obj[target].at + Authorization: 'Bearer ' + at } }) - .then(function(response) { + .then(function (response) { if (!response.ok) { - response.text().then(function(text) { + response.text().then(function (text) { setLog(response.url, response.status, text) }) } return response.json() }) - .catch(function(error) { + .catch(function (error) { todo(error) setLog(start, 'JSON', error) console.error(error) }) - .then(function(json) { + .then(function (json) { if (json.error) { console.error('Error:' + json.error) M.toast({ html: lang.lang_fatalerroroccured + 'Error:' + json.error, displayLength: 5000 }) @@ -369,6 +375,29 @@ function refresh(target, loadskip) { } }) } +async function refreshPleromaAt(obj) { + const start = 'https://' + obj.domain + '/oauth/token' + const rt = obj.rt.split(' ') + let promise = await fetch(start, { + method: 'POST', + headers: { + 'content-type': 'application/json' + }, + body: JSON.stringify({ + grant_type : 'refresh_token', + refresh_token: rt[0], + client_id: rt[1], + client_secret: rt[2] + }) + }) + + const json = await promise.json() + if (json.access_token) { + return json.access_token + } else { + return false + } +} //MarkdownやBBCodeの対応、文字数制限をチェック //絶対ストリーミングを閉じさせないマン function ckdb(acct_id) { @@ -454,13 +483,13 @@ function ckdb(acct_id) { 'content-type': 'application/json' } }) - .then(function(response) { + .then(function (response) { return response.json() }) - .catch(function(error) { + .catch(function (error) { console.error(error) }) - .then(function(json) { + .then(function (json) { if (json.error) { console.error(json.error) return @@ -507,7 +536,7 @@ function multiSelector(parseC) { $('#src-acct-sel').html('') $('#add-acct-sel').html('') } else { - Object.keys(obj).forEach(function(key) { + Object.keys(obj).forEach(function (key) { var acct = obj[key] var list = key * 1 + 1 if (key + '' === last) { @@ -575,8 +604,8 @@ function multiSelector(parseC) { $('#src-acct-sel').append('') $('#add-acct-sel').append( '' + lang.lang_login_noauth + + '' ) $('#dir-acct-sel').append('') } @@ -605,18 +634,18 @@ function ticker() { 'content-type': 'application/json' } }) - .then(function(response) { + .then(function (response) { if (!response.ok) { - response.text().then(function(text) { + response.text().then(function (text) { setLog(response.url, response.status, text) }) } return response.json() }) - .catch(function(error) { + .catch(function (error) { console.error(error) }) - .then(function(json) { + .then(function (json) { if (json) { localStorage.removeItem('ticker') localStorage.setItem('sticker', JSON.stringify(json)) diff --git a/app/js/login/manager.js b/app/js/login/manager.js index 9f1ba7b8..ab58c2be 100644 --- a/app/js/login/manager.js +++ b/app/js/login/manager.js @@ -36,7 +36,7 @@ function load() { var domains = [] var templete $('#acct-list').html('') - Object.keys(obj).forEach(function(key) { + Object.keys(obj).forEach(function (key) { var acct = obj[key] var list = key * 1 + 1 if (acct.background != 'def' && acct.text != 'def') { @@ -55,6 +55,9 @@ function load() {
${list}. ${name}${escapeHTML(acct.user)}@${acct.domain} + + login +

-
- @@codesetupwarn@@
- -
-
+ + Account Manager - TheDesk + + + + + + + + + + + @@comment-start@@ + + + @@comment-end@@ + @@pwa@@ @@store@@ + + + + + + + + + + +
+ @@back@@
+
@@list@@
+
+
+
+ +
+
@@add@@
+
+
+
+
+ @@addAcct@@
+ +
+
+
+ @@codesetupwarn@@

-
- +
+
+
- - + -
-
@@mainacct@@
-
- @@selacct@@ -
- - -
-
    -
    - Some instance data by - instances.social API
    -
    - Administered by:
    -
    -
    -
    - @@domain@@:
    - @@connect@@:@@ko@@
    - @@toots@@:@@ko@@
    - @@users@@:@@users@@
    - @@safety@@:%
    - @@ver@@:@
    -
    + - - - - - - - - - - - +
    +
    +
    @@mainacct@@
    +
    + @@selacct@@ +
    + + +
    +
      +
      + Some instance data by + instances.social API
      +
      + Administered by:
      +
      +
      +
      + @@domain@@:
      + @@connect@@:@@ko@@
      + @@toots@@:@@ko@@
      + @@users@@:@@users@@
      + @@safety@@:%
      + @@ver@@:@
      +
      +
      + + + + + + + + + + + + \ No newline at end of file