semaphore/src/routes/_api/oauth.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

import { post, paramsString, WRITE_TIMEOUT } from '../_utils/ajax.js'
import { basename } from './utils.js'
2018-02-09 06:29:29 +00:00
2023-01-10 14:21:38 +00:00
const WEBSITE = 'https://semaphore.social'
const SCOPES = 'read write follow push'
2023-01-10 14:21:38 +00:00
const CLIENT_NAME = 'Semaphore'
2018-01-13 22:19:51 +00:00
2018-02-09 06:29:29 +00:00
export function registerApplication (instanceName, redirectUri) {
const url = `${basename(instanceName)}/api/v1/apps`
return post(url, {
2018-02-09 06:29:29 +00:00
client_name: CLIENT_NAME,
redirect_uris: redirectUri,
scopes: SCOPES,
website: WEBSITE
}, null, { timeout: WRITE_TIMEOUT })
2018-01-13 22:19:51 +00:00
}
2018-02-09 06:29:29 +00:00
export function generateAuthLink (instanceName, clientId, redirectUri) {
2019-08-03 21:49:37 +01:00
const params = paramsString({
client_id: clientId,
redirect_uri: redirectUri,
response_type: 'code',
scope: SCOPES
2018-01-13 22:19:51 +00:00
})
return `${basename(instanceName)}/oauth/authorize?${params}`
2018-01-13 22:19:51 +00:00
}
2018-02-09 06:29:29 +00:00
export function getAccessTokenFromAuthCode (instanceName, clientId, clientSecret, code, redirectUri) {
2019-08-03 21:49:37 +01:00
const url = `${basename(instanceName)}/oauth/token`
// Using URLSearchParams here guarantees a content type of application/x-www-form-urlencoded
// See https://fetch.spec.whatwg.org/#bodyinit-unions
return post(url, new URLSearchParams({
2018-01-13 22:19:51 +00:00
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',
2022-11-18 17:32:31 +00:00
code
}), null, { timeout: WRITE_TIMEOUT })
2018-02-09 06:29:29 +00:00
}