-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBrowserSigninError+Extensions.swift
More file actions
150 lines (135 loc) · 6.93 KB
/
Copy pathBrowserSigninError+Extensions.swift
File metadata and controls
150 lines (135 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//
import Foundation
import AuthenticationServices
@available(iOS 13.0, macOS 10.15, tvOS 16.0, watchOS 7.0, visionOS 1.0, macCatalyst 13.0, *)
extension BrowserSigninError: LocalizedError {
init(_ error: any Error) {
let nsError = error as NSError
if nsError.domain == ASWebAuthenticationSessionErrorDomain,
nsError.code == ASWebAuthenticationSessionError.canceledLogin.rawValue
{
self = .userCancelledLogin(nsError.localizedFailureReason)
} else if let error = error as? OAuth2Error {
self = .oauth2(error: error)
} else if let error = error as? OAuth2ServerError {
self = .serverError(error)
} else {
self = .generic(error: error)
}
}
public var errorDescription: String? {
switch self {
case .noCompatibleAuthenticationProviders:
return NSLocalizedString("no_compatible_authentication_providers_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "")
case .cannotComposeAuthenticationURL:
return NSLocalizedString("cannot_compose_authentication_url_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "")
case .authenticationProvider(error: let error):
if let error = error as? (any LocalizedError) {
return error.localizedDescription
}
return NSLocalizedString("authentication_provider_error",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "")
case .invalidRedirectScheme(let scheme):
return String.localizedStringWithFormat(
NSLocalizedString("invalid_redirect_scheme_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: ""),
scheme ?? NSLocalizedString("no_scheme_defined",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: ""))
case .userCancelledLogin(let reason):
if let reason {
return String.localizedStringWithFormat(
NSLocalizedString("user_cancelled_login_reason_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: ""),
reason)
} else {
return NSLocalizedString("user_cancelled_login_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "")
}
case .missingIdToken:
return NSLocalizedString("missing_id_token_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "Missing ID Token")
case .oauth2(error: let error):
return error.localizedDescription
case .serverError(let error):
return error.errorDescription
case .generic(error: let error):
if let error = error as? (any LocalizedError) {
return error.localizedDescription
}
let errorString = String(describing: error)
return String.localizedStringWithFormat(
NSLocalizedString("generic_description",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: ""),
errorString)
case .noAuthenticatorProviderResonse:
return NSLocalizedString("no_authenticator_provider_response",
tableName: "BrowserSignin",
bundle: .browserSignin,
comment: "No authenticator provider response")
case .genericError(message: let message):
return message
case .noSignOutFlowProvided:
return "FOO"
case .cannotStartBrowserSession:
return "FOO"
}
}
}
extension BrowserSigninError: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.noCompatibleAuthenticationProviders, .noCompatibleAuthenticationProviders): return true
case (.noSignOutFlowProvided, .noSignOutFlowProvided): return true
case (.cannotStartBrowserSession, .cannotStartBrowserSession): return true
case (.cannotComposeAuthenticationURL, .cannotComposeAuthenticationURL): return true
case (.userCancelledLogin(let lhs), .userCancelledLogin(let rhs)):
return lhs == rhs
case (.noAuthenticatorProviderResonse, .noAuthenticatorProviderResonse): return true
case (.missingIdToken, .missingIdToken): return true
case (.authenticationProvider(error: let lhsValue), .authenticationProvider(error: let rhsValue)):
return lhsValue as NSError == rhsValue as NSError
case (.serverError(let lhsValue), .serverError(let rhsValue)):
return lhsValue == rhsValue
case (.invalidRedirectScheme(let lhsValue), .invalidRedirectScheme(let rhsValue)):
return lhsValue == rhsValue
case (.oauth2(error: let lhsValue), .oauth2(error: let rhsValue)):
return lhsValue == rhsValue
case (.generic(error: let lhsValue), .generic(error: let rhsValue)):
return lhsValue as NSError == rhsValue as NSError
case (.genericError(message: let lhsValue), .genericError(message: let rhsValue)):
return lhsValue == rhsValue
default:
return false
}
}
}