Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIROAuthCredential.h"
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

#import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
#import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
#import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
#import "FirebaseAuth/Sources/AuthProvider/OAuth/FIROAuthCredential_Internal.h"
Expand Down Expand Up @@ -309,6 +310,8 @@ - (void)getHeadFulLiteURLWithEventID:(NSString *)eventID
NSString *apiKey =
strongSelf->_auth.requestConfiguration.APIKey;
NSString *tenantID = strongSelf->_auth.tenantID;
id<FIRAppCheckInterop> appCheck =
strongSelf->_auth.requestConfiguration.appCheck;
NSMutableDictionary *urlArguments = [@{
@"apiKey" : apiKey,
@"authType" : kAuthTypeSignInWithRedirect,
Expand Down Expand Up @@ -345,6 +348,7 @@ - (void)getHeadFulLiteURLWithEventID:(NSString *)eventID
urlArguments[@"hl"] =
strongSelf->_auth.requestConfiguration.languageCode;
}

NSString *argumentsString = [strongSelf
httpArgumentsStringForArgsDictionary:urlArguments];
NSString *URLString;
Expand All @@ -358,16 +362,41 @@ - (void)getHeadFulLiteURLWithEventID:(NSString *)eventID
[NSString stringWithFormat:kHeadfulLiteURLStringFormat,
authDomain, argumentsString];
}
if (completion) {
NSCharacterSet *set =
[NSCharacterSet URLFragmentAllowedCharacterSet];
completion(
[NSURL
URLWithString:
[URLString
stringByAddingPercentEncodingWithAllowedCharacters:
set]],
nil);
NSCharacterSet *set =
[NSCharacterSet URLFragmentAllowedCharacterSet];
NSURLComponents *components = [[NSURLComponents alloc]
initWithString:
[URLString
stringByAddingPercentEncodingWithAllowedCharacters:
set]];
if (appCheck) {
[appCheck
getTokenForcingRefresh:false
completion:^(
id<FIRAppCheckTokenResultInterop> _Nonnull tokenResult) {
if (tokenResult.error) {
FIRLogWarning(
kFIRLoggerAuth, @"I-AUT000018",
@"Error getting App Check token; "
@"using placeholder token "
@"instead. Error: %@",
tokenResult.error);
}
NSString *appCheckTokenFragment = [@"fac="
stringByAppendingString:tokenResult
.token];
[components
setFragment:appCheckTokenFragment];

if (completion) {
completion([components URL], nil);
}
}];

} else {
if (completion) {
completion([components URL], nil);
}
}
}];
}
Expand Down
33 changes: 30 additions & 3 deletions FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRPhoneAuthProvider.h"
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"

#import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
#import "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
#import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
#import "FirebaseAuth/Sources/Backend/FIRAuthBackend+MultiFactor.h"
Expand Down Expand Up @@ -718,6 +719,9 @@ - (void)reCAPTCHAURLWithEventID:(NSString *)eventID completion:(FIRReCAPTCHAURLC
NSString *clientID = self->_auth.app.options.clientID;
NSString *appID = self->_auth.app.options.googleAppID;
NSString *apiKey = self->_auth.requestConfiguration.APIKey;
id<FIRAppCheckInterop> appCheck =
self->_auth.requestConfiguration.appCheck;

NSMutableArray<NSURLQueryItem *> *queryItems = [@[
[NSURLQueryItem queryItemWithName:@"apiKey" value:apiKey],
[NSURLQueryItem queryItemWithName:@"authType"
Expand All @@ -738,7 +742,6 @@ - (void)reCAPTCHAURLWithEventID:(NSString *)eventID completion:(FIRReCAPTCHAURLC
addObject:[NSURLQueryItem queryItemWithName:@"appId"
value:appID]];
}

if (self->_auth.requestConfiguration.languageCode) {
[queryItems
addObject:[NSURLQueryItem
Expand All @@ -752,8 +755,32 @@ - (void)reCAPTCHAURLWithEventID:(NSString *)eventID completion:(FIRReCAPTCHAURLC
[NSString stringWithFormat:kReCAPTCHAURLStringFormat,
authDomain]];
[components setQueryItems:queryItems];
if (completion) {
completion([components URL], nil);
if (appCheck) {
[appCheck
getTokenForcingRefresh:false
completion:^(
id<FIRAppCheckTokenResultInterop> _Nonnull tokenResult) {
if (tokenResult.error) {
FIRLogWarning(
kFIRLoggerAuth, @"I-AUT000018",
@"Error getting App Check token; "
@"using placeholder token "
@"instead. Error: %@",
tokenResult.error);
}
NSString *appCheckTokenFragment = [@"fac="
stringByAppendingString:tokenResult
.token];
[components
setFragment:appCheckTokenFragment];
if (completion) {
completion([components URL], nil);
}
}];
} else {
if (completion) {
completion([components URL], nil);
}
}
}];
}
Expand Down
36 changes: 2 additions & 34 deletions FirebaseAuth/Tests/Unit/FIRAuthBackendRPCImplementationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

@import HeartbeatLoggingTestUtils;

#import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
#import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
#import "FirebaseAuth/Sources/Backend/FIRAuthRPCRequest.h"
#import "FirebaseAuth/Sources/Backend/FIRAuthRPCResponse.h"
#import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
#import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
#import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
#import "FirebaseAuth/Tests/Unit/FIRFakeAppCheck.h"
#import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"

#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
Expand All @@ -43,7 +43,7 @@
/** @var kFakeAppCheckToken
@brief Used as a fakeappCheck token for a fake RPC request.
*/
static NSString *const kFakeAppCheckToken = @"FAKE_APP_CHECK_TOKEN";
static NSString *const kFakeAppCheckToken = @"appCheckToken";

/** @var kFakeFirebaseAppID
@brief Used as a fake Firebase app ID for a fake RPC request. We don't test this here.
Expand Down Expand Up @@ -182,38 +182,6 @@ - (void)log {

@end

#pragma mark - FIRFakeAppCheckResult

/// A fake appCheckResult used for dependency injection during testing.
@interface FIRFakeAppCheckResult : NSObject <FIRAppCheckTokenResultInterop>
@property(nonatomic) NSString *token;
@property(nonatomic, nullable) NSError *error;
@end

@implementation FIRFakeAppCheckResult

@end

#pragma mark - FIRFakeAppCheck

/// A fake appCheck used for dependency injection during testing.
@interface FIRFakeAppCheck : NSObject <FIRAppCheckInterop>
@property(nonatomic, nonnull, readwrite, copy) NSString *tokenDidChangeNotificationName;
@property(nonatomic, nonnull, readwrite, copy) NSString *notificationAppNameKey;
@property(nonatomic, nonnull, readwrite, copy) NSString *notificationTokenKey;
@end

@implementation FIRFakeAppCheck

- (void)getTokenForcingRefresh:(BOOL)forcingRefresh
completion:(nonnull FIRAppCheckTokenHandlerInterop)completion {
FIRFakeAppCheckResult *fakeAppCheckResult = [[FIRFakeAppCheckResult alloc] init];
fakeAppCheckResult.token = kFakeAppCheckToken;
completion(fakeAppCheckResult);
}

@end

#pragma mark - FIRFakeRequest

/** @class FIRFakeRequest
Expand Down
51 changes: 51 additions & 0 deletions FirebaseAuth/Tests/Unit/FIRFakeAppCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Foundation/Foundation.h>
#import "FirebaseAppCheck/Interop/FIRAppCheckInterop.h"
#import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRFakeAppCheck : NSObject <FIRAppCheckInterop>

/** @property tokenDidChangeNotificationName
@brief A notification with the specified name is sent to the default notification center
(`NotificationCenter.default`) each time a Firebase app check token is refreshed.
*/
@property(nonatomic, nonnull, readwrite, copy) NSString *tokenDidChangeNotificationName;

/** @property notificationAppNameKey
@brief `userInfo` key for the FAC token in a notification for `tokenDidChangeNotificationName`.
*/
@property(nonatomic, nonnull, readwrite, copy) NSString *notificationAppNameKey;

/** @property notificationAppNameKey
@brief `userInfo` key for the `FirebaseApp.name` in a notification for
`tokenDidChangeNotificationName`.
*/
@property(nonatomic, nonnull, readwrite, copy) NSString *notificationTokenKey;

/** @fn getTokenForcingRefresh:completion:
@brief A fake appCheck used for dependency injection during testing.
@param forcingRefresh dtermines if a new token is generated.
@param handler to update the cache.
*/
- (void)getTokenForcingRefresh:(BOOL)forcingRefresh
completion:(nonnull FIRAppCheckTokenHandlerInterop)handler;

@end

NS_ASSUME_NONNULL_END
46 changes: 46 additions & 0 deletions FirebaseAuth/Tests/Unit/FIRFakeAppCheck.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "FirebaseAuth/Tests/Unit/FIRFakeAppCheck.h"
#import <Foundation/Foundation.h>
#import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"

/** @var kFakeAppCheckToken
@brief A fake App Check token.
*/
static NSString *const kFakeAppCheckToken = @"appCheckToken";

#pragma mark - FIRFakeAppCheckResult

/// A fake appCheckResult used for dependency injection during testing.
@interface FIRFakeAppCheckResult : NSObject <FIRAppCheckTokenResultInterop>
@property(nonatomic) NSString *token;
@property(nonatomic, nullable) NSError *error;
@end

@implementation FIRFakeAppCheckResult

@end

@implementation FIRFakeAppCheck

- (void)getTokenForcingRefresh:(BOOL)forcingRefresh
completion:(nonnull FIRAppCheckTokenHandlerInterop)completion {
FIRFakeAppCheckResult *fakeAppCheckResult = [[FIRFakeAppCheckResult alloc] init];
fakeAppCheckResult.token = kFakeAppCheckToken;
completion(fakeAppCheckResult);
}

@end
Loading