2023-05-09 09:51:35 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-21 10:17:20 -04:00
|
|
|
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckDebugProvider.h"
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
#if __has_include(<FBLPromises/FBLPromises.h>)
|
|
|
|
|
#import <FBLPromises/FBLPromises.h>
|
|
|
|
|
#else
|
|
|
|
|
#import "FBLPromises.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-07-21 10:17:20 -04:00
|
|
|
#import "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
|
|
|
|
|
#import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
|
|
|
|
|
#import "AppCheckCore/Sources/DebugProvider/API/GACAppCheckDebugProviderAPIService.h"
|
|
|
|
|
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckErrors.h"
|
|
|
|
|
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
2023-05-29 18:42:26 -04:00
|
|
|
// TODO(andrewheard): Parameterize the following Firebase-specific keys.
|
2023-05-11 15:59:50 -04:00
|
|
|
// FIREBASE_APP_CHECK_ONLY_BEGIN
|
2023-05-09 09:51:35 -04:00
|
|
|
static NSString *const kDebugTokenEnvKey = @"FIRAAppCheckDebugToken";
|
|
|
|
|
static NSString *const kDebugTokenUserDefaultsKey = @"FIRAAppCheckDebugToken";
|
2023-05-29 18:42:26 -04:00
|
|
|
// FIREBASE_APP_CHECK_ONLY_END
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
@interface GACAppCheckDebugProvider ()
|
|
|
|
|
@property(nonatomic, readonly) id<GACAppCheckDebugProviderAPIServiceProtocol> APIService;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation GACAppCheckDebugProvider
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithAPIService:(id<GACAppCheckDebugProviderAPIServiceProtocol>)APIService {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_APIService = APIService;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 18:42:26 -04:00
|
|
|
- (instancetype)initWithStorageID:(NSString *)storageID
|
|
|
|
|
resourceName:(NSString *)resourceName
|
|
|
|
|
APIKey:(nullable NSString *)APIKey
|
|
|
|
|
requestHooks:(nullable NSArray<GACAppCheckAPIRequestHook> *)requestHooks {
|
2023-05-09 09:51:35 -04:00
|
|
|
NSURLSession *URLSession = [NSURLSession
|
|
|
|
|
sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
|
|
|
|
|
|
|
|
|
|
GACAppCheckAPIService *APIService =
|
|
|
|
|
[[GACAppCheckAPIService alloc] initWithURLSession:URLSession
|
2023-07-13 13:53:04 -04:00
|
|
|
baseURL:nil
|
2023-05-29 18:42:26 -04:00
|
|
|
APIKey:APIKey
|
|
|
|
|
requestHooks:requestHooks];
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
GACAppCheckDebugProviderAPIService *debugAPIService =
|
|
|
|
|
[[GACAppCheckDebugProviderAPIService alloc] initWithAPIService:APIService
|
2023-05-29 18:42:26 -04:00
|
|
|
resourceName:resourceName];
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
return [self initWithAPIService:debugAPIService];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)currentDebugToken {
|
|
|
|
|
NSString *envVariableValue = [[NSProcessInfo processInfo] environment][kDebugTokenEnvKey];
|
|
|
|
|
if (envVariableValue.length > 0) {
|
|
|
|
|
return envVariableValue;
|
|
|
|
|
} else {
|
|
|
|
|
return [self localDebugToken];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)localDebugToken {
|
|
|
|
|
return [self storedDebugToken] ?: [self generateAndStoreDebugToken];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (nullable NSString *)storedDebugToken {
|
|
|
|
|
return [[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)storeDebugToken:(nullable NSString *)token {
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:token forKey:kDebugTokenUserDefaultsKey];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)generateAndStoreDebugToken {
|
|
|
|
|
NSString *token = [NSUUID UUID].UUIDString;
|
|
|
|
|
[self storeDebugToken:token];
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - GACAppCheckProvider
|
|
|
|
|
|
|
|
|
|
- (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable token,
|
|
|
|
|
NSError *_Nullable error))handler {
|
|
|
|
|
[FBLPromise do:^NSString * {
|
|
|
|
|
return [self currentDebugToken];
|
|
|
|
|
}]
|
|
|
|
|
.then(^FBLPromise<GACAppCheckToken *> *(NSString *debugToken) {
|
|
|
|
|
return [self.APIService appCheckTokenWithDebugToken:debugToken];
|
|
|
|
|
})
|
|
|
|
|
.then(^id(GACAppCheckToken *appCheckToken) {
|
|
|
|
|
handler(appCheckToken, nil);
|
|
|
|
|
return nil;
|
|
|
|
|
})
|
|
|
|
|
.catch(^void(NSError *error) {
|
2023-07-19 16:24:05 -04:00
|
|
|
NSString *logMessage = [NSString
|
|
|
|
|
stringWithFormat:@"Failed to exchange debug token to app check token: %@", error];
|
|
|
|
|
GACAppCheckLogDebug(GACLoggerAppCheckMessageDebugProviderFailedExchange, logMessage);
|
2023-05-09 09:51:35 -04:00
|
|
|
handler(nil, error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|