FIRAppCheckToken+APIResponse.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckToken+APIResponse.h"
  17. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
  18. #if __has_include(<FBLPromises/FBLPromises.h>)
  19. #import <FBLPromises/FBLPromises.h>
  20. #else
  21. #import "FBLPromises.h"
  22. #endif
  23. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  24. #import <GoogleUtilities/GULURLSessionDataResponse.h>
  25. @implementation FIRAppCheckToken (APIResponse)
  26. - (nullable instancetype)initWithTokenExchangeResponse:(NSData *)response
  27. requestDate:(NSDate *)requestDate
  28. error:(NSError **)outError {
  29. if (response.length <= 0) {
  30. FIRAppCheckSetErrorToPointer(
  31. [FIRAppCheckErrorUtil errorWithFailureReason:@"Empty server response body."], outError);
  32. return nil;
  33. }
  34. NSError *JSONError;
  35. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:response
  36. options:0
  37. error:&JSONError];
  38. if (![responseDict isKindOfClass:[NSDictionary class]]) {
  39. FIRAppCheckSetErrorToPointer([FIRAppCheckErrorUtil JSONSerializationError:JSONError], outError);
  40. return nil;
  41. }
  42. return [self initWithResponseDict:responseDict requestDate:requestDate error:outError];
  43. }
  44. - (nullable instancetype)initWithResponseDict:(NSDictionary<NSString *, id> *)responseDict
  45. requestDate:(NSDate *)requestDate
  46. error:(NSError **)outError {
  47. NSString *token = responseDict[@"attestationToken"];
  48. if (![token isKindOfClass:[NSString class]]) {
  49. FIRAppCheckSetErrorToPointer(
  50. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:@"attestationToken"],
  51. outError);
  52. return nil;
  53. }
  54. NSString *timeToLiveString = responseDict[@"ttl"];
  55. if (![token isKindOfClass:[NSString class]] || token.length <= 0) {
  56. FIRAppCheckSetErrorToPointer(
  57. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:@"ttl"], outError);
  58. return nil;
  59. }
  60. // Expect a string like "3600s" representing a time interval in seconds.
  61. NSString *timeToLiveValueString = [timeToLiveString stringByReplacingOccurrencesOfString:@"s"
  62. withString:@""];
  63. NSTimeInterval secondsToLive = timeToLiveValueString.doubleValue;
  64. if (secondsToLive == 0) {
  65. FIRAppCheckSetErrorToPointer(
  66. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:@"ttl"], outError);
  67. return nil;
  68. }
  69. NSDate *expirationDate = [requestDate dateByAddingTimeInterval:secondsToLive];
  70. return [self initWithToken:token expirationDate:expirationDate receivedAtDate:requestDate];
  71. }
  72. @end