google / GoogleSignIn-iOS

Enables iOS and macOS apps to sign in with Google.
https://developers.google.com/identity/sign-in/ios
Apache License 2.0
506 stars 201 forks source link

GIDGoogleUser doesn't look like it will handle EMM support #304

Closed mdmathias closed 1 year ago

mdmathias commented 1 year ago

Currently, GIDGoogleUser provides EMM error handling support by checking with itself if EMM support is provided (i.e., self.emmSupport). This call will notably occur before self.fetcherAuthorizer = authorization in the initializer.

The problem is that the implementation of -[GIDGoogleUser emmSupport] uses self.authState to determine EMM support. self.authState unfortunately, references fetcherAuthorizer.

Therefore, since GIDGoogleUser checks for EMM support prior to fetcherAuthorizer being set, it will always return nil when being checked.

- (OIDAuthState *) authState{
  return ((GTMAppAuthFetcherAuthorization *)self.fetcherAuthorizer).authState;
}

- (nullable NSString *)emmSupport {
  return self.authState.lastAuthorizationResponse
      .request.additionalParameters[kEMMSupportParameterName];
}

- (instancetype)initWithAuthState:(OIDAuthState *)authState
                      profileData:(nullable GIDProfileData *)profileData {
  self = [super init];
  if (self) {
    _tokenRefreshHandlerQueue = [[NSMutableArray alloc] init];
    _profile = profileData;

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
    GTMAppAuthFetcherAuthorization *authorization = self.emmSupport ?
        [[GIDAppAuthFetcherAuthorizationWithEMMSupport alloc] initWithAuthState:authState] :
        [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
    GTMAppAuthFetcherAuthorization *authorization =
        [[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
    authorization.tokenRefreshDelegate = self;
    authorization.authState.stateChangeDelegate = self;
    self.fetcherAuthorizer = authorization;

    [self updateTokensWithAuthState:authState];
  }
  return self;
}