2021-07-05 05:19:04 +02:00
|
|
|
import { post, paramsString, WRITE_TIMEOUT } from '../_utils/ajax.js'
|
|
|
|
import { basename } from './utils.js'
|
2018-02-09 07:29:29 +01:00
|
|
|
|
2018-01-13 23:19:51 +01:00
|
|
|
const WEBSITE = 'https://pinafore.social'
|
2018-10-06 22:06:10 +02:00
|
|
|
const SCOPES = 'read write follow push'
|
2018-01-13 23:19:51 +01:00
|
|
|
const CLIENT_NAME = 'Pinafore'
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export function registerApplication (instanceName, redirectUri) {
|
2018-01-19 06:52:58 +01:00
|
|
|
const url = `${basename(instanceName)}/api/v1/apps`
|
2018-06-13 16:38:36 +02:00
|
|
|
return post(url, {
|
2018-02-09 07:29:29 +01:00
|
|
|
client_name: CLIENT_NAME,
|
|
|
|
redirect_uris: redirectUri,
|
|
|
|
scopes: SCOPES,
|
|
|
|
website: WEBSITE
|
2018-08-30 06:42:57 +02:00
|
|
|
}, null, { timeout: WRITE_TIMEOUT })
|
2018-01-13 23:19:51 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export function generateAuthLink (instanceName, clientId, redirectUri) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const params = paramsString({
|
|
|
|
client_id: clientId,
|
|
|
|
redirect_uri: redirectUri,
|
|
|
|
response_type: 'code',
|
|
|
|
scope: SCOPES
|
2018-01-13 23:19:51 +01:00
|
|
|
})
|
2018-01-19 06:52:58 +01:00
|
|
|
return `${basename(instanceName)}/oauth/authorize?${params}`
|
2018-01-13 23:19:51 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export function getAccessTokenFromAuthCode (instanceName, clientId, clientSecret, code, redirectUri) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const url = `${basename(instanceName)}/oauth/token`
|
2018-06-13 16:38:36 +02:00
|
|
|
return post(url, {
|
2018-01-13 23:19:51 +01:00
|
|
|
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',
|
2022-11-18 18:32:31 +01:00
|
|
|
code
|
2018-08-30 06:42:57 +02:00
|
|
|
}, null, { timeout: WRITE_TIMEOUT })
|
2018-02-09 07:29:29 +01:00
|
|
|
}
|