mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
@ -10,14 +10,15 @@ import { createBackend } from '@/utils';
|
||||
import { JWTPayloadSchema } from './scheme';
|
||||
import { registerAuth, registerControl } from './routes';
|
||||
|
||||
import type { APIServerConfig } from '../config';
|
||||
import { type APIServerConfig, AuthStrategy } from '../config';
|
||||
|
||||
import type { BackendType } from './types';
|
||||
|
||||
export const backend = createBackend<BackendType, APIServerConfig>({
|
||||
async start(ctx) {
|
||||
const config = await ctx.getConfig();
|
||||
|
||||
this.init(ctx);
|
||||
await this.init(ctx);
|
||||
registerCallback((songInfo) => {
|
||||
this.songInfo = songInfo;
|
||||
});
|
||||
@ -49,17 +50,20 @@ export const backend = createBackend<BackendType, APIServerConfig>({
|
||||
this.app.use('*', cors());
|
||||
|
||||
// middlewares
|
||||
this.app.use(
|
||||
'/api/*',
|
||||
jwt({
|
||||
secret: config.secret,
|
||||
}),
|
||||
);
|
||||
this.app.use('/api/*', async (ctx, next) => {
|
||||
if (config.authStrategy !== AuthStrategy.NONE) {
|
||||
return await jwt({
|
||||
secret: config.secret,
|
||||
})(ctx, next);
|
||||
}
|
||||
await next();
|
||||
});
|
||||
this.app.use('/api/*', async (ctx, next) => {
|
||||
const result = await JWTPayloadSchema.spa(await ctx.get('jwtPayload'));
|
||||
|
||||
const isAuthorized =
|
||||
result.success && config.authorizedClients.includes(result.data.id);
|
||||
config.authStrategy === AuthStrategy.NONE ||
|
||||
(result.success && config.authorizedClients.includes(result.data.id));
|
||||
if (!isAuthorized) {
|
||||
ctx.status(401);
|
||||
return ctx.body('Unauthorized');
|
||||
|
||||
@ -6,9 +6,9 @@ import { getConnInfo } from '@hono/node-server/conninfo';
|
||||
|
||||
import { t } from '@/i18n';
|
||||
|
||||
import { APIServerConfig } from '../../config';
|
||||
import { JWTPayload } from '../scheme';
|
||||
import { type APIServerConfig, AuthStrategy } from '../../config';
|
||||
|
||||
import type { JWTPayload } from '../scheme';
|
||||
import type { HonoApp } from '../types';
|
||||
import type { BackendContext } from '@/types/contexts';
|
||||
|
||||
@ -52,7 +52,7 @@ export const register = (
|
||||
|
||||
if (config.authorizedClients.includes(id)) {
|
||||
// SKIP CHECK
|
||||
} else if (config.authStrategy === 'AUTH_AT_FIRST') {
|
||||
} else if (config.authStrategy === AuthStrategy.AUTH_AT_FIRST) {
|
||||
const result = await dialog.showMessageBox({
|
||||
title: t('plugins.api-server.dialog.request.title'),
|
||||
message: t('plugins.api-server.dialog.request.message', {
|
||||
@ -71,7 +71,7 @@ export const register = (
|
||||
ctx.status(403);
|
||||
return ctx.body(null);
|
||||
}
|
||||
} else if (config.authStrategy === 'NONE') {
|
||||
} else if (config.authStrategy === AuthStrategy.NONE) {
|
||||
// SKIP CHECK
|
||||
}
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@ export type BackendType = {
|
||||
oldConfig?: APIServerConfig;
|
||||
songInfo?: SongInfo;
|
||||
|
||||
init: (ctx: BackendContext<APIServerConfig>) => void;
|
||||
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
|
||||
run: (hostname: string, port: number) => void;
|
||||
end: () => void;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user