pleroma-fe/src/services/new_api/oauth.js

197 lines
4.6 KiB
JavaScript
Raw Normal View History

import { reduce } from 'lodash'
2025-03-09 15:06:19 -04:00
import { StatusCodeError } from 'src/services/errors/errors.js'
const REDIRECT_URI = `${window.location.origin}/oauth-callback`
2025-03-09 15:06:19 -04:00
export const getJsonOrError = async (response) => {
if (response.ok) {
2026-01-06 16:22:52 +02:00
return response.json().catch((error) => {
throw new StatusCodeError(response.status, error, {}, response)
})
2025-03-09 15:06:19 -04:00
} else {
2026-01-06 16:22:52 +02:00
throw new StatusCodeError(
response.status,
await response.text(),
{},
response,
)
}
2025-03-09 15:06:19 -04:00
}
2018-10-26 15:16:23 +02:00
2025-03-09 15:06:19 -04:00
export const createApp = (instance) => {
2018-10-26 15:16:23 +02:00
const url = `${instance}/api/v1/apps`
const form = new window.FormData()
form.append('client_name', 'PleromaFE')
form.append('website', 'https://pleroma.social')
form.append('redirect_uris', REDIRECT_URI)
form.append('scopes', 'read write follow push admin')
2018-10-26 15:16:23 +02:00
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
2025-03-09 15:06:19 -04:00
.then(getJsonOrError)
2026-01-06 16:22:52 +02:00
.then((app) => ({
clientId: app.client_id,
clientSecret: app.client_secret,
}))
2025-03-09 15:06:19 -04:00
}
export const verifyAppToken = ({ instance, appToken }) => {
2026-01-06 16:22:52 +02:00
return window
.fetch(`${instance}/api/v1/apps/verify_credentials`, {
method: 'GET',
headers: { Authorization: `Bearer ${appToken}` },
})
2025-03-09 15:06:19 -04:00
.then(getJsonOrError)
2018-10-26 15:16:23 +02:00
}
const login = ({ instance, clientId }) => {
const data = {
response_type: 'code',
client_id: clientId,
redirect_uri: REDIRECT_URI,
2026-01-06 16:22:52 +02:00
scope: 'read write follow push admin',
}
2018-10-26 15:16:23 +02:00
2026-01-06 16:22:52 +02:00
const dataString = reduce(
data,
(acc, v, k) => {
const encoded = `${k}=${encodeURIComponent(v)}`
if (!acc) {
return encoded
} else {
return `${acc}&${encoded}`
}
},
false,
)
2018-10-26 15:16:23 +02:00
// Do the redirect...
const url = `${instance}/oauth/authorize?${dataString}`
window.location.href = url
2018-10-26 15:16:23 +02:00
}
2026-01-06 16:22:52 +02:00
const getTokenWithCredentials = ({
clientId,
clientSecret,
instance,
username,
password,
}) => {
const url = `${instance}/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)
form.append('client_secret', clientSecret)
form.append('grant_type', 'password')
form.append('username', username)
form.append('password', password)
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then((data) => data.json())
}
const getToken = ({ clientId, clientSecret, instance, code }) => {
2018-10-26 15:16:23 +02:00
const url = `${instance}/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)
form.append('client_secret', clientSecret)
2018-10-26 15:16:23 +02:00
form.append('grant_type', 'authorization_code')
form.append('code', code)
form.append('redirect_uri', `${window.location.origin}/oauth-callback`)
2018-10-26 15:16:23 +02:00
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then((data) => data.json())
}
export const getClientToken = ({ clientId, clientSecret, instance }) => {
const url = `${instance}/oauth/token`
const form = new window.FormData()
form.append('client_id', clientId)
form.append('client_secret', clientSecret)
form.append('grant_type', 'client_credentials')
form.append('redirect_uri', `${window.location.origin}/oauth-callback`)
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then(getJsonOrError)
2018-10-26 15:16:23 +02:00
}
2019-07-05 10:02:14 +03:00
const verifyOTPCode = ({ app, instance, mfaToken, code }) => {
const url = `${instance}/oauth/mfa/challenge`
const form = new window.FormData()
form.append('client_id', app.client_id)
form.append('client_secret', app.client_secret)
form.append('mfa_token', mfaToken)
form.append('code', code)
form.append('challenge_type', 'totp')
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then((data) => data.json())
}
2019-07-05 10:02:14 +03:00
const verifyRecoveryCode = ({ app, instance, mfaToken, code }) => {
const url = `${instance}/oauth/mfa/challenge`
const form = new window.FormData()
form.append('client_id', app.client_id)
form.append('client_secret', app.client_secret)
form.append('mfa_token', mfaToken)
form.append('code', code)
form.append('challenge_type', 'recovery')
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then((data) => data.json())
}
2018-10-26 15:16:23 +02:00
2019-07-02 15:33:40 +07:00
const revokeToken = ({ app, instance, token }) => {
const url = `${instance}/oauth/revoke`
const form = new window.FormData()
form.append('client_id', app.clientId)
form.append('client_secret', app.clientSecret)
form.append('token', token)
2026-01-06 16:22:52 +02:00
return window
.fetch(url, {
method: 'POST',
body: form,
})
.then((data) => data.json())
2019-07-02 15:33:40 +07:00
}
2018-10-26 15:16:23 +02:00
const oauth = {
login,
getToken,
getTokenWithCredentials,
verifyOTPCode,
2019-07-02 15:33:40 +07:00
verifyRecoveryCode,
2026-01-06 16:22:52 +02:00
revokeToken,
2018-10-26 15:16:23 +02:00
}
export default oauth