Use safer defaults for newly created service endpoints and guide operators toward suitable access credentials without disrupting existing deployments. Apply the same policy at the save boundary, retain deliberate local-only setups and external client compatibility, and add regression coverage for editing, recovery, and trusted access paths.
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { assessAccessToken } from '@snowluma/common/access-token';
|
|
|
|
export interface AccessTokenFeedback {
|
|
valid: boolean;
|
|
tone: 'success' | 'warning' | 'error';
|
|
message: string;
|
|
}
|
|
|
|
export function accessTokenFeedback(
|
|
token: string,
|
|
userInputs: readonly (string | number)[],
|
|
allowEmpty: boolean,
|
|
): AccessTokenFeedback {
|
|
if (!token) {
|
|
return allowEmpty
|
|
? {
|
|
valid: true,
|
|
tone: 'warning',
|
|
message: '留空将关闭该服务端的鉴权,仅建议在本机使用。',
|
|
}
|
|
: {
|
|
valid: false,
|
|
tone: 'error',
|
|
message: '当前为远程访问,请使用右侧按钮生成令牌后保存。',
|
|
};
|
|
}
|
|
|
|
const assessment = assessAccessToken(token, userInputs);
|
|
if (assessment.reason === 'too-short') {
|
|
return {
|
|
valid: false,
|
|
tone: 'error',
|
|
message: '令牌至少需要 16 个字符,请继续补充或重新生成。',
|
|
};
|
|
}
|
|
if (!assessment.acceptable) {
|
|
return {
|
|
valid: false,
|
|
tone: 'error',
|
|
message: '令牌容易被猜中,请使用右侧按钮生成新的随机令牌。',
|
|
};
|
|
}
|
|
return {
|
|
valid: true,
|
|
tone: 'success',
|
|
message: '令牌强度符合要求。',
|
|
};
|
|
}
|