pleroma-fe/src/stores/oauth.js

159 lines
4.8 KiB
JavaScript
Raw Normal View History

2025-03-11 18:48:55 -04:00
import { defineStore } from 'pinia'
2026-01-06 16:22:52 +02:00
import {
createApp,
getClientToken,
verifyAppToken,
} from 'src/services/new_api/oauth.js'
2025-03-09 15:06:19 -04:00
// status codes about verifyAppToken (GET /api/v1/apps/verify_credentials)
2026-01-06 16:22:52 +02:00
const isAppTokenRejected = (error) =>
2025-03-09 15:06:19 -04:00
// Pleroma API docs say it returns 422 when unauthorized
error.statusCode === 422 ||
// but it actually returns 400 (as of 2.9.0)
// NOTE: don't try to match against the error message, it is translatable
error.statusCode === 400 ||
// and Mastodon docs say it returns 401
error.statusCode === 401
// status codes about getAppToken (GET /oauth/token)
2026-01-06 16:22:52 +02:00
const isClientDataRejected = (error) =>
2025-03-09 15:06:19 -04:00
// Mastodon docs say it returns 401
error.statusCode === 401 ||
// but Pleroma actually returns 400 (as of 2.9.0)
// NOTE: don't try to match against the error message, it is translatable
error.statusCode === 400
2025-03-11 18:48:55 -04:00
export const useOAuthStore = defineStore('oauth', {
2025-03-09 15:06:19 -04:00
state: () => ({
clientId: false,
clientSecret: false,
2019-06-13 00:44:25 +03:00
/* App token is authentication for app without any user, used mostly for
* MastoAPI's registration of new users, stored so that we can fall back to
* it on logout
*/
appToken: false,
2019-06-13 00:44:25 +03:00
/* User token is authentication for app with user, this is for every calls
* that need authorized user to be successful (i.e. posting, liking etc)
*/
2026-01-06 16:22:52 +02:00
userToken: false,
2025-03-09 15:06:19 -04:00
}),
getters: {
2026-01-06 16:22:52 +02:00
getToken() {
2025-03-11 18:48:55 -04:00
return this.userToken || this.appToken
2019-06-13 10:00:06 +03:00
},
2026-01-06 16:22:52 +02:00
getUserToken() {
2025-03-11 18:48:55 -04:00
return this.userToken
2026-01-06 16:22:52 +02:00
},
2025-03-09 15:06:19 -04:00
},
actions: {
2026-01-06 16:22:52 +02:00
setClientData({ clientId, clientSecret }) {
2025-03-11 18:48:55 -04:00
this.clientId = clientId
this.clientSecret = clientSecret
},
2026-01-06 16:22:52 +02:00
setAppToken(token) {
2025-03-11 18:48:55 -04:00
this.appToken = token
},
2026-01-06 16:22:52 +02:00
setToken(token) {
2025-03-11 18:48:55 -04:00
this.userToken = token
},
2026-01-06 16:22:52 +02:00
clearToken() {
2025-03-11 18:48:55 -04:00
this.userToken = false
},
2026-01-06 16:22:52 +02:00
async createApp() {
2025-03-11 18:48:55 -04:00
const { state } = window.vuex
const instance = state.instance.server
2025-03-09 15:06:19 -04:00
const app = await createApp(instance)
2025-03-11 18:48:55 -04:00
this.setClientData(app)
2025-03-09 15:06:19 -04:00
return app
},
/// Use this if you want to get the client id and secret but are not interested
/// in whether they are valid.
/// @return {{ clientId: string, clientSecret: string }} An object representing the app
2026-01-06 16:22:52 +02:00
async ensureApp() {
2025-03-11 18:48:55 -04:00
if (this.clientId && this.clientSecret) {
2025-03-09 15:06:19 -04:00
return {
2025-03-11 18:48:55 -04:00
clientId: this.clientId,
2026-01-06 16:22:52 +02:00
clientSecret: this.clientSecret,
2025-03-09 15:06:19 -04:00
}
} else {
2025-03-11 18:48:55 -04:00
return this.createApp()
2025-03-09 15:06:19 -04:00
}
},
2026-01-06 16:22:52 +02:00
async getAppToken() {
2025-03-11 18:48:55 -04:00
const { state } = window.vuex
const instance = state.instance.server
2025-03-09 15:06:19 -04:00
const res = await getClientToken({
2025-03-11 18:48:55 -04:00
clientId: this.clientId,
clientSecret: this.clientSecret,
2026-01-06 16:22:52 +02:00
instance,
2025-03-09 15:06:19 -04:00
})
2025-03-11 18:48:55 -04:00
this.setAppToken(res.access_token)
2025-03-09 15:06:19 -04:00
return res.access_token
},
/// Use this if you want to ensure the app is still valid to use.
/// @return {string} The access token to the app (not attached to any user)
2026-01-06 16:22:52 +02:00
async ensureAppToken() {
2025-03-11 18:48:55 -04:00
const { state } = window.vuex
if (this.appToken) {
2025-03-09 15:06:19 -04:00
try {
await verifyAppToken({
2025-03-11 18:48:55 -04:00
instance: state.instance.server,
2026-01-06 16:22:52 +02:00
appToken: this.appToken,
2025-03-09 15:06:19 -04:00
})
2025-03-11 18:48:55 -04:00
return this.appToken
2025-03-09 15:06:19 -04:00
} catch (e) {
if (!isAppTokenRejected(e)) {
// The server did not reject our token, but we encountered other problems. Maybe the server is down.
throw e
} else {
// The app token is rejected, so it is no longer useful.
2025-03-11 18:48:55 -04:00
this.setAppToken(false)
2025-03-09 15:06:19 -04:00
}
}
}
// appToken is not available, or is rejected: try to get a new one.
// First, make sure the client id and client secret are filled.
try {
2025-03-11 18:48:55 -04:00
await this.ensureApp()
2025-03-09 15:06:19 -04:00
} catch (e) {
console.error('Cannot create app', e)
throw e
}
// Note that at this step, the client id and secret may be invalid
// (because the backend may have already deleted the app due to no user login)
try {
2025-03-11 18:48:55 -04:00
return await this.getAppToken()
2025-03-09 15:06:19 -04:00
} catch (e) {
if (!isClientDataRejected(e)) {
// Non-credentials problem, fail fast
console.error('Cannot get app token', e)
throw e
} else {
// the client id and secret are invalid, so we should clear them
// and re-create our app
2025-03-11 18:48:55 -04:00
this.setClientData({
2025-03-09 15:06:19 -04:00
clientId: false,
2026-01-06 16:22:52 +02:00
clientSecret: false,
2025-03-09 15:06:19 -04:00
})
2025-03-11 18:48:55 -04:00
await this.createApp()
2025-03-09 15:06:19 -04:00
// try once again to get the token
2025-03-11 18:48:55 -04:00
return await this.getAppToken()
2025-03-09 15:06:19 -04:00
}
}
2026-01-06 16:22:52 +02:00
},
2025-03-11 18:48:55 -04:00
},
persist: {
2026-01-06 16:22:52 +02:00
afterLoad(state) {
2025-03-11 18:48:55 -04:00
// state.token is userToken with older name, coming from persistent state
if (state.token && !state.userToken) {
state.userToken = state.token
}
if ('token' in state) {
delete state.token
}
return state
2026-01-06 16:22:52 +02:00
},
},
2025-03-11 18:48:55 -04:00
})