Skip to content

Commit 1779308

Browse files
feat(auth, apple): create a credential with idToken, rawNonce & appleFullPersonName (#12356)
1 parent 093b5fe commit 1779308

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

‎packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
NSString *const kArgumentVerificationId = @"verificationId";
5353
NSString *const kArgumentSmsCode = @"smsCode";
5454
NSString *const kArgumentActionCodeSettings = @"actionCodeSettings";
55+
NSString *const kArgumentFamilyName = @"familyName";
56+
NSString *const kArgumentGivenName = @"givenName";
57+
NSString *const kArgumentMiddleName = @"middleName";
58+
NSString *const kArgumentNickname = @"nickname";
59+
NSString *const kArgumentNamePrefix = @"namePrefix";
60+
NSString *const kArgumentNameSuffix = @"nameSuffix";
5561

5662
// MultiFactor
5763
NSString *const kArgumentMultiFactorHints = @"multiFactorHints";
@@ -754,7 +760,31 @@ - (void)getFIRAuthCredentialFromArguments:(NSDictionary *)arguments
754760
return;
755761
#endif
756762
}
757-
763+
// Apple Auth
764+
if ([signInMethod isEqualToString:kSignInMethodApple]) {
765+
if (idToken && rawNonce) {
766+
// Credential with idToken, rawNonce and fullName
767+
NSPersonNameComponents *fullName = [[NSPersonNameComponents alloc] init];
768+
fullName.givenName =
769+
arguments[kArgumentGivenName] == [NSNull null] ? nil : arguments[kArgumentGivenName];
770+
fullName.familyName =
771+
arguments[kArgumentFamilyName] == [NSNull null] ? nil : arguments[kArgumentFamilyName];
772+
fullName.nickname =
773+
arguments[kArgumentNickname] == [NSNull null] ? nil : arguments[kArgumentNickname];
774+
fullName.namePrefix =
775+
arguments[kArgumentNamePrefix] == [NSNull null] ? nil : arguments[kArgumentNamePrefix];
776+
fullName.nameSuffix =
777+
arguments[kArgumentNameSuffix] == [NSNull null] ? nil : arguments[kArgumentNameSuffix];
778+
fullName.middleName =
779+
arguments[kArgumentMiddleName] == [NSNull null] ? nil : arguments[kArgumentMiddleName];
780+
781+
completion([FIROAuthProvider appleCredentialWithIDToken:idToken
782+
rawNonce:rawNonce
783+
fullName:fullName],
784+
nil);
785+
return;
786+
}
787+
}
758788
// OAuth
759789
if ([signInMethod isEqualToString:kSignInMethodOAuth]) {
760790
NSString *providerId = arguments[kArgumentProviderId];

‎packages/firebase_auth/firebase_auth/lib/firebase_auth.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export 'package:firebase_auth_platform_interface/firebase_auth_platform_interfac
3434
AuthCredential,
3535
AuthProvider,
3636
AppleAuthProvider,
37+
AppleFullPersonName,
3738
AppleAuthCredential,
3839
EmailAuthProvider,
3940
EmailAuthCredential,

‎packages/firebase_auth/firebase_auth_platform_interface/lib/src/providers/apple_auth.dart

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ class AppleAuthProvider extends AuthProvider {
4646
);
4747
}
4848

49+
/// Create a new [AppleAuthCredential] from a provided [idToken], [rawNonce] and [appleFullPersonName];
50+
static OAuthCredential credentialWithIDToken(
51+
String idToken,
52+
String rawNonce,
53+
AppleFullPersonName appleFullPersonName,
54+
) {
55+
return AppleAuthCredential._credentialWithIDToken(
56+
idToken,
57+
rawNonce,
58+
appleFullPersonName,
59+
);
60+
}
61+
4962
/// This corresponds to the sign-in method identifier.
5063
static String get APPLE_SIGN_IN_METHOD {
5164
return _kProviderId;
@@ -89,13 +102,52 @@ class AppleAuthProvider extends AuthProvider {
89102
/// [AppleAuthProvider.credential].
90103
class AppleAuthCredential extends OAuthCredential {
91104
AppleAuthCredential._({
92-
required String accessToken,
105+
String? accessToken,
106+
String? rawNonce,
107+
String? idToken,
108+
AppleFullPersonName? appleFullPersonName,
93109
}) : super(
94-
providerId: _kProviderId,
95-
signInMethod: _kProviderId,
96-
accessToken: accessToken);
110+
providerId: _kProviderId,
111+
signInMethod: _kProviderId,
112+
accessToken: accessToken,
113+
appleFullPersonName: appleFullPersonName,
114+
rawNonce: rawNonce,
115+
idToken: idToken,
116+
);
97117

98118
factory AppleAuthCredential._credential(String accessToken) {
99-
return AppleAuthCredential._(accessToken: accessToken);
119+
return AppleAuthCredential._(
120+
accessToken: accessToken,
121+
);
122+
}
123+
124+
factory AppleAuthCredential._credentialWithIDToken(
125+
String idToken,
126+
String rawNonce,
127+
AppleFullPersonName appleFullPersonName,
128+
) {
129+
return AppleAuthCredential._(
130+
idToken: idToken,
131+
rawNonce: rawNonce,
132+
appleFullPersonName: appleFullPersonName,
133+
);
100134
}
101135
}
136+
137+
class AppleFullPersonName {
138+
AppleFullPersonName({
139+
this.givenName,
140+
this.familyName,
141+
this.middleName,
142+
this.nickname,
143+
this.namePrefix,
144+
this.nameSuffix,
145+
});
146+
147+
final String? givenName;
148+
final String? familyName;
149+
final String? middleName;
150+
final String? nickname;
151+
final String? namePrefix;
152+
final String? nameSuffix;
153+
}

‎packages/firebase_auth/firebase_auth_platform_interface/lib/src/providers/oauth.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class OAuthCredential extends AuthCredential {
8282
this.secret,
8383
this.rawNonce,
8484
this.serverAuthCode,
85+
this.appleFullPersonName,
8586
}) : super(
8687
providerId: providerId,
8788
signInMethod: signInMethod,
@@ -104,6 +105,12 @@ class OAuthCredential extends AuthCredential {
104105
/// the server auth code for Play Games credential.
105106
final String? serverAuthCode;
106107

108+
/// The full name of the user. Used to create an AuthCredential for the
109+
/// Sign in with Apple OAuth 2 provider identified by ID token, raw nonce,
110+
/// and full name. This method is specific to the Sign in with Apple OAuth 2
111+
/// provider as this provider requires the full name to be passed explicitly.
112+
final AppleFullPersonName? appleFullPersonName;
113+
107114
@override
108115
Map<String, String?> asMap() {
109116
return <String, String?>{
@@ -114,6 +121,12 @@ class OAuthCredential extends AuthCredential {
114121
'secret': secret,
115122
'rawNonce': rawNonce,
116123
'serverAuthCode': serverAuthCode,
124+
'familyName': appleFullPersonName?.familyName,
125+
'givenName': appleFullPersonName?.givenName,
126+
'middleName': appleFullPersonName?.middleName,
127+
'nickname': appleFullPersonName?.nickname,
128+
'namePrefix': appleFullPersonName?.namePrefix,
129+
'nameSuffix': appleFullPersonName?.nameSuffix,
117130
};
118131
}
119132
}

0 commit comments

Comments
 (0)