From 3cf88dcbc818613ea73ac2727c42442458967868 Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Tue, 7 Mar 2017 09:50:18 +0800 Subject: [PATCH] Fix a bug about TOTP refresh --- .../PasswordDetailTableViewController.swift | 16 ++++++--------- pass/Models/Password.swift | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pass/Controllers/PasswordDetailTableViewController.swift b/pass/Controllers/PasswordDetailTableViewController.swift index 3237263..05a6f4f 100644 --- a/pass/Controllers/PasswordDetailTableViewController.swift +++ b/pass/Controllers/PasswordDetailTableViewController.swift @@ -170,20 +170,16 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni return } switch token.generator.factor { - case .counter: - // htop - break - case .timer(let period): + case .timer: // totp - let timeSinceEpoch = Date().timeIntervalSince1970 - let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period)) - strongSelf.tableData[indexPath.section].item[indexPath.row].title = "time-based (expiring in \(validTime)s)" - cell.cellData?.title = "time-based (valid within \(validTime)s)" - if validTime <= 1 || validTime >= Int(period - 1) { - let otp = token.currentPassword ?? "error" + if let (title, otp) = strongSelf.password?.getOtpStrings() { + strongSelf.tableData[indexPath.section].item[indexPath.row].title = title strongSelf.tableData[indexPath.section].item[indexPath.row].content = otp + cell.cellData?.title = title cell.cellData?.content = otp } + default: + break } } } diff --git a/pass/Models/Password.swift b/pass/Models/Password.swift index 52b74ec..5e202e8 100644 --- a/pass/Models/Password.swift +++ b/pass/Models/Password.swift @@ -239,4 +239,24 @@ class Password { } self.updatePassword(name: self.name, plainText: lines.joined(separator: "\n")) } + + // return the description and the password strings + func getOtpStrings() -> (description: String, otp: String)? { + guard let token = self.otpToken else { + return nil + } + var description : String + switch token.generator.factor { + case .counter: + // htop + description = "HMAC-based" + case .timer(let period): + // totp + let timeSinceEpoch = Date().timeIntervalSince1970 + let validTime = Int(period - timeSinceEpoch.truncatingRemainder(dividingBy: period)) + description = "time-based (expiring in \(validTime)s)" + } + let otp = self.otpToken?.currentPassword ?? "error" + return (description, otp) + } }