|
| 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 | + |
| 17 | +#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h" |
| 18 | + |
| 19 | +#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h" |
| 20 | + |
| 21 | +#import "GoogleSignIn/Sources/GIDEMMSupport.h" |
| 22 | +#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h" |
| 23 | +#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h" |
| 24 | +#import "GoogleSignIn/Sources/GIDSignInPreferences.h" |
| 25 | + |
| 26 | +#ifdef SWIFT_PACKAGE |
| 27 | +@import AppAuth; |
| 28 | +#else |
| 29 | +#import <AppAuth/AppAuth.h> |
| 30 | +#endif |
| 31 | + |
| 32 | +NS_ASSUME_NONNULL_BEGIN |
| 33 | + |
| 34 | +// Parameters for the auth and token exchange endpoints. |
| 35 | +static NSString *const kAudienceParameter = @"audience"; |
| 36 | + |
| 37 | +static NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes"; |
| 38 | +static NSString *const kLoginHintParameter = @"login_hint"; |
| 39 | +static NSString *const kHostedDomainParameter = @"hd"; |
| 40 | + |
| 41 | +@interface GIDAuthorizationFlowProcessor () |
| 42 | + |
| 43 | +/// AppAuth external user-agent session state. |
| 44 | +@property(nonatomic, nullable)id<OIDExternalUserAgentSession> currentAuthorizationFlow; |
| 45 | + |
| 46 | +/// AppAuth configuration object. |
| 47 | +@property(nonatomic)OIDServiceConfiguration *appAuthConfiguration; |
| 48 | + |
| 49 | +@end |
| 50 | + |
| 51 | +@implementation GIDAuthorizationFlowProcessor |
| 52 | + |
| 53 | +# pragma mark - Public API |
| 54 | + |
| 55 | +- (BOOL)isStarted { |
| 56 | + return self.currentAuthorizationFlow != nil; |
| 57 | +} |
| 58 | + |
| 59 | +- (void)startWithOptions:(GIDSignInInternalOptions *)options |
| 60 | + emmSupport:(nullable NSString *)emmSupport |
| 61 | + completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse, |
| 62 | + NSError *_Nullable error))completion { |
| 63 | + GIDSignInCallbackSchemes *schemes = |
| 64 | + [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID]; |
| 65 | + NSString *urlString = [NSString stringWithFormat:@"%@:%@", |
| 66 | + [schemes clientIdentifierScheme], kBrowserCallbackPath]; |
| 67 | + NSURL *redirectURL = [NSURL URLWithString:urlString]; |
| 68 | + |
| 69 | + NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy]; |
| 70 | + additionalParameters[kIncludeGrantedScopesParameter] = @"true"; |
| 71 | + if (options.configuration.serverClientID) { |
| 72 | + additionalParameters[kAudienceParameter] = options.configuration.serverClientID; |
| 73 | + } |
| 74 | + if (options.loginHint) { |
| 75 | + additionalParameters[kLoginHintParameter] = options.loginHint; |
| 76 | + } |
| 77 | + if (options.configuration.hostedDomain) { |
| 78 | + additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain; |
| 79 | + } |
| 80 | + |
| 81 | +#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
| 82 | + [additionalParameters addEntriesFromDictionary: |
| 83 | + [GIDEMMSupport parametersWithParameters:options.extraParams |
| 84 | + emmSupport:emmSupport |
| 85 | + isPasscodeInfoRequired:NO]]; |
| 86 | +#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST |
| 87 | + [additionalParameters addEntriesFromDictionary:options.extraParams]; |
| 88 | +#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST |
| 89 | + additionalParameters[kSDKVersionLoggingParameter] = GIDVersion(); |
| 90 | + additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment(); |
| 91 | + |
| 92 | + NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL]; |
| 93 | + NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL]; |
| 94 | + OIDServiceConfiguration *appAuthConfiguration = |
| 95 | + [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL |
| 96 | + tokenEndpoint:tokenEndpointURL]; |
| 97 | + OIDAuthorizationRequest *request = |
| 98 | + [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration |
| 99 | + clientId:options.configuration.clientID |
| 100 | + scopes:options.scopes |
| 101 | + redirectURL:redirectURL |
| 102 | + responseType:OIDResponseTypeCode |
| 103 | + additionalParameters:additionalParameters]; |
| 104 | + |
| 105 | + _currentAuthorizationFlow = [OIDAuthorizationService |
| 106 | + presentAuthorizationRequest:request |
| 107 | +#if TARGET_OS_IOS || TARGET_OS_MACCATALYST |
| 108 | + presentingViewController:options.presentingViewController |
| 109 | +#elif TARGET_OS_OSX |
| 110 | + presentingWindow:options.presentingWindow |
| 111 | +#endif // TARGET_OS_OSX |
| 112 | + callback:^(OIDAuthorizationResponse *authorizationResponse, |
| 113 | + NSError *error) { |
| 114 | + completion(authorizationResponse, error); |
| 115 | + }]; |
| 116 | +} |
| 117 | + |
| 118 | +- (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url { |
| 119 | + if ([self.currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) { |
| 120 | + self.currentAuthorizationFlow = nil; |
| 121 | + return YES; |
| 122 | + } else { |
| 123 | + return NO; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +- (void)cancelAuthenticationFlow { |
| 128 | + [self.currentAuthorizationFlow cancel]; |
| 129 | + self.currentAuthorizationFlow = nil; |
| 130 | +} |
| 131 | + |
| 132 | +@end |
| 133 | + |
| 134 | +NS_ASSUME_NONNULL_END |
0 commit comments