Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the issue of wrong OTP codes #126

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Guardian/API/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import Foundation

struct PushCredentials: Codable {
let service = "APNS"
private(set) var service = "APNS"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this being modified somewhere?

Copy link
Contributor Author

@Artelas Artelas May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It produces warning because of conforming to Codable. It is modified in the process of state restoring. There are several ways to fix the warning (like providing default value in init), but private setter is the best option here.

let token: String
}

Expand Down
2 changes: 1 addition & 1 deletion Guardian/Authentication/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public protocol Authentication {
}

public extension Authentication {
public func reject(notification: Notification, withReason reason: String? = nil) -> Request<Transaction, NoContent> {
func reject(notification: Notification, withReason reason: String? = nil) -> Request<Transaction, NoContent> {
return self.reject(notification: notification, withReason: reason)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Guardian/Crypto/Base32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ class Base32 {
}
}

return Data(bytes: UnsafePointer<UInt8>(decodedBytes), count: decodedBaseIndex)
return Data(decodedBytes.prefix(decodedBaseIndex))
}
}
4 changes: 2 additions & 2 deletions Guardian/Generators/OneTimePasswordGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public protocol TOTP {
}

public extension TOTP {
public func stringCode(time: TimeInterval = Date().timeIntervalSince1970, formatter: NumberFormatter? = nil) -> String {
func stringCode(time: TimeInterval = Date().timeIntervalSince1970, formatter: NumberFormatter? = nil) -> String {
return self.stringCode(time: time, formatter: formatter)
}

public func code() -> Int {
func code() -> Int {
return self.code(time: Date().timeIntervalSince1970)
}
}
Expand Down
4 changes: 4 additions & 0 deletions GuardianTests/Generators/OneTimePasswordGeneratorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class OneTimePasswordGeneratorSpec: QuickSpec {
it("should generate the same string and int code for the same time") {
expect(otp.stringCode(time: 49)).to(equal(otp.code(time: 49).description)) // Picked time that generates a number with 6 digits and no 0 prefix in string
}

it("should generate the same code for functions code() and code(time: Date().timeIntervalSince1970)") {
expect(otp.code(time: Date().timeIntervalSince1970)).to(equal(otp.code()))
}

it("should allow custom format for string code") {
let formatter = NumberFormatter()
Expand Down
Loading