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
|
|
|
|
|
|
2024-04-12 15:40:49 -04:00
|
|
|
#import <GoogleUtilities/GULUserDefaults.h>
|
|
|
|
|
|
2023-07-21 10:17:20 -04:00
|
|
|
#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"
|
2026-06-09 16:44:19 -04:00
|
|
|
#import "AppCheckCore/Sources/Public/AppCheckCore/_GACAppCheckAPIService.h"
|
2023-05-09 09:51:35 -04:00
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
2023-09-07 10:57:38 -04:00
|
|
|
static NSString *const kDebugTokenEnvKey = @"AppCheckDebugToken";
|
|
|
|
|
static NSString *const kFirebaseDebugTokenEnvKey = @"FIRAAppCheckDebugToken";
|
|
|
|
|
static NSString *const kDebugTokenUserDefaultsKey = @"GACAppCheckDebugToken";
|
2023-05-09 09:51:35 -04:00
|
|
|
|
2026-06-10 11:30:20 -04:00
|
|
|
// The base key for registration status.
|
|
|
|
|
// NOTE: Do not use this key directly. Use `registeredUserDefaultsKeyForServiceName:resourceName:`
|
|
|
|
|
// to obtain the namespaced key.
|
|
|
|
|
static NSString *const kDebugTokenRegisteredUserDefaultsKey = @"GACAppCheckDebugTokenRegistered";
|
|
|
|
|
|
2023-05-09 09:51:35 -04:00
|
|
|
@interface GACAppCheckDebugProvider ()
|
|
|
|
|
@property(nonatomic, readonly) id<GACAppCheckDebugProviderAPIServiceProtocol> APIService;
|
2023-09-07 10:57:38 -04:00
|
|
|
@property(nonatomic, readonly, nullable, copy) NSString *debugTokenEnvValue;
|
2026-06-10 11:30:20 -04:00
|
|
|
@property(nonatomic, readonly, copy) NSString *registeredUserDefaultsKey;
|
|
|
|
|
|
|
|
|
|
+ (NSString *)registeredUserDefaultsKeyForServiceName:(NSString *)serviceName
|
|
|
|
|
resourceName:(NSString *)resourceName;
|
2023-05-09 09:51:35 -04:00
|
|
|
@end
|
|
|
|
|
|
2026-06-10 11:30:20 -04:00
|
|
|
static NSString *_Nullable EnvironmentVariableDebugToken(NSString *registeredUserDefaultsKey);
|
|
|
|
|
|
2023-05-09 09:51:35 -04:00
|
|
|
@implementation GACAppCheckDebugProvider
|
|
|
|
|
|
2026-06-10 11:30:20 -04:00
|
|
|
- (instancetype)initWithAPIService:(id<GACAppCheckDebugProviderAPIServiceProtocol>)APIService
|
|
|
|
|
serviceName:(NSString *)serviceName
|
|
|
|
|
resourceName:(NSString *)resourceName {
|
2023-05-09 09:51:35 -04:00
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_APIService = APIService;
|
2026-06-10 11:30:20 -04:00
|
|
|
_registeredUserDefaultsKey =
|
|
|
|
|
[[[self class] registeredUserDefaultsKeyForServiceName:serviceName
|
|
|
|
|
resourceName:resourceName] copy];
|
|
|
|
|
_debugTokenEnvValue = EnvironmentVariableDebugToken(_registeredUserDefaultsKey);
|
2023-05-09 09:51:35 -04:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 15:50:34 -04:00
|
|
|
- (instancetype)initWithServiceName:(NSString *)serviceName
|
|
|
|
|
resourceName:(NSString *)resourceName
|
2023-08-31 12:18:47 -04:00
|
|
|
baseURL:(nullable NSString *)baseURL
|
2023-09-07 15:15:43 -04:00
|
|
|
APIKey:(NSString *)APIKey
|
2023-07-25 15:50:34 -04:00
|
|
|
requestHooks:(nullable NSArray<GACAppCheckAPIRequestHook> *)requestHooks {
|
2023-05-09 09:51:35 -04:00
|
|
|
NSURLSession *URLSession = [NSURLSession
|
|
|
|
|
sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
|
|
|
|
|
|
2026-06-09 16:44:19 -04:00
|
|
|
_GACAppCheckAPIService *APIService =
|
|
|
|
|
[[_GACAppCheckAPIService alloc] initWithURLSession:URLSession
|
|
|
|
|
baseURL:baseURL
|
|
|
|
|
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
|
|
|
|
2026-06-10 11:30:20 -04:00
|
|
|
return [self initWithAPIService:debugAPIService
|
|
|
|
|
serviceName:serviceName
|
|
|
|
|
resourceName:resourceName];
|
2023-05-09 09:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)currentDebugToken {
|
2023-09-07 10:57:38 -04:00
|
|
|
if (self.debugTokenEnvValue) {
|
|
|
|
|
return self.debugTokenEnvValue;
|
2023-05-09 09:51:35 -04:00
|
|
|
} else {
|
|
|
|
|
return [self localDebugToken];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString *)localDebugToken {
|
2023-09-07 10:57:38 -04:00
|
|
|
return LocalDebugToken();
|
2023-05-09 09:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - GACAppCheckProvider
|
|
|
|
|
|
2023-08-30 14:22:33 -04:00
|
|
|
- (void)getTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable, NSError *_Nullable))handler {
|
2023-08-22 12:56:00 -04:00
|
|
|
[self getTokenWithLimitedUse:NO completion:handler];
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 14:22:33 -04:00
|
|
|
- (void)getLimitedUseTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable,
|
2023-08-22 12:56:00 -04:00
|
|
|
NSError *_Nullable))handler {
|
|
|
|
|
[self getTokenWithLimitedUse:YES completion:handler];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Internal
|
|
|
|
|
|
|
|
|
|
- (void)getTokenWithLimitedUse:(BOOL)limitedUse
|
|
|
|
|
completion:(void (^)(GACAppCheckToken *_Nullable token,
|
|
|
|
|
NSError *_Nullable error))handler {
|
2023-05-09 09:51:35 -04:00
|
|
|
[FBLPromise do:^NSString * {
|
|
|
|
|
return [self currentDebugToken];
|
|
|
|
|
}]
|
|
|
|
|
.then(^FBLPromise<GACAppCheckToken *> *(NSString *debugToken) {
|
2023-08-22 12:56:00 -04:00
|
|
|
return [self.APIService appCheckTokenWithDebugToken:debugToken limitedUse:limitedUse];
|
2023-05-09 09:51:35 -04:00
|
|
|
})
|
|
|
|
|
.then(^id(GACAppCheckToken *appCheckToken) {
|
2026-06-10 11:30:20 -04:00
|
|
|
[[GULUserDefaults standardUserDefaults] setBool:YES forKey:self.registeredUserDefaultsKey];
|
2023-05-09 09:51:35 -04:00
|
|
|
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);
|
2026-06-10 11:30:20 -04:00
|
|
|
if (error.code != GACAppCheckErrorCodeServerUnreachable) {
|
|
|
|
|
[[GULUserDefaults standardUserDefaults]
|
|
|
|
|
removeObjectForKey:self.registeredUserDefaultsKey];
|
|
|
|
|
}
|
2023-05-09 09:51:35 -04:00
|
|
|
handler(nil, error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 17:36:04 -04:00
|
|
|
static NSString *LocalDebugToken(void) {
|
2023-09-07 10:57:38 -04:00
|
|
|
return StoredDebugToken() ?: GenerateAndStoreDebugToken();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 17:36:04 -04:00
|
|
|
static NSString *_Nullable StoredDebugToken(void) {
|
2024-04-12 15:40:49 -04:00
|
|
|
return [[GULUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey];
|
2023-09-07 10:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
2023-09-08 17:36:04 -04:00
|
|
|
static NSString *GenerateAndStoreDebugToken(void) {
|
2023-09-07 10:57:38 -04:00
|
|
|
NSString *token = [NSUUID UUID].UUIDString;
|
2024-04-12 15:40:49 -04:00
|
|
|
[[GULUserDefaults standardUserDefaults] setObject:token forKey:kDebugTokenUserDefaultsKey];
|
2023-09-07 10:57:38 -04:00
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 11:30:20 -04:00
|
|
|
+ (NSString *)registeredUserDefaultsKeyForServiceName:(NSString *)serviceName
|
|
|
|
|
resourceName:(NSString *)resourceName {
|
|
|
|
|
NSString *safeServiceName = serviceName.length > 0 ? serviceName : @"default";
|
|
|
|
|
NSString *safeResourceName = [resourceName stringByReplacingOccurrencesOfString:@"/"
|
|
|
|
|
withString:@"_"];
|
|
|
|
|
if (safeResourceName.length == 0) {
|
|
|
|
|
safeResourceName = @"default";
|
|
|
|
|
}
|
|
|
|
|
return [NSString stringWithFormat:@"%@_%@_%@", kDebugTokenRegisteredUserDefaultsKey,
|
|
|
|
|
safeServiceName, safeResourceName];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static NSString *_Nullable EnvironmentVariableDebugToken(NSString *registeredUserDefaultsKey) {
|
2023-09-07 10:57:38 -04:00
|
|
|
NSDictionary<NSString *, NSString *> *environment = [[NSProcessInfo processInfo] environment];
|
|
|
|
|
NSString *envVariableValue = environment[kDebugTokenEnvKey];
|
|
|
|
|
NSString *firebaseEnvVariableValue = environment[kFirebaseDebugTokenEnvKey];
|
|
|
|
|
if (envVariableValue.length == 0) {
|
|
|
|
|
envVariableValue = nil;
|
|
|
|
|
}
|
|
|
|
|
if (firebaseEnvVariableValue == 0) {
|
|
|
|
|
firebaseEnvVariableValue = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (envVariableValue && firebaseEnvVariableValue) {
|
|
|
|
|
GACAppCheckLog(
|
|
|
|
|
GACLoggerAppCheckMessageDebugProviderFirebaseEnvironmentVariable,
|
|
|
|
|
GACAppCheckLogLevelWarning,
|
|
|
|
|
[NSString stringWithFormat:@"The environment variables %@ and %@ are both set; using the "
|
|
|
|
|
@"debug token specified in %@ and ignoring the value of %@.",
|
|
|
|
|
kDebugTokenEnvKey, kFirebaseDebugTokenEnvKey, kDebugTokenEnvKey,
|
|
|
|
|
kFirebaseDebugTokenEnvKey]);
|
|
|
|
|
|
|
|
|
|
return envVariableValue;
|
|
|
|
|
} else if (envVariableValue) {
|
|
|
|
|
GACAppCheckLog(
|
|
|
|
|
GACLoggerAppCheckMessageEnvironmentVariableDebugToken, GACAppCheckLogLevelDebug,
|
|
|
|
|
[NSString
|
|
|
|
|
stringWithFormat:@"Using the debug token specified in the environment variable %@.",
|
|
|
|
|
kDebugTokenEnvKey]);
|
|
|
|
|
|
|
|
|
|
return envVariableValue;
|
|
|
|
|
} else if (firebaseEnvVariableValue) {
|
|
|
|
|
// TODO(andrewheard): Update the message to warn that "FIRAAppCheckDebugToken"
|
|
|
|
|
// (kFirebaseDebugTokenEnvKey) is deprecated after Firebase App Check supports
|
|
|
|
|
// "AppCheckDebugToken" (kDebugTokenEnvKey) and increase the severity to
|
|
|
|
|
// GACAppCheckLogLevelWarning.
|
|
|
|
|
GACAppCheckLog(
|
|
|
|
|
GACLoggerAppCheckMessageDebugProviderFirebaseEnvironmentVariable, GACAppCheckLogLevelDebug,
|
|
|
|
|
[NSString
|
|
|
|
|
stringWithFormat:@"Using the debug token specified in the environment variable %@.",
|
|
|
|
|
kFirebaseDebugTokenEnvKey]);
|
|
|
|
|
|
|
|
|
|
return firebaseEnvVariableValue;
|
|
|
|
|
} else {
|
2026-06-10 11:30:20 -04:00
|
|
|
BOOL isRegistered =
|
|
|
|
|
[[GULUserDefaults standardUserDefaults] boolForKey:registeredUserDefaultsKey];
|
|
|
|
|
if (!isRegistered) {
|
|
|
|
|
// Print only a locally generated token to avoid a valid token leak on CI.
|
|
|
|
|
GACAppCheckLog(
|
|
|
|
|
GACLoggerAppCheckMessageLocalDebugToken, GACAppCheckLogLevelWarning,
|
|
|
|
|
[NSString stringWithFormat:@"App Check debug token: '%@'.", LocalDebugToken()]);
|
|
|
|
|
}
|
2023-09-07 10:57:38 -04:00
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 09:51:35 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|