GIDAppAuthFetcherAuthorizationWithEMMSupport.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2022 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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  18. #import "GoogleSignIn/Sources/GIDAppAuthFetcherAuthorizationWithEMMSupport.h"
  19. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  20. #ifdef SWIFT_PACKAGE
  21. @import AppAuth;
  22. @import GTMAppAuth;
  23. #else
  24. #import <AppAuth/AppAuth.h>
  25. #import <GTMAppAuth/GTMAppAuth.h>
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. // The specialized GTMAppAuthFetcherAuthorization delegate that handles potential EMM error
  29. // responses.
  30. @interface GIDAppAuthFetcherAuthorizationEMMChainedDelegate : NSObject
  31. // Initializes with chained delegate and selector.
  32. - (instancetype)initWithDelegate:(id)delegate selector:(SEL)selector;
  33. // The callback method for GTMAppAuthFetcherAuthorization to invoke.
  34. - (void)authentication:(GTMAppAuthFetcherAuthorization *)auth
  35. request:(NSMutableURLRequest *)request
  36. finishedWithError:(nullable NSError *)error;
  37. @end
  38. @implementation GIDAppAuthFetcherAuthorizationEMMChainedDelegate {
  39. // We use a weak reference here to match GTMAppAuthFetcherAuthorization.
  40. __weak id _delegate;
  41. SEL _selector;
  42. // We need to maintain a reference to the chained delegate because GTMAppAuthFetcherAuthorization
  43. // only keeps a weak reference.
  44. GIDAppAuthFetcherAuthorizationEMMChainedDelegate *_retained_self;
  45. }
  46. - (instancetype)initWithDelegate:(id)delegate selector:(SEL)selector {
  47. self = [super init];
  48. if (self) {
  49. _delegate = delegate;
  50. _selector = selector;
  51. _retained_self = self;
  52. }
  53. return self;
  54. }
  55. - (void)authentication:(GTMAppAuthFetcherAuthorization *)auth
  56. request:(NSMutableURLRequest *)request
  57. finishedWithError:(nullable NSError *)error {
  58. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *_Nullable error) {
  59. if (!self->_delegate || !self->_selector) {
  60. return;
  61. }
  62. NSMethodSignature *signature = [self->_delegate methodSignatureForSelector:self->_selector];
  63. if (!signature) {
  64. return;
  65. }
  66. id argument1 = auth;
  67. id argument2 = request;
  68. id argument3 = error;
  69. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  70. [invocation setTarget:self->_delegate]; // index 0
  71. [invocation setSelector:self->_selector]; // index 1
  72. [invocation setArgument:&argument1 atIndex:2];
  73. [invocation setArgument:&argument2 atIndex:3];
  74. [invocation setArgument:&argument3 atIndex:4];
  75. [invocation invoke];
  76. }];
  77. // Prepare to deallocate the chained delegate instance because the above block will retain the
  78. // iVar references it uses.
  79. _retained_self = nil;
  80. }
  81. @end
  82. @implementation GIDAppAuthFetcherAuthorizationWithEMMSupport
  83. #pragma clang diagnostic push
  84. #pragma clang diagnostic ignored "-Wdeprecated-implementations"
  85. - (void)authorizeRequest:(nullable NSMutableURLRequest *)request
  86. delegate:(id)delegate
  87. didFinishSelector:(SEL)sel {
  88. #pragma clang diagnostic pop
  89. GIDAppAuthFetcherAuthorizationEMMChainedDelegate *chainedDelegate =
  90. [[GIDAppAuthFetcherAuthorizationEMMChainedDelegate alloc] initWithDelegate:delegate
  91. selector:sel];
  92. #pragma clang diagnostic push
  93. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  94. [super authorizeRequest:request
  95. delegate:chainedDelegate
  96. didFinishSelector:@selector(authentication:request:finishedWithError:)];
  97. #pragma clang diagnostic pop
  98. }
  99. - (void)authorizeRequest:(nullable NSMutableURLRequest *)request
  100. completionHandler:(GTMAppAuthFetcherAuthorizationCompletion)handler {
  101. [super authorizeRequest:request completionHandler:^(NSError *_Nullable error) {
  102. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *_Nullable error) {
  103. handler(error);
  104. }];
  105. }];
  106. }
  107. @end
  108. NS_ASSUME_NONNULL_END
  109. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST