mirror of
https://github.com/google/app-check
synced 2026-11-10 22:05:53 +05:45
Add generation of limited-use tokens with a TTL of 5 mins (#11)
This commit is contained in:
1 parent
2b623874c9
commit
8d0bdd3f24
20 files changed
+482
-144
No files matched your search
@@ -24,7 +24,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol GACDeviceCheckAPIServiceProtocol <NSObject>
|
||||
|
||||
- (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken;
|
||||
- (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken
|
||||
limitedUse:(BOOL)limitedUse;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ 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 ()
|
||||
|
||||
@@ -58,12 +59,13 @@ static NSString *const kDeviceTokenField = @"device_token";
|
||||
|
||||
#pragma mark - Public API
|
||||
|
||||
- (FBLPromise<GACAppCheckToken *> *)appCheckTokenWithDeviceToken:(NSData *)deviceToken {
|
||||
- (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]
|
||||
return [self HTTPBodyWithDeviceToken:deviceToken limitedUse:limitedUse]
|
||||
.then(^FBLPromise<GULURLSessionDataResponse *> *(NSData *HTTPBody) {
|
||||
return [self.APIService sendRequestWithURL:URL
|
||||
HTTPMethod:@"POST"
|
||||
@@ -75,7 +77,8 @@ static NSString *const kDeviceTokenField = @"device_token";
|
||||
});
|
||||
}
|
||||
|
||||
- (FBLPromise<NSData *> *)HTTPBodyWithDeviceToken:(NSData *)deviceToken {
|
||||
- (FBLPromise<NSData *> *)HTTPBodyWithDeviceToken:(NSData *)deviceToken
|
||||
limitedUse:(BOOL)limitedUse {
|
||||
if (deviceToken.length <= 0) {
|
||||
FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
|
||||
[rejectedPromise reject:[GACAppCheckErrorUtil
|
||||
@@ -83,23 +86,25 @@ static NSString *const kDeviceTokenField = @"device_token";
|
||||
return rejectedPromise;
|
||||
}
|
||||
|
||||
return [FBLPromise onQueue:[self backgroundQueue]
|
||||
do:^id _Nullable {
|
||||
NSString *base64EncodedToken =
|
||||
[deviceToken base64EncodedStringWithOptions:0];
|
||||
return [FBLPromise
|
||||
onQueue:[self backgroundQueue]
|
||||
do:^id _Nullable {
|
||||
NSString *base64EncodedToken = [deviceToken base64EncodedStringWithOptions:0];
|
||||
|
||||
NSError *encodingError;
|
||||
NSData *payloadJSON = [NSJSONSerialization
|
||||
dataWithJSONObject:@{kDeviceTokenField : base64EncodedToken}
|
||||
options:0
|
||||
error:&encodingError];
|
||||
NSError *encodingError;
|
||||
NSData *payloadJSON = [NSJSONSerialization dataWithJSONObject:@{
|
||||
kDeviceTokenField : base64EncodedToken,
|
||||
kLimitedUseField : @(limitedUse)
|
||||
}
|
||||
options:0
|
||||
error:&encodingError];
|
||||
|
||||
if (payloadJSON != nil) {
|
||||
return payloadJSON;
|
||||
} else {
|
||||
return [GACAppCheckErrorUtil JSONSerializationError:encodingError];
|
||||
}
|
||||
}];
|
||||
if (payloadJSON != nil) {
|
||||
return payloadJSON;
|
||||
} else {
|
||||
return [GACAppCheckErrorUtil JSONSerializationError:encodingError];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (dispatch_queue_t)backgroundQueue {
|
||||
|
||||
Reference in new issue
Block a user