fix mine
This commit is contained in:
parent
17a02ea47e
commit
70e7a113e6
22 changed files with 967 additions and 285 deletions
|
@ -43,7 +43,7 @@ function verifyTokenWindow(token, timestamp, period) {
|
|||
const generateCode = async (config) => {
|
||||
try {
|
||||
const options = {
|
||||
algorithm: config.algorithm || config.algo || 'SHA1',
|
||||
algorithm: config.algorithm || 'SHA1',
|
||||
digits: Number(config.digits) || 6,
|
||||
_forceRefresh: !!config._forceRefresh,
|
||||
timestamp: config.timestamp || otp.getCurrentTimestamp()
|
||||
|
@ -94,7 +94,7 @@ const generateCode = async (config) => {
|
|||
const verifyCode = async (token, config) => {
|
||||
try {
|
||||
const options = {
|
||||
algorithm: config.algorithm || config.algo || 'SHA1',
|
||||
algorithm: config.algorithm || 'SHA1',
|
||||
digits: Number(config.digits) || 6,
|
||||
timestamp: otp.getCurrentTimestamp() // 使用统一的时间戳获取方法
|
||||
};
|
||||
|
@ -150,12 +150,21 @@ const addToken = async (tokenData) => {
|
|||
|
||||
// 生成唯一ID
|
||||
const id = `token_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
const now = formatTime(new Date());
|
||||
const token = {
|
||||
...tokenData,
|
||||
id,
|
||||
createTime: formatTime(new Date())
|
||||
createTime: now,
|
||||
lastUpdate: now,
|
||||
code: '' // 初始化为空字符串
|
||||
};
|
||||
|
||||
// 对于HOTP类型,添加counter字段
|
||||
if (tokenData.type && tokenData.type.toUpperCase() === 'HOTP') {
|
||||
token.counter = 0; // HOTP类型需要counter >= 0
|
||||
}
|
||||
// 对于TOTP类型,不设置counter字段,让它在JSON序列化时被忽略
|
||||
|
||||
// 验证是否可以生成代码
|
||||
try {
|
||||
await generateCode(token);
|
||||
|
@ -235,7 +244,26 @@ const formatDate = (date) => {
|
|||
* @returns {Promise<Array>} 同步后的令牌列表
|
||||
*/
|
||||
const syncTokens = async (tokens) => {
|
||||
return await cloud.syncTokens(tokens);
|
||||
try {
|
||||
// 上传本地令牌到云端
|
||||
await cloud.uploadTokens(tokens);
|
||||
|
||||
// 获取云端最新数据
|
||||
const cloudData = await cloud.fetchLatestTokens();
|
||||
|
||||
// 使用cloud.js中的mergeTokens函数合并本地和云端数据
|
||||
const mergedTokens = cloud.mergeTokens(tokens, cloudData.tokens, { preferCloud: true });
|
||||
|
||||
// 更新所有令牌的时间戳
|
||||
for (const token of mergedTokens) {
|
||||
token.timestamp = cloudData.timestamp;
|
||||
}
|
||||
|
||||
return mergedTokens;
|
||||
} catch (error) {
|
||||
console.error('同步令牌失败:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -243,7 +271,16 @@ const syncTokens = async (tokens) => {
|
|||
* @returns {Promise<Array>} 云端的令牌列表
|
||||
*/
|
||||
const getCloudTokens = async () => {
|
||||
return await cloud.getTokens();
|
||||
try {
|
||||
const cloudData = await cloud.fetchLatestTokens();
|
||||
return cloudData.tokens;
|
||||
} catch (error) {
|
||||
// 如果是404错误(云端无数据),不打印错误日志
|
||||
if (error.statusCode !== 404) {
|
||||
console.error('获取云端令牌失败:', error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// ============ UI相关功能 ============
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue