-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle authorization response and return result object. (#426)
- Loading branch information
Showing
14 changed files
with
596 additions
and
11 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
GoogleSignIn/Sources/GIDVerifyAccountDetail/Fake/GIDVerifiedAccountDetailHandlingFake.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 <TargetConditionals.h> | ||
|
||
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiedAccountDetailHandling.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@class GIDVerifiedAccountDetailResult; | ||
@class GIDVerifiableAccountDetail; | ||
@class OIDAuthState; | ||
@class OIDTokenResponse; | ||
|
||
/// A fake implementation of `GIDVerifiedAccountDetailHandling` for testing purposes. | ||
@interface GIDVerifiedAccountDetailHandlingFake : NSObject <GIDVerifiedAccountDetailHandling> | ||
|
||
/// The token response to be updated in the auth state. | ||
@property (nonatomic, nullable) OIDTokenResponse *tokenResponse; | ||
|
||
/// The auth state to be used to refresh tokens. | ||
@property (nonatomic, nullable) OIDAuthState *verifiedAuthState; | ||
|
||
/// The error to be updated in the auth state. | ||
@property (nonatomic, nullable) NSError *error; | ||
|
||
/// A list of verified account details. | ||
@property(nonatomic, copy, readonly) NSArray<GIDVerifiableAccountDetail *> | ||
*verifiedAccountDetails; | ||
|
||
/// Creates an instance conforming to `GIDVerifiedAccountDetailHandling` with the provided | ||
/// token response, auth state, and error. | ||
/// | ||
/// @param tokenResponse The `OIDTokenResponse` instance to update the auth state. | ||
/// @param verifiedAuthState The `OIDAuthState` instance to refresh tokens. | ||
/// @param error Error to indicate failure getting the token response. | ||
- (instancetype)initWithTokenResponse:(nullable OIDTokenResponse *)tokenResponse | ||
verifiedAuthState:(nullable OIDAuthState *)verifiedAuthState | ||
error:(nullable NSError *)error; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
94 changes: 94 additions & 0 deletions
94
GoogleSignIn/Sources/GIDVerifyAccountDetail/Fake/GIDVerifiedAccountDetailHandlingFake.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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 "GoogleSignIn/Sources/GIDVerifyAccountDetail/Fake/GIDVerifiedAccountDetailHandlingFake.h" | ||
|
||
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiableAccountDetail.h" | ||
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiedAccountDetailResult.h" | ||
|
||
#ifdef SWIFT_PACKAGE | ||
@import AppAuth; | ||
#else | ||
#import <AppAuth/OIDAuthState.h> | ||
#import <AppAuth/OIDTokenResponse.h> | ||
#import <AppAuth/OIDScopeUtilities.h> | ||
#endif | ||
|
||
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
|
||
@implementation GIDVerifiedAccountDetailHandlingFake | ||
|
||
- (instancetype)initWithLastTokenResponse:(OIDTokenResponse *)tokenResponse | ||
accountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails | ||
authState:(OIDAuthState *)authState { | ||
self = [super init]; | ||
if (self) { | ||
NSAssert(false, @"This class is only to be used in testing. Do not use."); | ||
} | ||
return self; | ||
} | ||
|
||
- (instancetype)initWithTokenResponse:(nullable OIDTokenResponse *)tokenResponse | ||
verifiedAuthState:(nullable OIDAuthState *)verifiedAuthState | ||
error:(nullable NSError *)error { | ||
self = [super init]; | ||
if (self) { | ||
_tokenResponse = tokenResponse; | ||
_verifiedAuthState = verifiedAuthState; | ||
_error = error; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)refreshTokensWithCompletion:(nullable void (^)(GIDVerifiedAccountDetailResult *, | ||
NSError *))completion { | ||
if (_tokenResponse) { | ||
[self.verifiedAuthState updateWithTokenResponse:_tokenResponse error:nil]; | ||
} else { | ||
[self.verifiedAuthState updateWithAuthorizationError:_error]; | ||
} | ||
|
||
[self updateVerifiedDetailsWithTokenResponse:_tokenResponse]; | ||
|
||
GIDVerifiedAccountDetailResult *result = | ||
[[GIDVerifiedAccountDetailResult alloc] initWithLastTokenResponse:_tokenResponse | ||
accountDetails:_verifiedAccountDetails | ||
authState:_verifiedAuthState]; | ||
completion(result, _error); | ||
} | ||
|
||
- (void)updateVerifiedDetailsWithTokenResponse:(nullable OIDTokenResponse *)response { | ||
if (response) { | ||
NSArray<NSString *> *accountDetailsString = | ||
[OIDScopeUtilities scopesArrayWithString:response.scope]; | ||
|
||
NSMutableArray<GIDVerifiableAccountDetail *> *verifiedAccountDetails = [NSMutableArray array]; | ||
for (NSString *type in accountDetailsString) { | ||
GIDAccountDetailType detailType = [GIDVerifiableAccountDetail detailTypeWithString:type]; | ||
if (detailType != GIDAccountDetailTypeUnknown) { | ||
[verifiedAccountDetails addObject: | ||
[[GIDVerifiableAccountDetail alloc] initWithAccountDetailType:detailType]]; | ||
} | ||
} | ||
_verifiedAccountDetails = [verifiedAccountDetails copy]; | ||
} else { | ||
_verifiedAccountDetails = @[]; | ||
} | ||
} | ||
|
||
@end | ||
|
||
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.