initOTPMode 初始化 OTP 认证模式
(force bool, seedTTL int, algorithm string, digits int)
| 14 | |
| 15 | // initOTPMode 初始化 OTP 认证模式 |
| 16 | func initOTPMode(force bool, seedTTL int, algorithm string, digits int) { |
| 17 | // 检查是否已存在 OTP 配置 |
| 18 | if otp.ConfigExists() && !force { |
| 19 | fmt.Println("OTP 配置已存在,使用 --force 覆盖") |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | // 提示设置密码 |
| 24 | fmt.Println("初始化 OTP 认证模式") |
| 25 | fmt.Println() |
| 26 | |
| 27 | password, err := otp.PromptPasswordWithConfirm( |
| 28 | "请设置 OTP 密码(至少12位): ", |
| 29 | "确认密码: ", |
| 30 | ) |
| 31 | if err != nil { |
| 32 | fatal(err) |
| 33 | } |
| 34 | |
| 35 | // 初始化选项 |
| 36 | opts := &otp.InitOptions{ |
| 37 | Password: password, |
| 38 | SeedUnlockTTL: seedTTL, |
| 39 | Algorithm: algorithm, |
| 40 | Digits: digits, |
| 41 | Period: 30, |
| 42 | GenerateRecovery: true, |
| 43 | } |
| 44 | |
| 45 | // 执行初始化 |
| 46 | seed, recoveryCodes, err := otp.Initialize(opts) |
| 47 | if err != nil { |
| 48 | fatal(err) |
| 49 | } |
| 50 | |
| 51 | // 生成 master key(从 OTP seed 派生) |
| 52 | masterKey, err := deriveMasterKeyFromSeed(seed, opts) |
| 53 | if err != nil { |
| 54 | fatal(err) |
| 55 | } |
| 56 | |
| 57 | // 保存 master key 到 Keychain(用于 import/export 等命令) |
| 58 | if err := keychain.StoreMasterKey(masterKey, force); err != nil { |
| 59 | fatal(err) |
| 60 | } |
| 61 | |
| 62 | // 显示结果 |
| 63 | if err := otp.DisplayInitResult(seed, recoveryCodes, algorithm, digits, 30); err != nil { |
| 64 | fatal(err) |
| 65 | } |
| 66 | |
| 67 | // 保存认证模式 |
| 68 | if err := auth.SaveMode(auth.ModeOTP); err != nil { |
| 69 | fatal(fmt.Errorf("保存认证模式失败: %w", err)) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // initTouchIDMode 初始化 Touch ID 认证模式 |
no test coverage detected