From a57cd0797d0a29378d7d00e8f13d8f4693300411 Mon Sep 17 00:00:00 2001 From: JekaNS Date: Sat, 20 Jul 2024 02:45:09 +0200 Subject: [PATCH 1/2] fix(firebase_auth): added supporting rawNonce for OAuth credential on Windows platform --- AUTHORS | 1 + .../test/firebase_auth_test.dart | 40 +++++++++++++++++++ .../windows/firebase_auth_plugin.cpp | 16 ++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 6505d237f219..f5c400e6a975 100644 --- a/AUTHORS +++ b/AUTHORS @@ -64,3 +64,4 @@ Mohd Faheem Ansari Om Phatak Horváth István Liu Zhisong +Ievgenii Kovtun diff --git a/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart index 0a8ae00321dd..3cb31a469c09 100644 --- a/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/firebase_auth/test/firebase_auth_test.dart @@ -26,6 +26,7 @@ void main() { const String kMockEmail = 'test@example.com'; const String kMockPassword = 'passw0rd'; const String kMockIdToken = '12345'; + const String kMockRawNonce = 'abcde12345'; const String kMockAccessToken = '67890'; const String kMockGithubToken = 'github'; const String kMockCustomToken = '12345'; @@ -587,6 +588,45 @@ void main() { expect(captured.providerId, equals('apple.com')); expect(captured.idToken, equals(kMockIdToken)); expect(captured.accessToken, equals(kMockAccessToken)); + expect(captured.rawNonce, equals(null)); + }); + + test('OAuthProvider signInWithCredential for Apple with rawNonce', + () async { + OAuthProvider oAuthProvider = OAuthProvider('apple.com'); + final AuthCredential credential = oAuthProvider.credential( + idToken: kMockIdToken, + rawNonce: kMockRawNonce, + accessToken: kMockAccessToken, + ); + await auth.signInWithCredential(credential); + final captured = + verify(mockAuthPlatform.signInWithCredential(captureAny)) + .captured + .single; + expect(captured.providerId, equals('apple.com')); + expect(captured.idToken, equals(kMockIdToken)); + expect(captured.rawNonce, equals(kMockRawNonce)); + expect(captured.accessToken, equals(kMockAccessToken)); + }); + + test( + 'OAuthProvider signInWithCredential for Apple with rawNonce (empty accessToken)', + () async { + OAuthProvider oAuthProvider = OAuthProvider('apple.com'); + final AuthCredential credential = oAuthProvider.credential( + idToken: kMockIdToken, + rawNonce: kMockRawNonce, + ); + await auth.signInWithCredential(credential); + final captured = + verify(mockAuthPlatform.signInWithCredential(captureAny)) + .captured + .single; + expect(captured.providerId, equals('apple.com')); + expect(captured.idToken, equals(kMockIdToken)); + expect(captured.rawNonce, equals(kMockRawNonce)); + expect(captured.accessToken, equals(null)); }); test('PhoneAuthProvider signInWithCredential', () async { diff --git a/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp b/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp index 1912b9221529..b32e93f78892 100644 --- a/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp +++ b/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp @@ -662,9 +662,19 @@ firebase::auth::Credential getCredentialFromArguments( if (signInMethod == kSignInMethodOAuth) { std::string providerId = std::get(arguments[kArgumentProviderId]); - return firebase::auth::OAuthProvider::GetCredential( - providerId.c_str(), idToken.value().c_str(), - accessToken.value().c_str()); + std::optional rawNonce = getStringOpt(kArgumentRawNonce); + // If rawNonce provided use corresponding credential builder + // e.g. AppleID auth through the webView + if(rawNonce) { + return firebase::auth::OAuthProvider::GetCredential( + providerId.c_str(), idToken.value().c_str(), + rawNonce.value().c_str(), + accessToken ? accessToken.value().c_str() : nullptr); + } else { + return firebase::auth::OAuthProvider::GetCredential( + providerId.c_str(), idToken.value().c_str(), + accessToken.value().c_str()); + } } // If no known auth method matched From bc04ec4fe43a2f34814b065d4e886390a4de61a7 Mon Sep 17 00:00:00 2001 From: JekaNS Date: Fri, 26 Jul 2024 18:23:27 +0200 Subject: [PATCH 2/2] fix(firebase_auth): added supporting rawNonce for OAuth credential on Windows platform fix formating --- .../firebase_auth/windows/firebase_auth_plugin.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp b/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp index b32e93f78892..7df94ba1e530 100644 --- a/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp +++ b/packages/firebase_auth/firebase_auth/windows/firebase_auth_plugin.cpp @@ -665,10 +665,9 @@ firebase::auth::Credential getCredentialFromArguments( std::optional rawNonce = getStringOpt(kArgumentRawNonce); // If rawNonce provided use corresponding credential builder // e.g. AppleID auth through the webView - if(rawNonce) { + if (rawNonce) { return firebase::auth::OAuthProvider::GetCredential( - providerId.c_str(), idToken.value().c_str(), - rawNonce.value().c_str(), + providerId.c_str(), idToken.value().c_str(), rawNonce.value().c_str(), accessToken ? accessToken.value().c_str() : nullptr); } else { return firebase::auth::OAuthProvider::GetCredential(