Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start the incremental authorization flow by creating OIDAuthorizationRequest. #406

Merged
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
54df794
Create method verifyAccountDetailsInteractivelyWithOptions.
brnnmrls Apr 17, 2024
5896985
Move configuration creation methods to GIDConfiguration from GIDSignIn.
brnnmrls Apr 18, 2024
94a4175
Implement default options creation methods.
brnnmrls Apr 18, 2024
4c2a643
Options validation.
brnnmrls Apr 18, 2024
78e175d
Add config initializers.
brnnmrls Apr 25, 2024
731d3a8
Merge branch 'briannamorales/vwg-flow' into briannamorales/optionsVal…
brnnmrls Apr 25, 2024
a5b9a61
Fix spacing.
brnnmrls Apr 25, 2024
d70400a
Add import to use GIDAccountDetailTypeAgeOver18.
brnnmrls Apr 25, 2024
f858d20
Import GIDConfiguration to test file.
brnnmrls Apr 25, 2024
4a97c00
Create OIDAuthorizationRequest.
brnnmrls Apr 29, 2024
c4d0a16
Add tests for config initializers.
brnnmrls Apr 30, 2024
e04a1ea
Add in initializer with config tests.
brnnmrls May 1, 2024
93fb453
Make used variables properties.
brnnmrls May 2, 2024
8441f85
Merge branch 'briannamorales/optionsValidation' into briannamorales/p…
brnnmrls May 2, 2024
066fa07
Add TODO for request/response handling tests.
brnnmrls May 2, 2024
dc6560b
Exclude MacOS for `GIDEMMSupport` use.
brnnmrls May 3, 2024
fa7845c
Silence unused 'request' variable for now until response is implemented.
brnnmrls May 3, 2024
8bfd659
Add tests for config initializer and current user exception.
brnnmrls May 7, 2024
9376a4b
Create fake main bundle initializer and modify tests.
brnnmrls May 8, 2024
4cebc05
Remove Emm flow support and move GIDSignIn strings to avoid repeated …
brnnmrls May 8, 2024
51c5a36
Unblock testing in GIDVerifyAccountDetailTest by changing GIDSignInTe…
brnnmrls May 8, 2024
6ce9f10
Address formatting and indicate designated initializer in GIDFakeMain…
brnnmrls May 9, 2024
4d981eb
Add stopFaking calls in appropriate tests.
brnnmrls May 9, 2024
b744a4b
Fix indentation and remove parameter from `additionalParameters`.
brnnmrls May 9, 2024
89b92d3
Let parameter-less `init` be nullable and require `initWithConfig` to…
brnnmrls May 10, 2024
78daa0b
Create `GIDSignInConstants` class to hold common constants across the…
brnnmrls May 10, 2024
7112ce5
Fix method description and make `initWithConfig` nullable.
brnnmrls May 10, 2024
241d72e
Merge branch 'briannamorales/optionsValidation' into briannamorales/p…
brnnmrls May 11, 2024
674ec5f
Merge branch 'briannamorales/vwg-flow' into briannamorales/presentAut…
brnnmrls May 14, 2024
aac80b4
Move constants to `GIDSignInConstants` and update doc comments.
brnnmrls May 14, 2024
298d9c4
Update preprocessor directives to exclude TARGET_OS_MACCATALYST.
brnnmrls May 14, 2024
227f3e3
Fix formatting.
brnnmrls May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions GoogleSignIn/Sources/GIDGoogleUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "GoogleSignIn/Sources/GIDEMMSupport.h"
#import "GoogleSignIn/Sources/GIDProfileData_Private.h"
#import "GoogleSignIn/Sources/GIDSignIn_Private.h"
#import "GoogleSignIn/Sources/GIDSignInConstants.h"
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
#import "GoogleSignIn/Sources/GIDToken_Private.h"

Expand All @@ -43,10 +44,6 @@
static NSString *const kProfileDataKey = @"profileData";
static NSString *const kAuthStateKey = @"authState";

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

// Additional parameter names for EMM.
static NSString *const kEMMSupportParameterName = @"emm_support";

Expand Down
9 changes: 1 addition & 8 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"

#import "GoogleSignIn/Sources/GIDEMMSupport.h"
#import "GoogleSignIn/Sources/GIDSignInConstants.h"
#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
#import "GoogleSignIn/Sources/GIDCallbackQueue.h"
Expand Down Expand Up @@ -124,14 +125,6 @@
// The delay before the new sign-in flow can be presented after the existing one is cancelled.
static const NSTimeInterval kPresentationDelayAfterCancel = 1.0;

// Parameters for the auth and token exchange endpoints.
static NSString *const kAudienceParameter = @"audience";
// See b/11669751 .
static NSString *const kOpenIDRealmParameter = @"openid.realm";
static NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
static NSString *const kLoginHintParameter = @"login_hint";
static NSString *const kHostedDomainParameter = @"hd";

// Minimum time to expiration for a restored access token.
static const NSTimeInterval kMinimumRestoredAccessTokenTimeToExpire = 600.0;

Expand Down
25 changes: 25 additions & 0 deletions GoogleSignIn/Sources/GIDSignInConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

// Parameters for the auth and token exchange endpoints.
extern NSString *const kAudienceParameter;
// See b/11669751 .
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
extern NSString *const kOpenIDRealmParameter;
extern NSString *const kIncludeGrantedScopesParameter;
extern NSString *const kLoginHintParameter;
extern NSString *const kHostedDomainParameter;
27 changes: 27 additions & 0 deletions GoogleSignIn/Sources/GIDSignInConstants.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "GoogleSignIn/Sources/GIDSignInConstants.h"

// Parameters for the auth and token exchange endpoints.
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
NSString *const kAudienceParameter = @"audience";
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
// See b/11669751 .
NSString *const kOpenIDRealmParameter = @"openid.realm";
NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
NSString *const kLoginHintParameter = @"login_hint";
NSString *const kHostedDomainParameter = @"hd";
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,67 @@

#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
#import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
#import "GoogleSignIn/Sources/GIDSignInConstants.h"
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"

#if TARGET_OS_IOS
mdmathias marked this conversation as resolved.
Show resolved Hide resolved
@import GTMAppAuth;

@implementation GIDVerifyAccountDetail
#ifdef SWIFT_PACKAGE
@import AppAuth;
@import GTMSessionFetcherCore;
#else
#import <AppAuth/OIDAuthorizationRequest.h>
#import <AppAuth/OIDResponseTypes.h>
#import <AppAuth/OIDServiceConfiguration.h>
#endif

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
mdmathias marked this conversation as resolved.
Show resolved Hide resolved

// The URL template for the authorization endpoint.
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";

// The URL template for the token endpoint.
static NSString *const kTokenURLTemplate = @"https://%@/token";

// Expected path in the URL scheme to be handled.
static NSString *const kBrowserCallbackPath = @"/oauth2callback";

@implementation GIDVerifyAccountDetail {
// AppAuth configuration object.
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
OIDServiceConfiguration *_appAuthConfiguration;
}

- (nullable instancetype)initWithConfig:(GIDConfiguration *)configuration {
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
self = [super init];
if (self) {
_configuration = configuration;

NSString *authorizationEndpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
[GIDSignInPreferences googleAuthorizationServer]];
NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
[GIDSignInPreferences googleTokenServer]];
_appAuthConfiguration = [[OIDServiceConfiguration alloc]
initWithAuthorizationEndpoint:[NSURL URLWithString:authorizationEndpointURL]
tokenEndpoint:[NSURL URLWithString:tokenEndpointURL]];
}
return self;
}

- (nullable instancetype)init {
GIDConfiguration *configuration;
NSBundle *bundle = NSBundle.mainBundle;
if (bundle) {
configuration = [GIDConfiguration configurationFromBundle:bundle];
}

if (!configuration) {
return nil;
}

return [self initWithConfig:configuration];
}

#pragma mark - Public methods

- (void)verifyAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
presentingViewController:(UIViewController *)presentingViewController
Expand All @@ -42,11 +99,6 @@ - (void)verifyAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDet
hint:(nullable NSString *)hint
completion:(nullable void (^)(GIDVerifiedAccountDetailResult *_Nullable verifyResult,
NSError *_Nullable error))completion {
NSBundle *bundle = NSBundle.mainBundle;
if (bundle) {
_configuration = [GIDConfiguration configurationFromBundle:bundle];
}

GIDSignInInternalOptions *options =
[GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
presentingViewController:presentingViewController
Expand All @@ -58,10 +110,103 @@ - (void)verifyAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDet
[self verifyAccountDetailsInteractivelyWithOptions:options];
}

#pragma mark - Authentication flow

- (void)verifyAccountDetailsInteractivelyWithOptions:(GIDSignInInternalOptions *)options {
// TODO(#397): Sanity checks and start the incremental authorization flow.
if (!options.interactive) {
return;
}

// Ensure that a configuration is set.
if (!_configuration) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
return;
}

[self assertValidCurrentUser];

// Explicitly throw exception for missing client ID here. This must come before
// scheme check because schemes rely on reverse client IDs.
[self assertValidParameters:options];

[self assertValidPresentingViewController:options];

// If the application does not support the required URL schemes tell the developer so.
GIDSignInCallbackSchemes *schemes =
[[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
NSArray<NSString *> *unsupportedSchemes = [schemes unsupportedSchemes];
if (unsupportedSchemes.count != 0) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"Your app is missing support for the following URL schemes: %@",
[unsupportedSchemes componentsJoinedByString:@", "]];
}
NSURL *redirectURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@:%@",
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
[schemes clientIdentifierScheme],
kBrowserCallbackPath]];

NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
if (options.configuration.serverClientID) {
additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
}
if (options.loginHint) {
additionalParameters[kLoginHintParameter] = options.loginHint;
}
if (options.configuration.hostedDomain) {
additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
}
additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();

NSMutableArray *scopes;
for (GIDVerifiableAccountDetail *detail in options.accountDetailsToVerify) {
NSString *scopeString = [detail scope];
if (scopeString) {
[scopes addObject:scopeString];
}
}

// TODO(#405): Use request variable to present request and process response.
__unused OIDAuthorizationRequest *request =
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
[[OIDAuthorizationRequest alloc] initWithConfiguration:_appAuthConfiguration
clientId:options.configuration.clientID
scopes:scopes
redirectURL:redirectURL
responseType:OIDResponseTypeCode
additionalParameters:additionalParameters];
}

#pragma mark - Helpers

// Assert that a current user exists.
- (void)assertValidCurrentUser {
if (!GIDSignIn.sharedInstance.currentUser) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"|currentUser| must be set to verify."];
}
}

// Asserts the parameters being valid.
- (void)assertValidParameters:(GIDSignInInternalOptions *)options {
if (![options.configuration.clientID length]) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"You must specify |clientID| in |GIDConfiguration|"];
}
}

// Assert that the presenting view controller has been set.
- (void)assertValidPresentingViewController:(GIDSignInInternalOptions *)options {
if (!options.presentingViewController) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"|presentingViewController| must be set."];
}
}

@end

#endif // TARGET_OS_IOS
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ typedef void (^GIDVerifyCompletion)(GIDVerifiedAccountDetailResult *_Nullable ve
/// The active configuration for this instance of `GIDVerifyAccountDetail`.
@property(nonatomic, nullable) GIDConfiguration *configuration;

/// Initialize a `GIDVerifyAccountDetail` object by specifying all available properties.
///
/// @param config The configuration to be used.
/// @return An initialized `GIDVerifyAccountDetail` instance.
/// Otherwise, `nil` if the configuration is invalid or the initializer fails.
- (nullable instancetype)initWithConfig:(GIDConfiguration *)config
NS_DESIGNATED_INITIALIZER;

/// Starts an interactive verification flow.
///
/// The completion will be called at the end of this process. Any saved verification
Expand Down
30 changes: 25 additions & 5 deletions GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,32 @@
*/
@interface GIDFakeMainBundle : NSObject

/**
* @fn initWithClientID:serverClientID:hostedDomain:openIDRealm:
* @brief Initializes a GIDFakeMainBundle object.
* @param clientID The fake client idenfitier for the app.
* @param serverClientID The fake server client idenfitier for the app.
* @param hostedDomain The fake hosted domain for the app.
* @param openIDRealm The fake OpenID realm for the app.
*/
- (nullable instancetype)initWithClientID:(nullable id)clientID
serverClientID:(nullable id)serverClientID
hostedDomain:(nullable id)hostedDomain
openIDRealm:(nullable id)openIDRealm
NS_DESIGNATED_INITIALIZER;

/**
* @fn startFaking:
* @brief Starts faking [NSBundle mainBundle]
*/
- (void)startFaking;

/**
* @fn startFakingWithClientID:
* @brief Starts faking [NSBundle mainBundle]
* @param clientID The fake client idenfitier for the app.
*/
- (void)startFakingWithClientID:(NSString *)clientID;
- (void)startFakingWithClientID:(nullable NSString *)clientID;

/**
* @fn stopFaking
Expand Down Expand Up @@ -87,9 +107,9 @@
* @param hostedDomain The fake hosted domain for the app.
* @param openIDRealm The fake OpenID realm for the app.
*/
- (void)fakeWithClientID:(id)clientID
serverClientID:(id)serverClientID
hostedDomain:(id)hostedDomain
openIDRealm:(id)openIDRealm;
- (void)fakeWithClientID:(nullable id)clientID
serverClientID:(nullable id)serverClientID
hostedDomain:(nullable id)hostedDomain
openIDRealm:(nullable id)openIDRealm;

@end
Loading
Loading