GIDAuthorizationFlowProcessor.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2023 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 "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
  17. #import "GoogleSignIn/Sources/GIDAuthorizationUtil.h"
  18. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  19. #ifdef SWIFT_PACKAGE
  20. @import AppAuth;
  21. #else
  22. #import <AppAuth/AppAuth.h>
  23. #endif
  24. NS_ASSUME_NONNULL_BEGIN
  25. @interface GIDAuthorizationFlowProcessor ()
  26. /// AppAuth external user-agent session state.
  27. @property(nonatomic, nullable) id<OIDExternalUserAgentSession> currentAuthorizationFlow;
  28. @end
  29. @implementation GIDAuthorizationFlowProcessor
  30. # pragma mark - Public API
  31. - (BOOL)isStarted {
  32. return self.currentAuthorizationFlow != nil;
  33. }
  34. - (void)startWithOptions:(GIDSignInInternalOptions *)options
  35. emmSupport:(nullable NSString *)emmSupport
  36. completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
  37. NSError *_Nullable error))completion {
  38. OIDAuthorizationRequest *request =
  39. [GIDAuthorizationUtil authorizationRequestWithOptions:options
  40. emmSupport:emmSupport];
  41. _currentAuthorizationFlow = [OIDAuthorizationService
  42. presentAuthorizationRequest:request
  43. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  44. presentingViewController:options.presentingViewController
  45. #elif TARGET_OS_OSX
  46. presentingWindow:options.presentingWindow
  47. #endif // TARGET_OS_OSX
  48. callback:^(OIDAuthorizationResponse *authorizationResponse,
  49. NSError *error) {
  50. completion(authorizationResponse, error);
  51. }];
  52. }
  53. - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
  54. if ([self.currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
  55. self.currentAuthorizationFlow = nil;
  56. return YES;
  57. } else {
  58. return NO;
  59. }
  60. }
  61. - (void)cancelAuthenticationFlow {
  62. [self.currentAuthorizationFlow cancel];
  63. self.currentAuthorizationFlow = nil;
  64. }
  65. @end
  66. NS_ASSUME_NONNULL_END