FIRAppAttestAttestationResponse.m 3.3 KB

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