mirror of
https://github.com/google/app-check
synced 2026-11-10 22:05:53 +05:45
Rewrite AppCheckCore in Swift (#111)
Signed-off-by: Nick Cooke <36927374+ncooke3@users.noreply.github.com> Co-authored-by: Nick Cooke <36927374+ncooke3@users.noreply.github.com> Co-authored-by: Nick Cooke <nickcooke@google.com>
This commit is contained in:
215 files changed
+11244
-15058
No files matched your search
@@ -0,0 +1,63 @@
|
||||
// Copyright 2026 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.
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol AppCheckCoreDeviceCheckAPIServiceProtocol: NSObjectProtocol {
|
||||
func appCheckToken(deviceToken: Data, limitedUse: Bool) async throws -> AppCheckCoreToken
|
||||
}
|
||||
|
||||
class AppCheckCoreDeviceCheckAPIService: NSObject, AppCheckCoreDeviceCheckAPIServiceProtocol {
|
||||
private let apiService: AppCheckCoreAPIServiceProtocol
|
||||
private let resourceName: String
|
||||
|
||||
init(apiService: AppCheckCoreAPIServiceProtocol, resourceName: String) {
|
||||
self.apiService = apiService
|
||||
self.resourceName = resourceName
|
||||
super.init()
|
||||
}
|
||||
|
||||
func appCheckToken(deviceToken: Data, limitedUse: Bool) async throws -> AppCheckCoreToken {
|
||||
guard !deviceToken.isEmpty else {
|
||||
throw AppCheckCoreErrorUtil.error(withFailureReason: "DeviceCheck token must not be empty.")
|
||||
}
|
||||
|
||||
let base64EncodedToken = deviceToken.base64EncodedString()
|
||||
let payload: [String: Any] = [
|
||||
"device_token": base64EncodedToken,
|
||||
"limited_use": limitedUse,
|
||||
]
|
||||
|
||||
let payloadJSON: Data
|
||||
do {
|
||||
payloadJSON = try JSONSerialization.data(withJSONObject: payload)
|
||||
} catch {
|
||||
throw AppCheckCoreErrorUtil.jsonSerializationError(error)
|
||||
}
|
||||
|
||||
let urlString = "\(apiService.baseURL)/\(resourceName):exchangeDeviceCheckToken"
|
||||
guard let url = URL(string: urlString) else {
|
||||
throw AppCheckCoreErrorUtil.error(withFailureReason: "Invalid URL.")
|
||||
}
|
||||
|
||||
let response = try await apiService.sendRequest(
|
||||
withURL: url,
|
||||
httpMethod: "POST",
|
||||
body: payloadJSON,
|
||||
additionalHeaders: ["Content-Type": "application/json"]
|
||||
)
|
||||
|
||||
return try apiService.appCheckToken(withAPIResponse: response)
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@class FBLPromise<Result>;
|
||||
@class GACAppCheckToken;
|
||||
@protocol _GACAppCheckAPIServiceProtocol;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol GACDeviceCheckAPIServiceProtocol <NSObject>
|
||||
|
||||
- (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken
|
||||
limitedUse:(BOOL)limitedUse;
|
||||
|
||||
@end
|
||||
|
||||
@interface GACDeviceCheckAPIService : NSObject <GACDeviceCheckAPIServiceProtocol>
|
||||
|
||||
/// Default initializer.
|
||||
/// @param APIService An instance implementing `_GACAppCheckAPIServiceProtocol` to be used to send
|
||||
/// network requests to the App Check backend.
|
||||
/// @param resourceName The name of the resource protected by App Check; for a Firebase App this is
|
||||
/// "projects/{project_id}/apps/{app_id}". See https://google.aip.dev/122 for more details about
|
||||
/// resource names.
|
||||
- (instancetype)initWithAPIService:(id<_GACAppCheckAPIServiceProtocol>)APIService
|
||||
resourceName:(NSString *)resourceName;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,106 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#import "AppCheckCore/Sources/DeviceCheckProvider/API/GACDeviceCheckAPIService.h"
|
||||
|
||||
#if __has_include(<FBLPromises/FBLPromises.h>)
|
||||
#import <FBLPromises/FBLPromises.h>
|
||||
#else
|
||||
#import "FBLPromises.h"
|
||||
#endif
|
||||
|
||||
#import "AppCheckCore/Sources/Core/APIService/GACAppCheckToken+APIResponse.h"
|
||||
#import "AppCheckCore/Sources/Public/AppCheckCore/_GACAppCheckAPIService.h"
|
||||
|
||||
#import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
|
||||
#import "AppCheckCore/Sources/Public/AppCheckCore/_GACAppCheckErrorUtil.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
static NSString *const kContentTypeKey = @"Content-Type";
|
||||
static NSString *const kJSONContentType = @"application/json";
|
||||
static NSString *const kDeviceTokenField = @"device_token";
|
||||
static NSString *const kLimitedUseField = @"limited_use";
|
||||
|
||||
@interface GACDeviceCheckAPIService ()
|
||||
|
||||
@property(nonatomic, readonly) id<_GACAppCheckAPIServiceProtocol> APIService;
|
||||
|
||||
@property(nonatomic, readonly) NSString *resourceName;
|
||||
|
||||
@end
|
||||
|
||||
@implementation GACDeviceCheckAPIService
|
||||
|
||||
- (instancetype)initWithAPIService:(id<_GACAppCheckAPIServiceProtocol>)APIService
|
||||
resourceName:(NSString *)resourceName {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_APIService = APIService;
|
||||
_resourceName = resourceName;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - Public API
|
||||
|
||||
- (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken
|
||||
limitedUse:(BOOL)limitedUse {
|
||||
NSString *URLString = [NSString stringWithFormat:@"%@/%@:exchangeDeviceCheckToken",
|
||||
self.APIService.baseURL, self.resourceName];
|
||||
NSURL *URL = [NSURL URLWithString:URLString];
|
||||
|
||||
return [self HTTPBodyWithDeviceToken:deviceToken limitedUse:limitedUse]
|
||||
.then(^FBLPromise<_GACURLSessionDataResponse *> *(NSData *HTTPBody) {
|
||||
return [self.APIService sendRequestWithURL:URL
|
||||
HTTPMethod:@"POST"
|
||||
body:HTTPBody
|
||||
additionalHeaders:@{kContentTypeKey : kJSONContentType}];
|
||||
})
|
||||
.then(^id _Nullable(_GACURLSessionDataResponse *_Nullable response) {
|
||||
return [self.APIService appCheckTokenWithAPIResponse:response];
|
||||
});
|
||||
}
|
||||
|
||||
- (FBLPromise<NSData *> *)HTTPBodyWithDeviceToken:(NSData *)deviceToken
|
||||
limitedUse:(BOOL)limitedUse {
|
||||
if (deviceToken.length <= 0) {
|
||||
FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
|
||||
[rejectedPromise reject:[_GACAppCheckErrorUtil
|
||||
errorWithFailureReason:@"DeviceCheck token must not be empty."]];
|
||||
return rejectedPromise;
|
||||
}
|
||||
|
||||
NSString *base64EncodedToken = [deviceToken base64EncodedStringWithOptions:0];
|
||||
|
||||
NSError *encodingError;
|
||||
NSData *payloadJSON = [NSJSONSerialization
|
||||
dataWithJSONObject:@{kDeviceTokenField : base64EncodedToken, kLimitedUseField : @(limitedUse)}
|
||||
options:0
|
||||
error:&encodingError];
|
||||
|
||||
FBLPromise<NSData *> *payloadPromise = [FBLPromise pendingPromise];
|
||||
if (payloadJSON != nil) {
|
||||
[payloadPromise fulfill:payloadJSON];
|
||||
} else {
|
||||
[payloadPromise reject:[_GACAppCheckErrorUtil JSONSerializationError:encodingError]];
|
||||
}
|
||||
return payloadPromise;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
Reference in new issue
Block a user