pinafore/routes/_api/oauth.js

36 lines
1 KiB
JavaScript
Raw Normal View History

2018-01-13 22:19:51 +00:00
const WEBSITE = 'https://pinafore.social'
const SCOPES = 'read write follow'
const CLIENT_NAME = 'Pinafore'
2018-02-08 16:22:14 +00:00
import { post, get, paramsString } from '../_utils/ajax'
import { basename } from './utils'
2018-01-13 22:19:51 +00:00
2018-01-14 03:23:05 +00:00
export function registerApplication(instanceName, redirectUri) {
const url = `${basename(instanceName)}/api/v1/apps`
2018-01-13 22:19:51 +00:00
return post(url, {
client_name: CLIENT_NAME,
2018-01-14 03:23:05 +00:00
redirect_uris: redirectUri,
2018-01-13 22:19:51 +00:00
scopes: SCOPES,
website: WEBSITE
})
}
2018-01-14 03:23:05 +00:00
export function generateAuthLink(instanceName, clientId, redirectUri) {
2018-01-13 22:19:51 +00:00
let params = paramsString({
'client_id': clientId,
2018-01-14 03:23:05 +00:00
'redirect_uri': redirectUri,
2018-01-13 22:19:51 +00:00
'response_type': 'code',
'scope': SCOPES
})
return `${basename(instanceName)}/oauth/authorize?${params}`
2018-01-13 22:19:51 +00:00
}
2018-01-14 03:23:05 +00:00
export function getAccessTokenFromAuthCode(instanceName, clientId, clientSecret, code, redirectUri) {
let url = `${basename(instanceName)}/oauth/token`
2018-01-13 22:19:51 +00:00
return post(url, {
client_id: clientId,
client_secret: clientSecret,
2018-01-14 03:23:05 +00:00
redirect_uri: redirectUri,
2018-01-13 22:19:51 +00:00
grant_type: 'authorization_code',
code: code
})
}