Skip to content

Ensure that most SDK calls don't result in hanging delegates #12

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

Merged
merged 3 commits into from
May 22, 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
11 changes: 10 additions & 1 deletion Sources/SnapAuth/Errors.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// FIXME: Go through and ensure errors are complete and accurate.
/* FIXME: Go through and ensure errors are complete and accurate.
public enum AuthenticationError: Error {
/// The user canceled
case canceled
Expand All @@ -7,6 +7,7 @@ public enum AuthenticationError: Error {

case asAuthorizationError
}
*/


public enum SnapAuthError: Error {
Expand All @@ -21,4 +22,12 @@ public enum SnapAuthError: Error {

/// The request was valid and understood, but processing was refused.
case badRequest

// Duplicated (ish) from ASAuthorizationError
case unknown
// case canceled
// case invalidResponse
// case notHandled
// case failed
// case notInteractive
}
13 changes: 7 additions & 6 deletions Sources/SnapAuth/SnapAuth+ASACD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ extension SnapAuth: ASAuthorizationControllerDelegate {

Task {
if (state == .authenticating) {
await delegate?.snapAuth(didFinishAuthentication: .failure(.asAuthorizationError))
await delegate?.snapAuth(didFinishAuthentication: .failure(.unknown))
} else if (state == .registering) {
await delegate?.snapAuth(didFinishRegistration: .failure(.asAuthorizationError))
await delegate?.snapAuth(didFinishRegistration: .failure(.unknown))
} else if (state == .autofill) {
// Intentional no-op
}
Expand Down Expand Up @@ -97,6 +97,7 @@ extension SnapAuth: ASAuthorizationControllerDelegate {
}
*/
guard registration.rawAttestationObject != nil else {
// TODO: what should be done here?
logger.error("No attestation")
return
}
Expand All @@ -117,8 +118,8 @@ extension SnapAuth: ASAuthorizationControllerDelegate {
body: body,
type: SAProcessAuthResponse.self)
guard case let .success(processAuth) = response else {
logger.debug("no/invalid process response")
// TODO: bubble this up via delegate failure (network error?)
logger.debug("/registration/process error")
await delegate?.snapAuth(didFinishRegistration: .failure(response.getError()!))
return
}
logger.debug("got token response")
Expand Down Expand Up @@ -163,8 +164,8 @@ extension SnapAuth: ASAuthorizationControllerDelegate {
body: body,
type: SAProcessAuthResponse.self)
guard case let .success(authResponse) = response else {
logger.debug("no/invalid process response")
// TODO: bubble this up via delegate failure (network error?)
logger.debug("/auth/process error")
await delegate?.snapAuth(didFinishAuthentication: .failure(response.getError()!))
return
}
logger.debug("got token response")
Expand Down
18 changes: 16 additions & 2 deletions Sources/SnapAuth/SnapAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public class SnapAuth: NSObject { // NSObject for ASAuthorizationControllerDeleg
type: SACreateRegisterOptionsResponse.self)

guard case let .success(options) = response else {
// TODO: bubble error
let error = response.getError()!
await delegate?.snapAuth(didFinishRegistration: .failure(error))
return
}

Expand Down Expand Up @@ -166,7 +167,8 @@ public class SnapAuth: NSObject { // NSObject for ASAuthorizationControllerDeleg


guard case let .success(options) = response else {
// TODO: bubble error
let error = response.getError()!
await delegate?.snapAuth(didFinishAuthentication: .failure(error))
return
}

Expand Down Expand Up @@ -228,3 +230,15 @@ extension AuthenticatingUser: Encodable {
}
}
}

/// Small addition to the native Result type to more easily extract error details.
extension Result {
func getError() -> Failure? {
switch self {
case .success:
return nil
case .failure(let failure):
return failure
}
}
}
2 changes: 1 addition & 1 deletion Sources/SnapAuth/SnapAuthDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public struct SnapAuthTokenInfo {
public let expiresAt: Date
}

public typealias SnapAuthResult = Result<SnapAuthTokenInfo, AuthenticationError>
public typealias SnapAuthResult = Result<SnapAuthTokenInfo, SnapAuthError>