Release NapCat / validate-version (push) Canceled after 0s
Release NapCat / Build-Framework (push) Canceled after 0s
Release NapCat / Build-Shell (push) Canceled after 0s
Release NapCat / Download-QNX64 (push) Canceled after 0s
Release NapCat / release-napcat (push) Canceled after 0s
* fix(rpc): clean callbacks when sending failed * chore: format * fix(pty): handle ws message properly * fix(webui-backend): unify credential timestamp validation Store credential CreatedTime in seconds and validate expiry using the same unit. Keep backward compatibility for legacy millisecond timestamps. * chore: format * Revert "chore: format" This reverts commit 6f2e9f1b53b55e8f4bf4a747ed7b05a0d6e4dbb4. * fix: naming Rename ONE_HOUR_SECONDS to MAX_CREDENTIAL_VALID_SECONDS for clearer intent. Simplify CreatedTime parsing by using Math.floor(Number(...)) and remove the legacy millisecond-to-second conversion branch. Update the time-difference check to use the new constant name.
115 lines
4.2 KiB
TypeScript
115 lines
4.2 KiB
TypeScript
import crypto from 'crypto';
|
|
import store from 'napcat-common/src/store';
|
|
import type { WebUiCredentialJson, WebUiCredentialInnerJson } from '@/napcat-webui-backend/src/types';
|
|
export class AuthHelper {
|
|
private static readonly secretKey = process.env['NAPCAT_WEBUI_JWT_SECRET_KEY'] || Math.random().toString(36).slice(2);
|
|
private static readonly MAX_CREDENTIAL_VALID_SECONDS = 3600;
|
|
|
|
public static getSecretKey (): string {
|
|
return AuthHelper.secretKey;
|
|
}
|
|
|
|
/**
|
|
* 签名凭证方法。
|
|
* @param hash 待签名的凭证字符串。
|
|
* @returns 签名后的凭证对象。
|
|
*/
|
|
public static signCredential (hash: string): WebUiCredentialJson {
|
|
const innerJson: WebUiCredentialInnerJson = {
|
|
// 统一使用秒级时间戳,避免与校验逻辑出现单位不一致
|
|
CreatedTime: Math.floor(Date.now() / 1000),
|
|
HashEncoded: hash,
|
|
};
|
|
const jsonString = JSON.stringify(innerJson);
|
|
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
|
return { Data: innerJson, Hmac: hmac };
|
|
}
|
|
|
|
/**
|
|
* 检查凭证是否被篡改的方法。
|
|
* @param credentialJson 凭证的JSON对象。
|
|
* @returns 布尔值,表示凭证是否有效。
|
|
*/
|
|
public static checkCredential (credentialJson: WebUiCredentialJson): boolean {
|
|
try {
|
|
const jsonString = JSON.stringify(credentialJson.Data);
|
|
const calculatedHmac = crypto
|
|
.createHmac('sha256', AuthHelper.secretKey)
|
|
.update(jsonString, 'utf8')
|
|
.digest('hex');
|
|
return calculatedHmac === credentialJson.Hmac;
|
|
} catch (_error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证凭证在1小时内有效且token与原始token相同。
|
|
* @param token 待验证的原始token。
|
|
* @param credentialJson 已签名的凭证JSON对象。
|
|
* @returns 布尔值,表示凭证是否有效且token匹配。
|
|
*/
|
|
public static validateCredentialWithinOneHour (token: string, credentialJson: WebUiCredentialJson): boolean {
|
|
// 首先检查凭证是否被篡改
|
|
const isValid = AuthHelper.checkCredential(credentialJson);
|
|
if (!isValid) {
|
|
return false;
|
|
}
|
|
|
|
// 检查凭证是否在黑名单中
|
|
if (AuthHelper.isCredentialRevoked(credentialJson)) {
|
|
return false;
|
|
}
|
|
|
|
const currentTimeSeconds = Math.floor(Date.now() / 1000);
|
|
const createdTimeSeconds = Math.floor(Number(credentialJson.Data.CreatedTime));
|
|
const timeDifferenceSeconds = currentTimeSeconds - createdTimeSeconds;
|
|
const isWithinOneHour = timeDifferenceSeconds >= 0 && timeDifferenceSeconds <= AuthHelper.MAX_CREDENTIAL_VALID_SECONDS;
|
|
return isWithinOneHour && credentialJson.Data.HashEncoded === AuthHelper.generatePasswordHash(token);
|
|
}
|
|
|
|
/**
|
|
* 注销指定的Token凭证
|
|
* @param credentialJson 凭证JSON对象
|
|
* @returns void
|
|
*/
|
|
public static revokeCredential (credentialJson: WebUiCredentialJson): void {
|
|
const jsonString = JSON.stringify(credentialJson.Data);
|
|
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
|
|
|
// 将已注销的凭证添加到黑名单中,有效期1小时
|
|
store.set(`revoked:${hmac}`, true, 3600);
|
|
}
|
|
|
|
/**
|
|
* 检查凭证是否已被注销
|
|
* @param credentialJson 凭证JSON对象
|
|
* @returns 布尔值,表示凭证是否已被注销
|
|
*/
|
|
public static isCredentialRevoked (credentialJson: WebUiCredentialJson): boolean {
|
|
const jsonString = JSON.stringify(credentialJson.Data);
|
|
const hmac = crypto.createHmac('sha256', AuthHelper.secretKey).update(jsonString, 'utf8').digest('hex');
|
|
|
|
return store.exists(`revoked:${hmac}`) > 0;
|
|
}
|
|
|
|
/**
|
|
* 生成密码Hash
|
|
* @param password 密码
|
|
* @returns 生成的Hash值
|
|
*/
|
|
public static generatePasswordHash (password: string): string {
|
|
return crypto.createHash('sha256').update(password + '.napcat').digest().toString('hex');
|
|
}
|
|
|
|
/**
|
|
* 对比密码和Hash值
|
|
* @param password 密码
|
|
* @param hash Hash值
|
|
* @returns 布尔值,表示密码是否匹配Hash值
|
|
*/
|
|
public static comparePasswordHash (password: string, hash: string): boolean {
|
|
return this.generatePasswordHash(password) === hash;
|
|
}
|
|
}
|