Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token deletion #7014

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

> Users must be able to say: “I had this issue, happy to know it's fixed”

- [User] _Forget all connection tokens_ button should not delete other users' tokens, even when current user is an administrator (PR [#7014](https://github.com/vatesfr/xen-orchestra/pull/7014))

### Packages to release

> When modifying a package, add it here with its release type.
Expand All @@ -27,4 +29,7 @@

<!--packages-start-->

- xo-server minor
- xo-web patch

<!--packages-end-->
26 changes: 25 additions & 1 deletion packages/xo-server/src/api/token.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,37 @@ async function delete_({ pattern, tokens }) {

export { delete_ as delete }

delete_.description = 'delete an existing authentication token'
delete_.description = 'delete matching authentication tokens for all users'

delete_.params = {
tokens: { type: 'array', optional: true, items: { type: 'string' } },
pattern: { type: 'object', optional: true },
}

delete_.permission = 'admin'

// -------------------------------------------------------------------

export async function deleteOwn({ pattern, tokens }) {
await this.deleteAuthenticationTokens({
filter: {
__and: [
{
user_id: this.apiContext.user.id,
},
pattern ?? { id: { __or: tokens } },
],
},
})
}

deleteOwn.description = 'delete matching authentication tokens for the current user'

deleteOwn.params = {
tokens: { type: 'array', optional: true, items: { type: 'string' } },
pattern: { type: 'object', optional: true },
}

// -------------------------------------------------------------------

export async function set({ id, ...props }) {
Expand Down
8 changes: 1 addition & 7 deletions packages/xo-server/src/xo-mixins/authentication.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,8 @@ export default class {
}

async deleteAuthenticationTokens({ filter }) {
let predicate
const { apiContext } = this._app
if (apiContext !== undefined && apiContext.permission !== 'admin') {
predicate = { user_id: apiContext.user.id }
}

const db = this._tokens
return db.remove((await db.get(predicate)).filter(createPredicate(filter)).map(({ id }) => id))
await db.remove((await db.get()).filter(createPredicate(filter)).map(({ id }) => id))
}

async _getAuthenticationToken(id, properties) {
Expand Down
6 changes: 3 additions & 3 deletions packages/xo-web/src/common/xo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2973,7 +2973,7 @@ export const removeUserAuthProvider = ({ userId, authProviderId }) => {
}

const _signOutFromEverywhereElse = () =>
_call('token.delete', {
_call('token.deleteOwn', {
pattern: {
id: {
__not: cookies.get('token'),
Expand Down Expand Up @@ -3111,7 +3111,7 @@ export const deleteAuthToken = async ({ id }) => {
icon: 'user',
title: _('deleteAuthTokenConfirm'),
})
return _call('token.delete', { tokens: [id] })::tap(subscribeUserAuthTokens.forceRefresh)
return _call('token.deleteOwn', { tokens: [id] })::tap(subscribeUserAuthTokens.forceRefresh)
}

export const deleteAuthTokens = async tokens => {
Expand All @@ -3122,7 +3122,7 @@ export const deleteAuthTokens = async tokens => {
icon: 'user',
title: _('deleteAuthTokensConfirm', { nTokens: tokens.length }),
})
return _call('token.delete', { tokens: tokens.map(token => token.id) })::tap(subscribeUserAuthTokens.forceRefresh)
return _call('token.deleteOwn', { tokens: tokens.map(token => token.id) })::tap(subscribeUserAuthTokens.forceRefresh)
}

export const editAuthToken = ({ description, id }) =>
Expand Down