fix mine
This commit is contained in:
parent
17a02ea47e
commit
70e7a113e6
22 changed files with 967 additions and 285 deletions
|
@ -40,42 +40,73 @@ const parseURL = (url) => {
|
|||
return null;
|
||||
}
|
||||
|
||||
// 解析URL
|
||||
const urlObj = new URL(url);
|
||||
const type = urlObj.hostname.toLowerCase();
|
||||
const pathParts = urlObj.pathname.substring(1).split(':');
|
||||
const issuer = decodeURIComponent(pathParts[0]);
|
||||
const account = pathParts.length > 1 ? decodeURIComponent(pathParts[1]) : '';
|
||||
// 解析类型和路径
|
||||
const protocolAndRest = url.split('://');
|
||||
if (protocolAndRest.length !== 2) return null;
|
||||
|
||||
const typeAndRest = protocolAndRest[1].split('/', 2);
|
||||
if (typeAndRest.length !== 2) return null;
|
||||
|
||||
const type = typeAndRest[0].toLowerCase();
|
||||
|
||||
// 分离路径和查询参数
|
||||
const pathAndQuery = typeAndRest[1].split('?');
|
||||
if (pathAndQuery.length !== 2) return null;
|
||||
|
||||
// 解析路径部分(issuer和account)
|
||||
const pathParts = decodeURIComponent(pathAndQuery[0]).split(':');
|
||||
let issuer = pathParts[0];
|
||||
const account = pathParts.length > 1 ? pathParts[1] : '';
|
||||
|
||||
// 解析查询参数
|
||||
const params = {};
|
||||
urlObj.searchParams.forEach((value, key) => {
|
||||
params[key.toLowerCase()] = decodeURIComponent(value);
|
||||
});
|
||||
|
||||
// 构建令牌数据
|
||||
const tokenData = {
|
||||
type: type,
|
||||
issuer: issuer,
|
||||
remark: account,
|
||||
secret: params.secret || '',
|
||||
algo: (params.algorithm || 'SHA1').toUpperCase(),
|
||||
digits: parseInt(params.digits || '6', 10)
|
||||
};
|
||||
|
||||
// 类型特定参数
|
||||
if (type === 'totp') {
|
||||
tokenData.period = parseInt(params.period || '30', 10);
|
||||
} else if (type === 'hotp') {
|
||||
tokenData.counter = parseInt(params.counter || '0', 10);
|
||||
const queryParts = pathAndQuery[1].split('&');
|
||||
for (const part of queryParts) {
|
||||
const [key, value] = part.split('=');
|
||||
if (key && value) {
|
||||
params[key.toLowerCase()] = decodeURIComponent(value);
|
||||
}
|
||||
}
|
||||
|
||||
return tokenData;
|
||||
// 如果params中有issuer参数,优先使用它
|
||||
if (params.issuer) {
|
||||
issuer = params.issuer;
|
||||
}
|
||||
|
||||
// 验证必要参数
|
||||
if (!params.secret) {
|
||||
throw new Error('缺少必要参数: secret');
|
||||
}
|
||||
|
||||
// 验证并规范化参数
|
||||
const digits = parseInt(params.digits) || 6;
|
||||
if (digits < 6 || digits > 8) {
|
||||
throw new Error('验证码位数必须在6-8之间');
|
||||
}
|
||||
|
||||
const period = parseInt(params.period) || (type === 'totp' ? 30 : undefined);
|
||||
if (type === 'totp' && (period < 15 || period > 60)) {
|
||||
throw new Error('TOTP周期必须在15-60秒之间');
|
||||
}
|
||||
|
||||
const counter = parseInt(params.counter) || (type === 'hotp' ? 0 : undefined);
|
||||
if (type === 'hotp' && counter < 0) {
|
||||
throw new Error('HOTP计数器不能为负数');
|
||||
}
|
||||
|
||||
// 构建返回对象
|
||||
return {
|
||||
type,
|
||||
issuer,
|
||||
account,
|
||||
secret: params.secret.toUpperCase(), // 统一转换为大写
|
||||
algorithm: (params.algorithm || 'SHA1').toUpperCase(),
|
||||
digits,
|
||||
period,
|
||||
counter
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[Error] Failed to parse OTP URL', {
|
||||
error: error.message,
|
||||
url: url.length > 100 ? url.substring(0, 100) + '...' : url
|
||||
});
|
||||
console.error('解析OTP URL失败:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
@ -97,9 +128,9 @@ const validateToken = (tokenData) => {
|
|||
}
|
||||
|
||||
// 验证算法
|
||||
const validAlgos = ['SHA1', 'SHA256', 'SHA512'];
|
||||
if (!validAlgos.includes(tokenData.algo)) {
|
||||
errors.push(`不支持的算法: ${tokenData.algo}`);
|
||||
const validAlgorithms = ['SHA1', 'SHA256', 'SHA512'];
|
||||
if (!validAlgorithms.includes(tokenData.algorithm)) {
|
||||
errors.push(`不支持的算法: ${tokenData.algorithm}`);
|
||||
}
|
||||
|
||||
// 验证位数
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue