Skip to content

Commit 6308920

Browse files
paulb777ryanwilson
andcommitted
[auth-swift] Rewrite Auth RPC types using Swift types (#11529)
* Rewrite Auth RPC types using Swift types. This largely stems from the unsafe casting that occurs. Current state: all types have been converted, almost no calls using those types have been converted, ran into an issue with TwitterAuthCredential where it takes a parameter that's an RPC type, but can't be visible in ObjC. This may result in an API change unfortunately, or a rethinking of how the AuthCredential and Providers work. --------- Co-authored-by: Ryan Wilson <wilsonryan@google.com>
1 parent 6fff705 commit 6308920

File tree

84 files changed

+692
-767
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+692
-767
lines changed

FirebaseAuth/Sources/Swift/Auth/Auth.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ extension Auth: AuthInterop {
299299
let request = CreateAuthURIRequest(identifier: email,
300300
continueURI: "http:www.google.com",
301301
requestConfiguration: self.requestConfiguration)
302-
AuthBackend.post(withRequest: request) { response, error in
302+
AuthBackend.post(with: request) { response, error in
303303
if let completion {
304304
DispatchQueue.main.async {
305-
completion((response as? CreateAuthURIResponse)?.signinMethods, error)
305+
completion(response?.signinMethods, error)
306306
}
307307
}
308308
}
@@ -384,12 +384,12 @@ extension Auth: AuthInterop {
384384
callback(nil, AuthErrorUtils.wrongPasswordError(message: nil))
385385
return
386386
}
387-
AuthBackend.post(withRequest: request) { rawResponse, error in
387+
AuthBackend.post(with: request) { rawResponse, error in
388388
if let error {
389389
callback(nil, error)
390390
return
391391
}
392-
guard let response = rawResponse as? VerifyPasswordResponse else {
392+
guard let response = rawResponse else {
393393
fatalError("Internal Auth Error: null response from VerifyPasswordRequest")
394394
}
395395
self.completeSignIn(withAccessToken: response.idToken,
@@ -746,12 +746,12 @@ extension Auth: AuthInterop {
746746
decoratedCallback(result, nil)
747747
}
748748
let request = SignUpNewUserRequest(requestConfiguration: self.requestConfiguration)
749-
AuthBackend.post(withRequest: request) { rawResponse, error in
749+
AuthBackend.post(with: request) { rawResponse, error in
750750
if let error {
751751
decoratedCallback(nil, error)
752752
return
753753
}
754-
guard let response = rawResponse as? SignUpNewUserResponse else {
754+
guard let response = rawResponse else {
755755
fatalError("Internal Auth Error: Failed to get a SignUpNewUserResponse")
756756
}
757757
self.completeSignIn(withAccessToken: response.idToken,
@@ -827,12 +827,12 @@ extension Auth: AuthInterop {
827827
let decoratedCallback = self.signInFlowAuthDataResultCallback(byDecorating: completion)
828828
let request = VerifyCustomTokenRequest(token: token,
829829
requestConfiguration: self.requestConfiguration)
830-
AuthBackend.post(withRequest: request) { rawResponse, error in
830+
AuthBackend.post(with: request) { rawResponse, error in
831831
if let error {
832832
decoratedCallback(nil, error)
833833
return
834834
}
835-
guard let response = rawResponse as? VerifyCustomTokenResponse else {
835+
guard let response = rawResponse else {
836836
fatalError("Internal Auth Error: Failed to get a VerifyCustomTokenResponse")
837837
}
838838
self.completeSignIn(withAccessToken: response.idToken,
@@ -932,12 +932,12 @@ extension Auth: AuthInterop {
932932
password: password,
933933
displayName: nil,
934934
requestConfiguration: self.requestConfiguration)
935-
AuthBackend.post(withRequest: request) { rawResponse, error in
935+
AuthBackend.post(with: request) { rawResponse, error in
936936
if let error {
937937
decoratedCallback(nil, error)
938938
return
939939
}
940-
guard let response = rawResponse as? SignUpNewUserResponse else {
940+
guard let response = rawResponse else {
941941
fatalError("Internal Auth Error: Failed to get a SignUpNewUserResponse")
942942
}
943943
self.completeSignIn(withAccessToken: response.idToken,
@@ -1025,7 +1025,7 @@ extension Auth: AuthInterop {
10251025
let request = ResetPasswordRequest(oobCode: code,
10261026
newPassword: newPassword,
10271027
requestConfiguration: self.requestConfiguration)
1028-
AuthBackend.post(withRequest: request) { _, error in
1028+
AuthBackend.post(with: request) { _, error in
10291029
DispatchQueue.main.async {
10301030
if let error {
10311031
completion(error)
@@ -1082,13 +1082,13 @@ extension Auth: AuthInterop {
10821082
let request = ResetPasswordRequest(oobCode: code,
10831083
newPassword: nil,
10841084
requestConfiguration: self.requestConfiguration)
1085-
AuthBackend.post(withRequest: request) { rawResponse, error in
1085+
AuthBackend.post(with: request) { rawResponse, error in
10861086
DispatchQueue.main.async {
10871087
if let error {
10881088
completion(nil, error)
10891089
return
10901090
}
1091-
guard let response = rawResponse as? ResetPasswordResponse,
1091+
guard let response = rawResponse,
10921092
let email = response.email else {
10931093
fatalError("Internal Auth Error: Failed to get a ResetPasswordResponse")
10941094
}
@@ -1176,7 +1176,7 @@ extension Auth: AuthInterop {
11761176
kAuthGlobalWorkQueue.async {
11771177
let request = SetAccountInfoRequest(requestConfiguration: self.requestConfiguration)
11781178
request.oobCode = code
1179-
AuthBackend.post(withRequest: request) { rawResponse, error in
1179+
AuthBackend.post(with: request) { rawResponse, error in
11801180
DispatchQueue.main.async {
11811181
completion(error)
11821182
}
@@ -1265,7 +1265,7 @@ extension Auth: AuthInterop {
12651265
actionCodeSettings: actionCodeSettings,
12661266
requestConfiguration: self.requestConfiguration
12671267
)
1268-
AuthBackend.post(withRequest: request) { response, error in
1268+
AuthBackend.post(with: request) { response, error in
12691269
if let completion {
12701270
DispatchQueue.main.async {
12711271
completion(error)
@@ -1334,7 +1334,7 @@ extension Auth: AuthInterop {
13341334
actionCodeSettings: actionCodeSettings,
13351335
requestConfiguration: self.requestConfiguration
13361336
)
1337-
AuthBackend.post(withRequest: request) { response, error in
1337+
AuthBackend.post(with: request) { response, error in
13381338
if let completion {
13391339
DispatchQueue.main.async {
13401340
completion(error)
@@ -1552,7 +1552,7 @@ extension Auth: AuthInterop {
15521552
let request = RevokeTokenRequest(withToken: authorizationCode,
15531553
idToken: idToken,
15541554
requestConfiguration: self.requestConfiguration)
1555-
AuthBackend.post(withRequest: request) { response, error in
1555+
AuthBackend.post(with: request) { response, error in
15561556
if let completion {
15571557
DispatchQueue.main.async {
15581558
if let error {
@@ -2157,7 +2157,7 @@ extension Auth: AuthInterop {
21572157
callback(nil, error)
21582158
return
21592159
}
2160-
guard let response = rawResponse as? VerifyPhoneNumberResponse else {
2160+
guard let response = rawResponse else {
21612161
fatalError("Internal Auth Error: Failed to get a VerifyPhoneNumberResponse")
21622162
}
21632163
self.completeSignIn(withAccessToken: response.idToken,
@@ -2192,14 +2192,14 @@ extension Auth: AuthInterop {
21922192
requestConfiguration: requestConfiguration)
21932193
request.autoCreate = !isReauthentication
21942194
credential.prepare(request)
2195-
AuthBackend.post(withRequest: request) { rawResponse, error in
2195+
AuthBackend.post(with: request) { rawResponse, error in
21962196
if let error {
21972197
if let callback {
21982198
callback(nil, error)
21992199
}
22002200
return
22012201
}
2202-
guard let response = rawResponse as? VerifyAssertionResponse else {
2202+
guard let response = rawResponse else {
22032203
fatalError("Internal Auth Error: Failed to get a VerifyAssertionResponse")
22042204
}
22052205
if response.needConfirmation {
@@ -2253,14 +2253,14 @@ extension Auth: AuthInterop {
22532253
*/
22542254
private func signIn(withPhoneCredential credential: PhoneAuthCredential,
22552255
operation: AuthOperationType,
2256-
callback: @escaping (AuthRPCResponse?, Error?) -> Void) {
2256+
callback: @escaping (VerifyPhoneNumberResponse?, Error?) -> Void) {
22572257
switch credential.credentialKind {
22582258
case let .phoneNumber(phoneNumber, temporaryProof):
22592259
let request = VerifyPhoneNumberRequest(temporaryProof: temporaryProof,
22602260
phoneNumber: phoneNumber,
22612261
operation: operation,
22622262
requestConfiguration: requestConfiguration)
2263-
AuthBackend.post(withRequest: request, callback: callback)
2263+
AuthBackend.post(with: request, callback: callback)
22642264
return
22652265
case let .verification(verificationID, code):
22662266
guard verificationID.count > 0 else {
@@ -2275,7 +2275,7 @@ extension Auth: AuthInterop {
22752275
verificationCode: code,
22762276
operation: operation,
22772277
requestConfiguration: requestConfiguration)
2278-
AuthBackend.post(withRequest: request, callback: callback)
2278+
AuthBackend.post(with: request, callback: callback)
22792279
}
22802280
}
22812281
#endif
@@ -2305,14 +2305,14 @@ extension Auth: AuthInterop {
23052305
timestamp: credential.timestamp,
23062306
displayName: credential.displayName,
23072307
requestConfiguration: requestConfiguration)
2308-
AuthBackend.post(withRequest: request) { rawResponse, error in
2308+
AuthBackend.post(with: request) { rawResponse, error in
23092309
if let error {
23102310
if let callback {
23112311
callback(nil, error)
23122312
}
23132313
return
23142314
}
2315-
guard let response = rawResponse as? SignInWithGameCenterResponse else {
2315+
guard let response = rawResponse else {
23162316
fatalError("Internal Auth Error: Failed to get a SignInWithGameCenterResponse")
23172317
}
23182318
self.completeSignIn(withAccessToken: response.idToken,
@@ -2362,14 +2362,14 @@ extension Auth: AuthInterop {
23622362
let request = EmailLinkSignInRequest(email: email,
23632363
oobCode: actionCode,
23642364
requestConfiguration: requestConfiguration)
2365-
AuthBackend.post(withRequest: request) { rawResponse, error in
2365+
AuthBackend.post(with: request) { rawResponse, error in
23662366
if let error {
23672367
if let callback {
23682368
callback(nil, error)
23692369
}
23702370
return
23712371
}
2372-
guard let response = rawResponse as? EmailLinkSignInResponse else {
2372+
guard let response = rawResponse else {
23732373
fatalError("Internal Auth Error: Failed to get a EmailLinkSignInResponse")
23742374
}
23752375
self.completeSignIn(withAccessToken: response.idToken,

FirebaseAuth/Sources/Swift/AuthProvider/AuthCredential.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import Foundation
2424
self.provider = provider
2525
}
2626

27-
// TODO: remove public after FIRUser port
28-
@objc(prepareVerifyAssertionRequest:) public func prepare(_ request: VerifyAssertionRequest) {
27+
func prepare(_ request: VerifyAssertionRequest) {
2928
fatalError("This method must be overridden by a subclass.")
3029
}
3130
}

FirebaseAuth/Sources/Swift/AuthProvider/FacebookAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Foundation
4646
super.init(provider: FacebookAuthProvider.id)
4747
}
4848

49-
@objc override func prepare(_ request: VerifyAssertionRequest) {
49+
override func prepare(_ request: VerifyAssertionRequest) {
5050
request.providerAccessToken = accessToken
5151
}
5252

FirebaseAuth/Sources/Swift/AuthProvider/GitHubAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GitHubAuthCredential: AuthCredential, NSSecureCoding {
4646
super.init(provider: GitHubAuthProvider.id)
4747
}
4848

49-
@objc override func prepare(_ request: VerifyAssertionRequest) {
49+
override func prepare(_ request: VerifyAssertionRequest) {
5050
request.providerAccessToken = token
5151
}
5252

FirebaseAuth/Sources/Swift/AuthProvider/GoogleAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import Foundation
5050
super.init(provider: GoogleAuthProvider.id)
5151
}
5252

53-
@objc override func prepare(_ request: VerifyAssertionRequest) {
53+
override func prepare(_ request: VerifyAssertionRequest) {
5454
request.providerIDToken = idToken
5555
request.providerAccessToken = accessToken
5656
}

FirebaseAuth/Sources/Swift/AuthProvider/OAuthCredential.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import Foundation
7474
super.init(provider: providerID)
7575
}
7676

77-
@objc public convenience init?(withVerifyAssertionResponse response: VerifyAssertionResponse) {
77+
convenience init?(withVerifyAssertionResponse response: VerifyAssertionResponse) {
7878
guard Self.nonEmptyString(response.oauthIDToken) ||
7979
Self.nonEmptyString(response.oauthAccessToken) ||
8080
Self.nonEmptyString(response.oauthSecretToken) else {
@@ -88,7 +88,7 @@ import Foundation
8888
pendingToken: response.pendingToken)
8989
}
9090

91-
@objc override public func prepare(_ request: VerifyAssertionRequest) {
91+
override func prepare(_ request: VerifyAssertionRequest) {
9292
request.providerIDToken = idToken
9393
request.providerRawNonce = rawNonce
9494
request.providerAccessToken = accessToken

FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ import FirebaseCore
264264
fatalError("Internal Phone Auth Error:Both reCAPTCHA token and app credential are nil")
265265
}
266266
if let request {
267-
AuthBackend.post(withRequest: request) { response, error in
267+
AuthBackend.post(with: request) { response, error in
268268
if let error {
269269
self.handleVerifyErrorWithRetry(error: error,
270270
phoneNumber: phoneNumber,
@@ -274,7 +274,7 @@ import FirebaseCore
274274
callback: callback)
275275
return
276276
}
277-
callback((response as? SendVerificationCodeResponse)?.verificationID, nil)
277+
callback(response?.verificationID, nil)
278278
}
279279
}
280280
}
@@ -302,8 +302,8 @@ import FirebaseCore
302302
requestConfiguration: auth.requestConfiguration
303303
)
304304

305-
AuthBackend.post(withRequest: request) { response, error in
306-
callback((response as? SendVerificationCodeResponse)?.verificationID, error)
305+
AuthBackend.post(with: request) { response, error in
306+
callback(response?.verificationID, error)
307307
}
308308
return
309309
}
@@ -324,7 +324,9 @@ import FirebaseCore
324324
appCredential: appCredential,
325325
reCAPTCHAToken: reCAPTCHAToken)
326326

327-
let request: AuthRPCRequest = (session.idToken != nil) ?
327+
// XXX TODO: Figure out the right logic here, where we're assuming the callback is a certain
328+
// type.
329+
let request: any AuthRPCRequest = (session.idToken != nil) ?
328330
StartMFAEnrollmentRequest(idToken: session.idToken,
329331
enrollmentInfo: startMFARequestInfo,
330332
requestConfiguration: self.auth.requestConfiguration) :
@@ -333,7 +335,7 @@ import FirebaseCore
333335
signInInfo: startMFARequestInfo,
334336
requestConfiguration: self.auth.requestConfiguration)
335337

336-
AuthBackend.post(withRequest: request) { response, error in
338+
AuthBackend.post(with: request) { response, error in
337339
if let error {
338340
self.handleVerifyErrorWithRetry(error: error,
339341
phoneNumber: phoneNumber,
@@ -397,7 +399,7 @@ import FirebaseCore
397399
let request = VerifyClientRequest(withAppToken: token.string,
398400
isSandbox: token.type == AuthAPNSTokenType.sandbox,
399401
requestConfiguration: self.auth.requestConfiguration)
400-
AuthBackend.post(withRequest: request) { response, error in
402+
AuthBackend.post(with: request) { response, error in
401403
if let error {
402404
let nserror = error as NSError
403405
// reCAPTCHA Flow if it's an invalid app credential or a missing app token.
@@ -412,7 +414,7 @@ import FirebaseCore
412414
return
413415
}
414416
}
415-
guard let verifyResponse = response as? VerifyClientResponse,
417+
guard let verifyResponse = response,
416418
let receipt = verifyResponse.receipt,
417419
let timeout = verifyResponse.suggestedTimeOutDate?.timeIntervalSinceNow else {
418420
fatalError("Internal Auth Error: invalid VerifyClientResponse")

FirebaseAuth/Sources/Swift/AuthProvider/TwitterAuthProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TwitterAuthCredential: AuthCredential, NSSecureCoding {
4949
super.init(provider: TwitterAuthProvider.id)
5050
}
5151

52-
@objc override func prepare(_ request: VerifyAssertionRequest) {
52+
override func prepare(_ request: VerifyAssertionRequest) {
5353
request.providerAccessToken = token
5454
request.providerOAuthTokenSecret = secret
5555
}

0 commit comments

Comments
 (0)