Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion GoogleSignIn/Sources/GIDGoogleUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@implementation GIDGoogleUser

- (instancetype)initWithAuthState:(OIDAuthState *)authState
profileData:(GIDProfileData *)profileData {
profileData:(nullable GIDProfileData *)profileData {
self = [super init];
if (self) {
_authentication = [[GIDAuthentication alloc] initWithAuthState:authState];
Expand Down
2 changes: 1 addition & 1 deletion GoogleSignIn/Sources/GIDGoogleUser_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN

// Create a object with an auth state, scopes, and profile data.
- (instancetype)initWithAuthState:(OIDAuthState *)authState
profileData:(GIDProfileData *)profileData;
profileData:(nullable GIDProfileData *)profileData;

@end

Expand Down
11 changes: 11 additions & 0 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ - (BOOL)hasPreviousSignIn {
return [authState isAuthorized];
}

- (nullable GIDGoogleUser *)restoredGoogleUserFromPreviousSignIn {
OIDAuthState *authState = [self loadAuthState];

if (!authState) {
return nil;
}
Comment thread
Alex-4-Git marked this conversation as resolved.
Resolved

return [[GIDGoogleUser alloc] initWithAuthState:authState
profileData:nil];
}

- (void)restorePreviousSignIn {
[self signInWithOptions:[GIDSignInInternalOptions silentOptions]];
}
Expand Down
4 changes: 4 additions & 0 deletions GoogleSignIn/Sources/GIDSignIn_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

NS_ASSUME_NONNULL_BEGIN

@class GIDGoogleUser;
@class GIDSignInInternalOptions;

// Private |GIDSignIn| methods that are used internally in this SDK and other Google SDKs.
Expand All @@ -29,6 +30,9 @@ NS_ASSUME_NONNULL_BEGIN
// Authenticates with extra options.
- (void)signInWithOptions:(GIDSignInInternalOptions *)options;

// Returns the previous sign-in user in the keychain without refreshing the access token.
- (nullable GIDGoogleUser *)restoredGoogleUserFromPreviousSignIn;

@end

NS_ASSUME_NONNULL_END
31 changes: 31 additions & 0 deletions GoogleSignIn/Tests/Unit/GIDSignInTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
return YES;\
}]

static NSString * const kFakeGaiaID = @"123456789";
static NSString * const kFakeIDToken = @"FakeIDToken";
static NSString * const kClientId = @"FakeClientID";
static NSString * const kDotReversedClientId = @"FakeClientID";
static NSString * const kClientId2 = @"FakeClientID2";
Expand Down Expand Up @@ -347,6 +349,35 @@ - (void)testDefaultScope {
[_signIn startMocking];
}

- (void)testRestoredGoogleUserFromPreviousSignIn_hasPreviousUser {
[[[_authorization expect] andReturn:_authState] authState];
OCMStub([_authState lastTokenResponse]).andReturn(_tokenResponse);
OCMStub([_tokenResponse scope]).andReturn(nil);
OCMStub([_tokenResponse additionalParameters]).andReturn(nil);
OCMStub([_tokenResponse idToken]).andReturn(kFakeIDToken);

id idTokenDecoded = OCMClassMock([OIDIDToken class]);
OCMStub([idTokenDecoded alloc]).andReturn(idTokenDecoded);
OCMStub([idTokenDecoded initWithIDTokenString:OCMOCK_ANY]).andReturn(idTokenDecoded);
OCMStub([idTokenDecoded subject]).andReturn(kFakeGaiaID);

GIDGoogleUser *previousUser = [_signIn restoredGoogleUserFromPreviousSignIn];

[_authorization verify];
[_authState verify];
[_tokenResponse verify];
XCTAssertEqual(previousUser.userID, kFakeGaiaID);
}

- (void)testRestoredGoogleUserFromPreviousSignIn_hasNoPreviousUser {
[[[_authorization expect] andReturn:nil] authState];

GIDGoogleUser *previousUser = [_signIn restoredGoogleUserFromPreviousSignIn];

[_authorization verify];
XCTAssertNil(previousUser);
}

- (void)testHasPreviousSignIn_HasBeenAuthenticated {
[[[_authorization expect] andReturn:_authState] authState];
[[[_authState expect] andReturnValue:[NSNumber numberWithBool:YES]] isAuthorized];
Expand Down