add Pleroma refreshable token

This commit is contained in:
cutls
2021-01-19 04:06:13 +09:00
parent eaec63adb8
commit cc5ba8af0f
6 changed files with 268 additions and 228 deletions

View File

@@ -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('<option value="tootsearch">Tootsearch</option>')
$('#add-acct-sel').html('<option value="noauth">' + lang.lang_login_noauth + '</option>')
} 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('<option value="tootsearch">Tootsearch</option>')
$('#add-acct-sel').append(
'<option value="noauth">' +
lang.lang_login_noauth +
'</option><option value="webview">Twitter</option>'
lang.lang_login_noauth +
'</option><option value="webview">Twitter</option>'
)
$('#dir-acct-sel').append('<option value="noauth">' + lang.lang_login_noauth + '</option>')
}
@@ -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))