Skip to content

Commit

Permalink
feat(MiAuth): アクセストークンの発行に失敗した場合コールバックに遷移しないようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
nafu-at committed Dec 15, 2024
1 parent a2e033d commit 8003596
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
11 changes: 10 additions & 1 deletion packages/backend/src/server/api/endpoints/miauth/gen-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IdService } from '@/core/IdService.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { DI } from '@/di-symbols.js';
import { RoleService } from '@/core/RoleService.js';
import { ApiError } from '../../error.js';

export const meta = {
tags: ['auth'],
Expand All @@ -28,6 +29,14 @@ export const meta = {
},
},
},

errors: {
tooManyAccessTokens: {
message: 'Too many access tokens',
code: 'TOO_MANY_ACCESS_TOKENS',
id: 'eb37e2f9-5475-46c3-805a-803805e81d3f',
},
},
} as const;

export const paramDef = {
Expand Down Expand Up @@ -56,7 +65,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
super(meta, paramDef, async (ps, me) => {
const currentCount = await accessTokensRepository.countBy({ userId: me.id });
if (currentCount >= (await this.roleService.getUserPolicies(me.id)).accessTokenLimit) {
throw new Error('Too many access tokens');
throw new ApiError(meta.errors.tooManyAccessTokens);
}

// Generate access token
Expand Down
20 changes: 10 additions & 10 deletions packages/frontend/src/pages/miauth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ async function onAccept(token: string) {
name: props.name,
iconUrl: props.icon,
permission: _permissions.value,
}, token).catch(() => {
}, token).then(() => {
if (props.callback && props.callback !== '') {
const cbUrl = new URL(props.callback);
if (['javascript:', 'file:', 'data:', 'mailto:', 'tel:', 'vbscript:'].includes(cbUrl.protocol)) throw new Error('invalid url');
cbUrl.searchParams.set('session', props.session);
location.href = cbUrl.toString();
} else {
authRoot.value?.showUI('success');
}
}).catch(() => {
authRoot.value?.showUI('failed');
});

if (props.callback && props.callback !== '') {
const cbUrl = new URL(props.callback);
if (['javascript:', 'file:', 'data:', 'mailto:', 'tel:', 'vbscript:'].includes(cbUrl.protocol)) throw new Error('invalid url');
cbUrl.searchParams.set('session', props.session);
location.href = cbUrl.toString();
} else {
authRoot.value?.showUI('success');
}
}

function onDeny() {
Expand Down

0 comments on commit 8003596

Please sign in to comment.