Skip to content

Commit f7ce004

Browse files
committed
Merge branch 'refactorGSI' into pin-GIDAuthorizationFlowProcessor-fake
2 parents 69aa820 + 2138a3a commit f7ce004

File tree

14 files changed

+558
-349
lines changed

14 files changed

+558
-349
lines changed

GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.m

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
1818

19-
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
20-
21-
#import "GoogleSignIn/Sources/GIDEMMSupport.h"
22-
#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
19+
#import "GoogleSignIn/Sources/GIDAuthorizationUtil.h"
2320
#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
24-
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
2521

2622
#ifdef SWIFT_PACKAGE
2723
@import AppAuth;
@@ -31,20 +27,10 @@
3127

3228
NS_ASSUME_NONNULL_BEGIN
3329

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-
4130
@interface GIDAuthorizationFlowProcessor ()
4231

4332
/// AppAuth external user-agent session state.
44-
@property(nonatomic, nullable)id<OIDExternalUserAgentSession> currentAuthorizationFlow;
45-
46-
/// AppAuth configuration object.
47-
@property(nonatomic)OIDServiceConfiguration *appAuthConfiguration;
33+
@property(nonatomic, nullable) id<OIDExternalUserAgentSession> currentAuthorizationFlow;
4834

4935
@end
5036

@@ -60,47 +46,9 @@ - (void)startWithOptions:(GIDSignInInternalOptions *)options
6046
emmSupport:(nullable NSString *)emmSupport
6147
completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
6248
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];
9749
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];
50+
[GIDAuthorizationUtil authorizationRequestWithOptions:options
51+
emmSupport:emmSupport];
10452

10553
_currentAuthorizationFlow = [OIDAuthorizationService
10654
presentAuthorizationRequest:request
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 OIDAuthorizationRequest;
20+
@class GIDSignInInternalOptions;
21+
22+
NS_ASSUME_NONNULL_BEGIN
23+
24+
/// The util class for authorization process.
25+
@interface GIDAuthorizationUtil : NSObject
26+
27+
/// Creates the request to AppAuth to start the authorization flow.
28+
///
29+
/// @param options The `GIDSignInInternalOptions` object to provide serverClientID, hostedDomain,
30+
/// clientID, scopes, loginHint and extraParams.
31+
/// @param emmSupport The EMM support info string.
32+
+ (OIDAuthorizationRequest *)
33+
authorizationRequestWithOptions:(GIDSignInInternalOptions *)options
34+
emmSupport:(nullable NSString *)emmSupport;
35+
36+
/// Unions granted scopes with new scopes or returns an error if the new scopes are the subset of
37+
/// the granted scopes.
38+
///
39+
/// @param scopes The existing scopes.
40+
/// @param newScopes The new scopes to add.
41+
/// @param error The reference to the error.
42+
/// @return The array of all scopes or nil if there is an error.
43+
+ (nullable NSArray<NSString *> *)
44+
resolvedScopesFromGrantedScoped:(NSArray<NSString *> *)scopes
45+
withNewScopes:(NSArray<NSString *> *)newScopes
46+
error:(NSError * __autoreleasing *)error;
47+
48+
@end
49+
50+
NS_ASSUME_NONNULL_END
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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/GIDAuthorizationUtil.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+
@implementation GIDAuthorizationUtil
35+
36+
+ (OIDAuthorizationRequest *)
37+
authorizationRequestWithOptions:(GIDSignInInternalOptions *)options
38+
emmSupport:(nullable NSString *)emmSupport {
39+
GIDSignInCallbackSchemes *schemes =
40+
[[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
41+
NSString *urlString = [NSString stringWithFormat:@"%@:%@",
42+
[schemes clientIdentifierScheme], kBrowserCallbackPath];
43+
NSURL *redirectURL = [NSURL URLWithString:urlString];
44+
45+
NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
46+
additionalParameters[kIncludeGrantedScopesParameter] = @"true";
47+
if (options.configuration.serverClientID) {
48+
additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
49+
}
50+
if (options.loginHint) {
51+
additionalParameters[kLoginHintParameter] = options.loginHint;
52+
}
53+
if (options.configuration.hostedDomain) {
54+
additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
55+
}
56+
57+
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
58+
[additionalParameters addEntriesFromDictionary:
59+
[GIDEMMSupport parametersWithParameters:options.extraParams
60+
emmSupport:emmSupport
61+
isPasscodeInfoRequired:NO]];
62+
#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
63+
[additionalParameters addEntriesFromDictionary:options.extraParams];
64+
#endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
65+
additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
66+
additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
67+
68+
NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
69+
NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
70+
OIDServiceConfiguration *appAuthConfiguration =
71+
[[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
72+
tokenEndpoint:tokenEndpointURL];
73+
OIDAuthorizationRequest *request =
74+
[[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
75+
clientId:options.configuration.clientID
76+
scopes:options.scopes
77+
redirectURL:redirectURL
78+
responseType:OIDResponseTypeCode
79+
additionalParameters:additionalParameters];
80+
81+
return request;
82+
}
83+
84+
+ (nullable NSArray<NSString *> *)
85+
resolvedScopesFromGrantedScoped:(NSArray<NSString *> *)scopes
86+
withNewScopes:(NSArray<NSString *> *)newScopes
87+
error:(NSError * __autoreleasing *)error {
88+
NSMutableSet<NSString *> *grantedScopes = [NSMutableSet setWithArray:scopes];
89+
NSSet<NSString *> *requestedScopes = [NSSet setWithArray:newScopes];
90+
91+
if ([requestedScopes isSubsetOfSet:grantedScopes]) {
92+
// All requested scopes have already been granted, generate an error.
93+
*error = [NSError errorWithDomain:kGIDSignInErrorDomain
94+
code:kGIDSignInErrorCodeScopesAlreadyGranted
95+
userInfo:nil];
96+
return nil;
97+
}
98+
99+
// Use the union of granted and requested scopes.
100+
[grantedScopes unionSet:requestedScopes];
101+
return [grantedScopes allObjects];
102+
}
103+
104+
@end
105+
106+
NS_ASSUME_NONNULL_END

GoogleSignIn/Sources/GIDGoogleUser.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
static NSString *const kProfileDataKey = @"profileData";
4343
static NSString *const kAuthStateKey = @"authState";
4444

45-
// Parameters for the token exchange endpoint.
46-
static NSString *const kAudienceParameter = @"audience";
4745
static NSString *const kOpenIDRealmParameter = @"openid.realm";
4846

4947
// Additional parameter names for EMM.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
20+
21+
@class GIDProfileData;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
/// The block type providing the response for user info request.
26+
///
27+
/// @param profileData The `GIDProfileData` object returned on success.
28+
/// @param error The error returned on failure.
29+
typedef void (^GIDProfileDataFetcherFakeResponseProvider)(GIDProfileData *_Nullable profileData,
30+
NSError *_Nullable error);
31+
32+
/// The block type setting up the response value.
33+
///
34+
/// @param responseProvider The block which provides the response.
35+
typedef void (^GIDProfileDataFetcherTestBlock)(GIDProfileDataFetcherFakeResponseProvider
36+
responseProvider);
37+
38+
@interface GIDFakeProfileDataFetcher : NSObject <GIDProfileDataFetcher>
39+
40+
/// The test block to set up the response value.
41+
@property(nonatomic) GIDProfileDataFetcherTestBlock testBlock;
42+
43+
@end
44+
45+
NS_ASSUME_NONNULL_END
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
2+
3+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
4+
5+
#ifdef SWIFT_PACKAGE
6+
@import AppAuth;
7+
#else
8+
#import <AppAuth/AppAuth.h>
9+
#endif
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@implementation GIDFakeProfileDataFetcher
14+
15+
- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState
16+
completion:(void (^)(GIDProfileData *_Nullable profileData,
17+
NSError *_Nullable error))completion {
18+
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
19+
self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error) {
20+
completion(profileData, error);
21+
});
22+
}
23+
24+
- (nullable GIDProfileData *)fetchProfileDataWithIDToken:(OIDIDToken *)idToken {
25+
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
26+
__block GIDProfileData *profileDataToReturn;
27+
self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error) {
28+
profileDataToReturn = profileData;
29+
});
30+
return profileDataToReturn;
31+
}
32+
33+
@end
34+
35+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)