pinafore/routes/_utils/mastodon/oauth.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-01-13 23:19:51 +01:00
const WEBSITE = 'https://pinafore.social'
const SCOPES = 'read write follow'
const CLIENT_NAME = 'Pinafore'
import { post, get, paramsString } from '../ajax'
2018-01-14 04:23:05 +01:00
export function registerApplication(instanceName, redirectUri) {
2018-01-13 23:19:51 +01:00
const url = `https://${instanceName}/api/v1/apps`
return post(url, {
client_name: CLIENT_NAME,
2018-01-14 04:23:05 +01:00
redirect_uris: redirectUri,
2018-01-13 23:19:51 +01:00
scopes: SCOPES,
website: WEBSITE
})
}
2018-01-14 04:23:05 +01:00
export function generateAuthLink(instanceName, clientId, redirectUri) {
2018-01-13 23:19:51 +01:00
let params = paramsString({
'client_id': clientId,
2018-01-14 04:23:05 +01:00
'redirect_uri': redirectUri,
2018-01-13 23:19:51 +01:00
'response_type': 'code',
'scope': SCOPES
})
return `https://${instanceName}/oauth/authorize?${params}`
}
2018-01-14 04:23:05 +01:00
export function getAccessTokenFromAuthCode(instanceName, clientId, clientSecret, code, redirectUri) {
2018-01-13 23:19:51 +01:00
let url = `https://${instanceName}/oauth/token`
return post(url, {
client_id: clientId,
client_secret: clientSecret,
2018-01-14 04:23:05 +01:00
redirect_uri: redirectUri,
2018-01-13 23:19:51 +01:00
grant_type: 'authorization_code',
code: code
})
}
2018-01-18 03:35:27 +01:00
export function getHomeTimeline(instanceName, accessToken, since, limit) {
2018-01-13 23:19:51 +01:00
let url = `https://${instanceName}/api/v1/timelines/home`
2018-01-18 03:35:27 +01:00
let params = {}
if (since) {
params[since] = since
}
if (limit) {
params[limit] = limit
}
url += '?' + paramsString(params)
2018-01-13 23:19:51 +01:00
return get(url, {
'Authorization': `Bearer ${accessToken}`
})
}