|
126 | 126 | return actionString |
127 | 127 | #else |
128 | 128 |
|
129 | | - let (token, error, linked, actionCreated) = await recaptchaToken(siteKey: siteKey, actionString: actionString, fakeToken: "NO_RECAPTCHA") |
130 | | - |
131 | | - guard linked else { |
132 | | - throw AuthErrorUtils.recaptchaSDKNotLinkedError() |
133 | | - } |
134 | | - guard actionCreated else { |
135 | | - throw AuthErrorUtils.recaptchaActionCreationFailed() |
136 | | - } |
137 | | - if let error { |
138 | | - throw error |
139 | | - } |
140 | | - if token == "NO_RECAPTCHA" { |
141 | | - AuthLog.logInfo(code: "I-AUT000031", |
142 | | - message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.") |
143 | | - } else { |
144 | | - AuthLog.logInfo( |
145 | | - code: "I-AUT000030", |
146 | | - message: "reCAPTCHA token retrieval succeeded." |
| 129 | + let (token, error, linked, actionCreated) = await recaptchaToken( |
| 130 | + siteKey: siteKey, |
| 131 | + actionString: actionString, |
| 132 | + fakeToken: "NO_RECAPTCHA" |
147 | 133 | ) |
148 | | - } |
149 | | - return token |
| 134 | + |
| 135 | + guard linked else { |
| 136 | + throw AuthErrorUtils.recaptchaSDKNotLinkedError() |
| 137 | + } |
| 138 | + guard actionCreated else { |
| 139 | + throw AuthErrorUtils.recaptchaActionCreationFailed() |
| 140 | + } |
| 141 | + if let error { |
| 142 | + throw error |
| 143 | + } |
| 144 | + if token == "NO_RECAPTCHA" { |
| 145 | + AuthLog.logInfo(code: "I-AUT000031", |
| 146 | + message: "reCAPTCHA token retrieval failed. NO_RECAPTCHA sent as the fake code.") |
| 147 | + } else { |
| 148 | + AuthLog.logInfo( |
| 149 | + code: "I-AUT000030", |
| 150 | + message: "reCAPTCHA token retrieval succeeded." |
| 151 | + ) |
| 152 | + } |
| 153 | + return token |
150 | 154 | #endif // !(COCOAPODS || SWIFT_PACKAGE) |
151 | 155 | } |
152 | 156 |
|
153 | 157 | private static var recaptchaClient: (any RCARecaptchaClientProtocol)? |
154 | 158 |
|
155 | | - private func recaptchaToken( |
156 | | - siteKey: String, |
157 | | - actionString: String, |
158 | | - fakeToken: String |
159 | | - ) async -> (token: String, error: Error?, linked: Bool, actionCreated: Bool) { |
| 159 | + private func recaptchaToken(siteKey: String, |
| 160 | + actionString: String, |
| 161 | + fakeToken: String) async -> (token: String, error: Error?, |
| 162 | + linked: Bool, actionCreated: Bool) { |
160 | 163 | if recaptchaClient != nil { |
161 | 164 | return await retrieveToken(actionString: actionString, fakeToken: fakeToken) |
162 | 165 | } |
163 | 166 |
|
164 | | - if let recaptcha = NSClassFromString("RecaptchaEnterprise.RCARecaptcha") as? RCARecaptchaProtocol.Type { |
| 167 | + if let recaptcha = |
| 168 | + NSClassFromString("RecaptchaEnterprise.RCARecaptcha") as? RCARecaptchaProtocol.Type { |
165 | 169 | do { |
166 | 170 | // TODO(ncooke3): This should be `fetchClient(withSiteKey:)`. |
167 | 171 | let client = try await recaptcha.getClient(withSiteKey: siteKey) |
168 | 172 | recaptchaClient = client |
169 | 173 | return await retrieveToken(actionString: actionString, fakeToken: fakeToken) |
170 | 174 | } catch { |
171 | | - return ("", error, true, true); |
| 175 | + return ("", error, true, true) |
172 | 176 | } |
173 | 177 | } else if let recaptcha = NSClassFromString("Recaptcha") { |
174 | 178 | // Fall back to attempting to connect with pre-18.7.0 RecaptchaEnterprise. |
| 179 | + do { |
175 | 180 | let client = try await recaptcha.getClient(withSiteKey: siteKey) |
176 | 181 | recaptchaClient = client |
177 | 182 | return await retrieveToken(actionString: actionString, fakeToken: fakeToken) |
178 | 183 | } catch { |
179 | | - return ("", error, true, true); |
| 184 | + return ("", error, true, true) |
180 | 185 | } |
181 | 186 | } else { |
182 | 187 | // RecaptchaEnterprise not linked. |
183 | 188 | return ("", nil, false, false) |
184 | 189 | } |
185 | 190 | } |
186 | 191 |
|
187 | | - private func retrieveToken( |
188 | | - actionString: String, |
189 | | - fakeToken: String |
190 | | - ) async -> (token: String, error: Error?, linked: Bool, actionCreated: Bool) { |
| 192 | + private func retrieveToken(actionString: String, |
| 193 | + fakeToken: String) async -> (token: String, error: Error?, |
| 194 | + linked: Bool, actionCreated: Bool) { |
191 | 195 | let recaptchaAction = ( |
192 | 196 | NSClassFromString("RecaptchaEnterprise.RCAAction") ?? NSClassFromString("RecaptchaAction") |
193 | 197 | ) as? RCAActionProtocol.Type |
|
0 commit comments