FIRAppAttestAttestationResponse.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2021 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/AppAttestProvider/API/FIRAppAttestAttestationResponse.h"
  17. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckToken+APIResponse.h"
  18. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  19. static NSString *const kResponseFieldAppCheckTokenDict = @"appCheckToken";
  20. static NSString *const kResponseFieldArtifact = @"artifact";
  21. @implementation FIRAppAttestAttestationResponse
  22. - (instancetype)initWithArtifact:(NSData *)artifact token:(FIRAppCheckToken *)token {
  23. self = [super init];
  24. if (self) {
  25. _artifact = artifact;
  26. _token = token;
  27. }
  28. return self;
  29. }
  30. - (nullable instancetype)initWithResponseData:(NSData *)response
  31. requestDate:(NSDate *)requestDate
  32. error:(NSError **)outError {
  33. if (response.length <= 0) {
  34. FIRAppCheckSetErrorToPointer(
  35. [FIRAppCheckErrorUtil
  36. errorWithFailureReason:
  37. @"Failed to parse the initial handshake response. Empty server response body."],
  38. outError);
  39. return nil;
  40. }
  41. NSError *JSONError;
  42. NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:response
  43. options:0
  44. error:&JSONError];
  45. if (![responseDict isKindOfClass:[NSDictionary class]]) {
  46. FIRAppCheckSetErrorToPointer([FIRAppCheckErrorUtil JSONSerializationError:JSONError], outError);
  47. return nil;
  48. }
  49. NSString *artifactBase64String = responseDict[kResponseFieldArtifact];
  50. if (![artifactBase64String isKindOfClass:[NSString class]]) {
  51. FIRAppCheckSetErrorToPointer(
  52. [FIRAppCheckErrorUtil
  53. appAttestAttestationResponseErrorWithMissingField:kResponseFieldArtifact],
  54. outError);
  55. return nil;
  56. }
  57. NSData *artifactData = [[NSData alloc] initWithBase64EncodedString:artifactBase64String
  58. options:0];
  59. if (artifactData == nil) {
  60. FIRAppCheckSetErrorToPointer(
  61. [FIRAppCheckErrorUtil
  62. appAttestAttestationResponseErrorWithMissingField:kResponseFieldArtifact],
  63. outError);
  64. return nil;
  65. }
  66. NSDictionary *appCheckTokenDict = responseDict[kResponseFieldAppCheckTokenDict];
  67. if (![appCheckTokenDict isKindOfClass:[NSDictionary class]]) {
  68. FIRAppCheckSetErrorToPointer(
  69. [FIRAppCheckErrorUtil
  70. appAttestAttestationResponseErrorWithMissingField:kResponseFieldAppCheckTokenDict],
  71. outError);
  72. return nil;
  73. }
  74. FIRAppCheckToken *appCheckToken = [[FIRAppCheckToken alloc] initWithResponseDict:appCheckTokenDict
  75. requestDate:requestDate
  76. error:outError];
  77. if (appCheckToken == nil) {
  78. return nil;
  79. }
  80. return [self initWithArtifact:artifactData token:appCheckToken];
  81. }
  82. @end