Skip to content

Commit

Permalink
Add logic to monitor if a user ends up in an auth loop
Browse files Browse the repository at this point in the history
  • Loading branch information
amddg44 committed Jun 6, 2024
1 parent 7e2b0dd commit 8538b80
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions DuckDuckGo/UserAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class UserAuthenticator {
private var context = LAContext()
private var reason: String
@Published private(set) var state = AuthenticationState.loggedOut
private var authenticationCallTimestamps: [Date] = []

init(reason: String) {
self.reason = reason
Expand Down Expand Up @@ -67,17 +68,19 @@ class UserAuthenticator {

if canAuthenticate() {
let reason = reason
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { [weak self] success, error in

DispatchQueue.main.async {
if success {
self.state = .loggedIn
self?.state = .loggedIn
completion?(nil)
} else {
os_log("Failed to authenticate: %s", log: .generalLog, type: .debug, error?.localizedDescription ?? "nil error")
completion?(.failedToAuthenticate)
}
}

self?.monitorAuthenticationCalls()
}
} else {
state = .notAvailable
Expand All @@ -88,4 +91,13 @@ class UserAuthenticator {
func invalidateContext() {
context.invalidate()
}

func monitorAuthenticationCalls() {
authenticationCallTimestamps.append(Date())
authenticationCallTimestamps = authenticationCallTimestamps.filter { Date().timeIntervalSince($0) <= 5 }
if authenticationCallTimestamps.count >= 2 {
os_log("authenticate called \(authenticationCallTimestamps.count) times in the last 5 seconds")
// TODO - fire new pixel here
}
}
}

0 comments on commit 8538b80

Please sign in to comment.