-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45bddcd
commit e508e04
Showing
3 changed files
with
78 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/Support/APIError+Unauthorized.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import AppSyncRealTimeClient | ||
|
||
extension APIError { | ||
|
||
static let UnauthorizedMessageString: String = "Unauthorized" | ||
|
||
/// By default, this consolidates unauthorized error scenarios coming from `APIError.httpStatusError` and | ||
/// `APIError.operationError`. For `.httpStatusError`, this checks if the status code is 401 or 403. For | ||
/// `.operationError`, this checks if the error description contains "Unauthorized". | ||
/// | ||
/// **Warning** Customized server responses that indicate unauthorized may not match the internal mapping done | ||
/// in this API and return `false`. Check APIError enum directly or create your own `UnauthorizedDeterming` rule, | ||
/// for example: | ||
/// ``` | ||
/// static let customRule = UnauthorizedDetermining { error in | ||
/// // Your custom logic to determine if `error` is unauthorized. | ||
/// // return `true` or `false`. | ||
/// } | ||
/// ``` | ||
/// | ||
/// - Parameter rule: Used to determine if the `APIError` is an unauthorized error. | ||
/// - Returns: `true` if unauthorized error, `false` otherwise | ||
public func isUnauthorized(rule: UnauthorizedDetermining = .default) -> Bool { | ||
rule.isUnauthorized(self) | ||
} | ||
|
||
public struct UnauthorizedDetermining { | ||
let isUnauthorized: (APIError) -> Bool | ||
|
||
public init(isUnauthenticated: @escaping (APIError) -> Bool) { | ||
self.isUnauthorized = isUnauthenticated | ||
} | ||
|
||
public static let `default` = UnauthorizedDetermining { error in | ||
error.containsUnauthorizedMessageString || error.is401or403 | ||
} | ||
} | ||
|
||
private var is401or403: Bool { | ||
switch self { | ||
case .httpStatusError(let statusCode, _): | ||
return statusCode == 401 || statusCode == 403 | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
private var containsUnauthorizedMessageString: Bool { | ||
switch self { | ||
case .operationError(let errorDescription, _, _): | ||
return errorDescription.range(of: APIError.UnauthorizedMessageString, options: .caseInsensitive) != nil | ||
default: return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters