FIRAppCheckToken+APIResponse.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. static NSString *const kResponseFieldToken = @"token";
  26. static NSString *const kResponseFieldTTL = @"ttl";
  27. @implementation FIRAppCheckToken (APIResponse)
  28. - (nullable instancetype)initWithTokenExchangeResponse:(NSData *)response
  29. requestDate:(NSDate *)requestDate
  30. error:(NSError **)outError {
  31. if (response.length <= 0) {
  32. FIRAppCheckSetErrorToPointer(
  33. [FIRAppCheckErrorUtil errorWithFailureReason:@"Empty server response body."], outError);
  34. return nil;
  35. }
  36. NSError *JSONError;
  37. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:response
  38. options:0
  39. error:&JSONError];
  40. if (![responseDict isKindOfClass:[NSDictionary class]]) {
  41. FIRAppCheckSetErrorToPointer([FIRAppCheckErrorUtil JSONSerializationError:JSONError], outError);
  42. return nil;
  43. }
  44. return [self initWithResponseDict:responseDict requestDate:requestDate error:outError];
  45. }
  46. - (nullable instancetype)initWithResponseDict:(NSDictionary<NSString *, id> *)responseDict
  47. requestDate:(NSDate *)requestDate
  48. error:(NSError **)outError {
  49. NSString *token = responseDict[kResponseFieldToken];
  50. if (![token isKindOfClass:[NSString class]]) {
  51. FIRAppCheckSetErrorToPointer(
  52. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:kResponseFieldToken],
  53. outError);
  54. return nil;
  55. }
  56. NSString *timeToLiveString = responseDict[kResponseFieldTTL];
  57. if (![token isKindOfClass:[NSString class]] || token.length <= 0) {
  58. FIRAppCheckSetErrorToPointer(
  59. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:kResponseFieldTTL],
  60. outError);
  61. return nil;
  62. }
  63. // Expect a string like "3600s" representing a time interval in seconds.
  64. NSString *timeToLiveValueString = [timeToLiveString stringByReplacingOccurrencesOfString:@"s"
  65. withString:@""];
  66. NSTimeInterval secondsToLive = timeToLiveValueString.doubleValue;
  67. if (secondsToLive == 0) {
  68. FIRAppCheckSetErrorToPointer(
  69. [FIRAppCheckErrorUtil appCheckTokenResponseErrorWithMissingField:kResponseFieldTTL],
  70. outError);
  71. return nil;
  72. }
  73. NSDate *expirationDate = [requestDate dateByAddingTimeInterval:secondsToLive];
  74. return [self initWithToken:token expirationDate:expirationDate receivedAtDate:requestDate];
  75. }
  76. @end