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