diff --git a/back/api/system.ts b/back/api/system.ts index cf662c30..7c89bc3a 100644 --- a/back/api/system.ts +++ b/back/api/system.ts @@ -7,6 +7,7 @@ import SystemService from '../services/system'; import { celebrate, Joi } from 'celebrate'; import UserService from '../services/user'; import { t } from '../shared/i18n'; +import { isDefaultAuthInfo } from '../shared/auth'; import { getUniqPath, handleLogPath, @@ -39,14 +40,7 @@ export default (app: Router) => { const { version, changeLog, changeLogLink, publishTime } = await parseVersion(config.versionFile); - let isInitialized = true; - if ( - Object.keys(authInfo).length === 2 && - authInfo.username === 'admin' && - authInfo.password === 'admin' - ) { - isInitialized = false; - } + const isInitialized = !isDefaultAuthInfo(authInfo); res.send({ code: 200, data: { @@ -401,7 +395,10 @@ export default (app: Router) => { async (req: Request, res: Response, next: NextFunction) => { try { const userService = Container.get(UserService); - await userService.resetAuthInfo(req.body); + const result = await userService.resetAuthInfo(req.body); + if (result) { + return res.send(result); + } res.send({ code: 200, message: t('更新成功') }); } catch (e) { return next(e); diff --git a/back/loaders/express.ts b/back/loaders/express.ts index 9213b046..81e4b014 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -8,8 +8,9 @@ import { getPlatform, getToken } from '../config/util'; import rewrite from 'express-urlrewrite'; import { errors } from 'celebrate'; import { serveEnv } from '../config/serverEnv'; -import { IKeyvStore, shareStore } from '../shared/store'; -import { isValidToken } from '../shared/auth'; +import { shareStore } from '../shared/store'; +import { isValidToken, isDefaultAuthInfo } from '../shared/auth'; +import { AuthInfo } from '../data/system'; import path from 'path'; import { t } from '../shared/i18n'; import { AppScope } from '../data/open'; @@ -142,16 +143,9 @@ export default ({ app }: { app: Application }) => { return next(); } const authInfo = - (await shareStore.getAuthInfo()) || ({} as IKeyvStore['authInfo']); + (await shareStore.getAuthInfo()) || ({} as AuthInfo); - let isInitialized = true; - if ( - Object.keys(authInfo).length === 2 && - authInfo.username === 'admin' && - authInfo.password === 'admin' - ) { - isInitialized = false; - } + let isInitialized = !isDefaultAuthInfo(authInfo); if (isInitialized) { return res.send({ code: 450, message: t('未知错误') }); diff --git a/back/services/user.ts b/back/services/user.ts index 88f00a11..92a86f7b 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -517,6 +517,9 @@ export default class UserService { public async resetAuthInfo(info: Partial) { const { retries, twoFactorActivated, password, username } = info; + if (password === 'admin') { + return { code: 400, message: t('密码不能设置为admin') }; + } const authInfo = await this.getAuthInfo(); const payload = pickBy( { diff --git a/back/shared/auth.ts b/back/shared/auth.ts index 8789f5cf..5bfd694a 100644 --- a/back/shared/auth.ts +++ b/back/shared/auth.ts @@ -1,5 +1,13 @@ import { AuthInfo, TokenInfo } from '../data/system'; +export function isDefaultAuthInfo(authInfo: AuthInfo): boolean { + return ( + Object.keys(authInfo).length === 2 && + authInfo.username === 'admin' && + authInfo.password === 'admin' + ); +} + /** * Validates if a token exists in the authentication info. * Supports both legacy string tokens and new TokenInfo array format. diff --git a/src/pages/subscription/modal.tsx b/src/pages/subscription/modal.tsx index d57dffaa..a727e590 100644 --- a/src/pages/subscription/modal.tsx +++ b/src/pages/subscription/modal.tsx @@ -17,7 +17,7 @@ import isNil from 'lodash/isNil'; const { Option } = Select; const repoUrlRegx = /([^\/\:]+\/[^\/]+)(?=\.git)/; -const fileUrlRegx = /([^\/\:]+\/[^\/\.]+)\.[a-z]+$/; +const fileUrlRegx = /([^\/\:]+\/[^\/\.]+)\.[a-z]+(\?[^\s]*)?(#.*)?$/; const SubscriptionModal = ({ subscription,