Skip to content

Commit 884b531

Browse files
authored
Add GIDAuthorizationFlowProcessor (#282)
1 parent ba223db commit 884b531

File tree

11 files changed

+491
-87
lines changed

11 files changed

+491
-87
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 <Foundation/Foundation.h>
18+
19+
@class GIDSignInInternalOptions;
20+
@class OIDAuthorizationResponse;
21+
22+
NS_ASSUME_NONNULL_BEGIN
23+
24+
/// The protocol to control the authorization flow.
25+
@protocol GIDAuthorizationFlowProcessor <NSObject>
26+
27+
/// The state of the authorization flow.
28+
@property(nonatomic, readonly, getter=isStarted) BOOL start;
29+
30+
/// Starts the authorization flow.
31+
///
32+
/// This method sends authorization request to AppAuth `OIDAuthorizationService` and gets back the
33+
/// response or an error.
34+
///
35+
/// @param options The `GIDSignInInternalOptions` object to provide serverClientID, hostedDomain,
36+
/// clientID, scopes, loginHint and extraParams.
37+
/// @param emmSupport The EMM support info string.
38+
/// @param completion The block that is called on completion asynchronously.
39+
/// authorizationResponse The response from `OIDAuthorizationService`.
40+
/// error The error from `OIDAuthorizationService`.
41+
- (void)startWithOptions:(GIDSignInInternalOptions *)options
42+
emmSupport:(nullable NSString *)emmSupport
43+
completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
44+
NSError *_Nullable error))completion;
45+
46+
/// Handles the custom URL scheme opened by SFSafariViewController and returns control to the
47+
/// client on iOS 10.
48+
///
49+
/// @param url The redirect URL invoked by the server.
50+
/// @return YES if the passed URL matches the expected redirect URL and was consumed, NO otherwise.
51+
- (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url;
52+
53+
/// Cancels the authorization flow.
54+
- (void)cancelAuthenticationFlow;
55+
56+
@end
57+
58+
NS_ASSUME_NONNULL_END
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 <Foundation/Foundation.h>
18+
19+
#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/API/GIDAuthorizationFlowProcessor.h"
20+
21+
@class OIDServiceConfiguration;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
/// Concrete implementation of the protocol `GIDAuthorizationFlowProcessor`.
26+
@interface GIDAuthorizationFlowProcessor : NSObject <GIDAuthorizationFlowProcessor>
27+
28+
@end
29+
30+
NS_ASSUME_NONNULL_END
31+
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)