Skip to content

Commit b30d005

Browse files
Implement oauth secret token in headful-lite. (#2663)
* Implement oauth secret token in headful-lite. This is useful for Twitter sign in. * Deprecate TwitterAuth{Provider,Credential}. OAuth sign in flow is now the recommended way to sign in with Twitter. * Add update OAuth credential to AuthDataResult. Developer can access the secret token from that.
1 parent ec23996 commit b30d005

20 files changed

+136
-34
lines changed

Example/Auth/Tests/FIRUserTests.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,8 @@ - (void)testlinkAndRetrieveDataError {
15131513
FIRVerifyAssertionResponseCallback callback) {
15141514
dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
15151515
callback(nil,
1516-
[FIRAuthErrorUtils accountExistsWithDifferentCredentialErrorWithEmail:kEmail]);
1516+
[FIRAuthErrorUtils accountExistsWithDifferentCredentialErrorWithEmail:kEmail
1517+
updatedCredential:nil]);
15171518
});
15181519
});
15191520

Example/Firebase.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6659,7 +6659,7 @@
66596659
DEBUG_INFORMATION_FORMAT = dwarf;
66606660
DEVELOPMENT_TEAM = 4ANB9W7R3P;
66616661
GCC_C_LANGUAGE_STANDARD = gnu11;
6662-
INFOPLIST_FILE = $SRCROOT/DynamicLinks/FDLBuilderTestAppObjC/Info.plist;
6662+
INFOPLIST_FILE = "$SRCROOT/DynamicLinks/FDLBuilderTestAppObjC/Info.plist";
66636663
IPHONEOS_DEPLOYMENT_TARGET = 11.4;
66646664
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
66656665
MTL_ENABLE_DEBUG_INFO = YES;
@@ -6693,7 +6693,7 @@
66936693
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
66946694
DEVELOPMENT_TEAM = 4ANB9W7R3P;
66956695
GCC_C_LANGUAGE_STANDARD = gnu11;
6696-
INFOPLIST_FILE = $SRCROOT/DynamicLinks/FDLBuilderTestAppObjC/Info.plist;
6696+
INFOPLIST_FILE = "$SRCROOT/DynamicLinks/FDLBuilderTestAppObjC/Info.plist";
66976697
IPHONEOS_DEPLOYMENT_TARGET = 11.4;
66986698
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
66996699
MTL_ENABLE_DEBUG_INFO = NO;

Firebase/Auth/Source/Auth Provider/OAuth/FIROAuthCredential.m

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#import "FIRAuthExceptionUtils.h"
2121
#import "FIROAuthCredential_Internal.h"
2222
#import "FIRVerifyAssertionRequest.h"
23+
#import "FIRVerifyAssertionResponse.h"
2324

2425
NS_ASSUME_NONNULL_BEGIN
2526

@@ -40,30 +41,50 @@ - (nullable instancetype)initWithProvider:(NSString *)provider {
4041
- (instancetype)initWithProviderID:(NSString *)providerID
4142
IDToken:(nullable NSString *)IDToken
4243
accessToken:(nullable NSString *)accessToken
44+
secret:(nullable NSString *)secret
4345
pendingToken:(nullable NSString *)pendingToken {
4446
self = [super initWithProvider:providerID];
4547
if (self) {
4648
_IDToken = IDToken;
4749
_accessToken = accessToken;
4850
_pendingToken = pendingToken;
51+
_secret = secret;
4952
}
5053
return self;
5154
}
5255

5356
- (instancetype)initWithProviderID:(NSString *)providerID
5457
sessionID:(NSString *)sessionID
5558
OAuthResponseURLString:(NSString *)OAuthResponseURLString {
56-
self = [self initWithProviderID:providerID IDToken:nil accessToken:nil pendingToken:nil];
59+
self =
60+
[self initWithProviderID:providerID IDToken:nil accessToken:nil secret:nil pendingToken:nil];
5761
if (self) {
5862
_OAuthResponseURLString = OAuthResponseURLString;
5963
_sessionID = sessionID;
6064
}
6165
return self;
6266
}
6367

68+
69+
- (nullable instancetype)initWithVerifyAssertionResponse:(FIRVerifyAssertionResponse *)response {
70+
if (response.oauthIDToken.length || response.oauthAccessToken.length ||
71+
response.oauthSecretToken.length) {
72+
return [self initWithProviderID:response.providerID
73+
IDToken:response.oauthIDToken
74+
accessToken:response.oauthAccessToken
75+
secret:response.oauthSecretToken
76+
pendingToken:response.pendingToken];
77+
}
78+
return nil;
79+
}
80+
6481
- (void)prepareVerifyAssertionRequest:(FIRVerifyAssertionRequest *)request {
6582
request.providerIDToken = _IDToken;
6683
request.providerAccessToken = _accessToken;
84+
request.requestURI = _OAuthResponseURLString;
85+
request.sessionID = _sessionID;
86+
request.providerOAuthTokenSecret = _secret;
87+
request.pendingToken = _pendingToken;
6788
}
6889

6990
#pragma mark - NSSecureCoding
@@ -76,9 +97,11 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
7697
NSString *IDToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"IDToken"];
7798
NSString *accessToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"accessToken"];
7899
NSString *pendingToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"pendingToken"];
100+
NSString *secret = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"secret"];
79101
self = [self initWithProviderID:self.provider
80102
IDToken:IDToken
81103
accessToken:accessToken
104+
secret:secret
82105
pendingToken:pendingToken];
83106
return self;
84107
}
@@ -87,6 +110,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
87110
[aCoder encodeObject:self.IDToken forKey:@"IDToken"];
88111
[aCoder encodeObject:self.accessToken forKey:@"accessToken"];
89112
[aCoder encodeObject:self.pendingToken forKey:@"pendingToken"];
113+
[aCoder encodeObject:self.secret forKey:@"secret"];
90114
}
91115

92116
@end

Firebase/Auth/Source/Auth Provider/OAuth/FIROAuthCredential_Internal.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#import "FIROAuthCredential.h"
2020

21+
@class FIRVerifyAssertionResponse;
22+
2123
NS_ASSUME_NONNULL_BEGIN
2224

2325
/** @extension FIROAuthCredential
@@ -40,16 +42,18 @@ NS_ASSUME_NONNULL_BEGIN
4042
*/
4143
@property(nonatomic, readonly, nullable) NSString *pendingToken;
4244

43-
/** @fn initWithProviderId:IDToken:accessToken:pendingToken
45+
/** @fn initWithProviderId:IDToken:accessToken:secret:pendingToken
4446
@brief Designated initializer.
4547
@param providerID The provider ID associated with the credential being created.
4648
@param IDToken The ID Token associated with the credential being created.
4749
@param accessToken The access token associated with the credential being created.
50+
@param secret The secret associated with the credential being created.
4851
@param pendingToken The pending token associated with the credential being created.
4952
*/
5053
- (instancetype)initWithProviderID:(NSString *)providerID
5154
IDToken:(nullable NSString *)IDToken
5255
accessToken:(nullable NSString *)accessToken
56+
secret:(nullable NSString *)secret
5357
pendingToken:(nullable NSString *)pendingToken NS_DESIGNATED_INITIALIZER;
5458

5559
/** @fn initWithProviderId:sessionID:OAuthResponseURLString:
@@ -62,6 +66,12 @@ NS_ASSUME_NONNULL_BEGIN
6266
sessionID:(NSString *)sessionID
6367
OAuthResponseURLString:(NSString *)OAuthResponseURLString;
6468

69+
/** @fn initWithVerifyAssertionResponse
70+
@brief Intitializer which takes an verifyAssertion response.
71+
@param response The verifyAssertion Response to create the credential instance.
72+
*/
73+
- (nullable instancetype)initWithVerifyAssertionResponse:(FIRVerifyAssertionResponse *)response;
74+
6575
@end
6676

6777
NS_ASSUME_NONNULL_END

Firebase/Auth/Source/Auth Provider/OAuth/FIROAuthProvider.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ + (FIROAuthCredential *)credentialWithProviderID:(NSString *)providerID
7070
return [[FIROAuthCredential alloc] initWithProviderID:providerID
7171
IDToken:IDToken
7272
accessToken:accessToken
73+
secret:nil
7374
pendingToken:nil];
7475
}
7576

@@ -78,6 +79,7 @@ + (FIROAuthCredential *)credentialWithProviderID:(NSString *)providerID
7879
return [[FIROAuthCredential alloc] initWithProviderID:providerID
7980
IDToken:nil
8081
accessToken:accessToken
82+
secret:nil
8183
pendingToken:nil];
8284
}
8385

Firebase/Auth/Source/Auth Provider/Twitter/FIRTwitterAuthCredential.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
2323
/** @class FIRTwitterAuthCredential
2424
@brief Internal implementation of FIRAuthCredential for Twitter credentials.
2525
*/
26+
DEPRECATED_MSG_ATTRIBUTE("Please use FIROAuthCredential instead of FIRTwitterAuthCredential.")
2627
@interface FIRTwitterAuthCredential : FIRAuthCredential <NSSecureCoding>
2728

2829
/** @property token

Firebase/Auth/Source/Auth Provider/Twitter/FIRTwitterAuthCredential.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
NS_ASSUME_NONNULL_BEGIN
2424

25+
#pragma clang diagnostic push
26+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
27+
2528
@interface FIRTwitterAuthCredential ()
2629

2730
- (nullable instancetype)initWithProvider:(NSString *)provider NS_UNAVAILABLE;
@@ -70,4 +73,6 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
7073

7174
@end
7275

76+
#pragma clang diagnostic pop
77+
7378
NS_ASSUME_NONNULL_END

Firebase/Auth/Source/Auth Provider/Twitter/FIRTwitterAuthProvider.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
NS_ASSUME_NONNULL_BEGIN
2525

26+
#pragma clang diagnostic push
27+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
28+
2629
@implementation FIRTwitterAuthProvider
2730

2831
- (instancetype)init {
@@ -37,4 +40,6 @@ + (FIRAuthCredential *)credentialWithToken:(NSString *)token secret:(NSString *)
3740

3841
@end
3942

43+
#pragma clang diagnostic pop
44+
4045
NS_ASSUME_NONNULL_END

Firebase/Auth/Source/Auth/FIRAuth.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,6 @@ - (void)internalSignInAndRetrieveDataWithCredential:(FIRAuthCredential *)credent
857857
requestConfiguration:_requestConfiguration];
858858
request.autoCreate = !isReauthentication;
859859
[credential prepareVerifyAssertionRequest:request];
860-
if ([credential isKindOfClass:[FIROAuthCredential class]]) {
861-
FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential;
862-
request.requestURI = OAuthCredential.OAuthResponseURLString;
863-
request.sessionID = OAuthCredential.sessionID;
864-
request.pendingToken = OAuthCredential.pendingToken;
865-
}
866860
[FIRAuthBackend verifyAssertion:request
867861
callback:^(FIRVerifyAssertionResponse *response, NSError *error) {
868862
if (error) {
@@ -875,7 +869,10 @@ - (void)internalSignInAndRetrieveDataWithCredential:(FIRAuthCredential *)credent
875869
if (response.needConfirmation) {
876870
if (callback) {
877871
NSString *email = response.email;
878-
callback(nil, [FIRAuthErrorUtils accountExistsWithDifferentCredentialErrorWithEmail:email]);
872+
FIROAuthCredential *credential =
873+
[[FIROAuthCredential alloc] initWithVerifyAssertionResponse:response];
874+
callback(nil, [FIRAuthErrorUtils accountExistsWithDifferentCredentialErrorWithEmail:email
875+
updatedCredential:credential]);
879876
}
880877
return;
881878
}
@@ -894,9 +891,12 @@ - (void)internalSignInAndRetrieveDataWithCredential:(FIRAuthCredential *)credent
894891
if (callback) {
895892
FIRAdditionalUserInfo *additionalUserInfo =
896893
[FIRAdditionalUserInfo userInfoWithVerifyAssertionResponse:response];
894+
FIROAuthCredential *updatedOAuthCredential =
895+
[[FIROAuthCredential alloc] initWithVerifyAssertionResponse:response];
897896
FIRAuthDataResult *result = user ?
898897
[[FIRAuthDataResult alloc] initWithUser:user
899-
additionalUserInfo:additionalUserInfo] : nil;
898+
additionalUserInfo:additionalUserInfo
899+
credential:updatedOAuthCredential] : nil;
900900
callback(result, error);
901901
}
902902
}];

Firebase/Auth/Source/Auth/FIRAuthDataResult.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import "FIRAdditionalUserInfo.h"
2020
#import "FIRUser.h"
21+
#import "FIROAuthCredential.h"
2122

2223
NS_ASSUME_NONNULL_BEGIN
2324

@@ -33,12 +34,24 @@ @implementation FIRAuthDataResult
3334
*/
3435
static NSString *const kUserCodingKey = @"user";
3536

37+
/** @var kCredentialCodingKey
38+
@brief The key used to encode the credential for NSSecureCoding.
39+
*/
40+
static NSString *const kCredentialCodingKey = @"credential";
41+
3642
- (nullable instancetype)initWithUser:(nullable FIRUser *)user
3743
additionalUserInfo:(nullable FIRAdditionalUserInfo *)additionalUserInfo {
44+
return [self initWithUser:user additionalUserInfo:additionalUserInfo credential:nil];
45+
}
46+
47+
- (nullable instancetype)initWithUser:(nullable FIRUser *)user
48+
additionalUserInfo:(nullable FIRAdditionalUserInfo *)additionalUserInfo
49+
credential:(nullable FIROAuthCredential *)credential {
3850
self = [super init];
3951
if (self) {
4052
_additionalUserInfo = additionalUserInfo;
4153
_user = user;
54+
_credential = credential;
4255
}
4356
return self;
4457
}
@@ -55,13 +68,16 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
5568
FIRAdditionalUserInfo *additionalUserInfo =
5669
[aDecoder decodeObjectOfClass:[FIRAdditionalUserInfo class]
5770
forKey:kAdditionalUserInfoCodingKey];
58-
59-
return [self initWithUser:user additionalUserInfo:additionalUserInfo];
71+
FIROAuthCredential *credential =
72+
[aDecoder decodeObjectOfClass:[FIROAuthCredential class]
73+
forKey:kCredentialCodingKey];
74+
return [self initWithUser:user additionalUserInfo:additionalUserInfo credential:credential];
6075
}
6176

6277
- (void)encodeWithCoder:(NSCoder *)aCoder {
6378
[aCoder encodeObject:_user forKey:kUserCodingKey];
6479
[aCoder encodeObject:_additionalUserInfo forKey:kAdditionalUserInfoCodingKey];
80+
[aCoder encodeObject:_credential forKey:kCredentialCodingKey];
6581
}
6682

6783
@end

Firebase/Auth/Source/Auth/FIRAuthDataResult_Internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ NS_ASSUME_NONNULL_BEGIN
2525
@param user The signed in user reference.
2626
@param additionalUserInfo The additional user info if available.
2727
*/
28+
- (nullable instancetype)initWithUser:(nullable FIRUser *)user
29+
additionalUserInfo:(nullable FIRAdditionalUserInfo *)additionalUserInfo;
30+
31+
/** @fn initWithUser:additionalUserInfo:
32+
@brief Designated initializer.
33+
@param user The signed in user reference.
34+
@param additionalUserInfo The additional user info if available.
35+
@param credential The updated OAuth credential if available.
36+
*/
2837
- (nullable instancetype)initWithUser:(nullable FIRUser *)user
2938
additionalUserInfo:(nullable FIRAdditionalUserInfo *)additionalUserInfo
39+
credential:(nullable FIROAuthCredential *)credential
3040
NS_DESIGNATED_INITIALIZER;
3141

3242
@end

Firebase/Auth/Source/Backend/FIRAuthBackend.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,13 +1067,8 @@ + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorM
10671067
NSString *email;
10681068
if ([response isKindOfClass:[FIRVerifyAssertionResponse class]]) {
10691069
FIRVerifyAssertionResponse *verifyAssertion = (FIRVerifyAssertionResponse *)response;
1070-
if (verifyAssertion.oauthIDToken.length || verifyAssertion.oauthAccessToken.length) {
1071-
credential =
1072-
[[FIROAuthCredential alloc] initWithProviderID:verifyAssertion.providerID
1073-
IDToken:verifyAssertion.oauthIDToken
1074-
accessToken:verifyAssertion.oauthAccessToken
1075-
pendingToken:verifyAssertion.pendingToken];
1076-
}
1070+
credential =
1071+
[[FIROAuthCredential alloc] initWithVerifyAssertionResponse:verifyAssertion];
10771072
email = verifyAssertion.email;
10781073
}
10791074
return [FIRAuthErrorUtils credentialAlreadyInUseErrorWithMessage:serverDetailErrorMessage

Firebase/Auth/Source/Backend/RPC/FIRVerifyAssertionResponse.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ NS_ASSUME_NONNULL_BEGIN
196196
*/
197197
@property(nonatomic, strong, readonly, nullable) NSString *oauthAccessToken;
198198

199+
/** @property oauthSecretToken
200+
@brief The secret for the OpenID OAuth extention.
201+
*/
202+
@property(nonatomic, readonly, nullable) NSString *oauthSecretToken;
203+
199204
/** @property pendingToken
200205
@brief The pending ID Token string.
201206
*/

Firebase/Auth/Source/Backend/RPC/FIRVerifyAssertionResponse.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ - (BOOL)setWithDictionary:(NSDictionary *)dictionary
7676
_oauthExpirationDate = [dictionary[@"oauthExpireIn"] isKindOfClass:[NSString class]] ?
7777
[NSDate dateWithTimeIntervalSinceNow:[dictionary[@"oauthExpireIn"] doubleValue]] : nil;
7878
_oauthAccessToken = [dictionary[@"oauthAccessToken"] copy];
79+
_oauthSecretToken = [dictionary[@"oauthTokenSecret"] copy];
7980
_pendingToken = [dictionary[@"pendingToken"] copy];
8081
return YES;
8182
}

Firebase/Auth/Source/Public/FIRAuthDataResult.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <Foundation/Foundation.h>
1818

1919
@class FIRAdditionalUserInfo;
20+
@class FIROAuthCredential;
2021
@class FIRUser;
2122

2223
NS_ASSUME_NONNULL_BEGIN
@@ -37,7 +38,14 @@ NS_SWIFT_NAME(AuthDataResult)
3738
/** @property user
3839
@brief The signed in user.
3940
*/
40-
@property(nonatomic, readonly) FIRUser *user;
41+
@property(nonatomic, readonly, nullable) FIRUser *user;
42+
43+
/** @property credential
44+
@brief The updated OAuth credential after the the sign-in, link and reauthenticate action.
45+
@detial This property is for OAuth sign in only.
46+
*/
47+
@property(nonatomic, readonly, nullable) FIROAuthCredential *credential;
48+
4149

4250
/** @property additionalUserInfo
4351
@brief If available contains the additional IdP specific information about signed in user.

Firebase/Auth/Source/Public/FIROAuthCredential.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ NS_SWIFT_NAME(OAuthCredential)
3636
*/
3737
@property(nonatomic, readonly, nullable) NSString *accessToken;
3838

39+
/** @property secret
40+
@brief The secret associated with this credential. This will be nil for OAuth 2.0 providers.
41+
@detail OAuthCredential already exposes a providerId getter. This will help the developer
42+
determine whether an access token/secret pair is needed.
43+
*/
44+
@property(nonatomic, readonly, nullable) NSString *secret;
45+
3946
/** @fn init
4047
@brief This class is not supposed to be instantiated directly.
4148
*/

0 commit comments

Comments
 (0)