diff --git a/GoogleSignIn/Sources/GIDSignInInternalOptions.m b/GoogleSignIn/Sources/GIDSignInInternalOptions.m index 617c0291..b08f6662 100644 --- a/GoogleSignIn/Sources/GIDSignInInternalOptions.m +++ b/GoogleSignIn/Sources/GIDSignInInternalOptions.m @@ -42,7 +42,6 @@ + (instancetype)defaultOptionsWithConfiguration:(nullable GIDConfiguration *)con callback:(nullable void (^)(GIDUserAuth *_Nullable userAuth, NSError *_Nullable error))callback { #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST - GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init]; if (options) { options->_interactive = YES; diff --git a/GoogleSignIn/Sources/GIDToken.m b/GoogleSignIn/Sources/GIDToken.m new file mode 100644 index 00000000..05d3f1dc --- /dev/null +++ b/GoogleSignIn/Sources/GIDToken.m @@ -0,0 +1,58 @@ +/* + * Copyright 2022 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/Public/GoogleSignIn/GIDToken.h" + +#import "GoogleSignIn/Sources/GIDToken_Private.h" + +// Key constants used for encode and decode. +static NSString *const kTokenStringKey = @"tokenString"; +static NSString *const kExpirationDateKey = @"expirationDate"; + +@implementation GIDToken + +- (instancetype)initWithTokenString:(NSString *)tokenString + expirationDate:(NSDate *)expirationDate { + self = [super init]; + if (self) { + _tokenString = tokenString; + _expirationDate = expirationDate; + } + + return self; +} + +#pragma mark - NSSecureCoding + ++ (BOOL)supportsSecureCoding { + return YES; +} + +- (nullable instancetype)initWithCoder:(NSCoder *)decoder { + self = [super init]; + if (self) { + _tokenString = [decoder decodeObjectOfClass:[NSString class] forKey:kTokenStringKey]; + _expirationDate = [decoder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey]; + } + return self; +} + +- (void)encodeWithCoder:(NSCoder *)encoder { + [encoder encodeObject:_tokenString forKey:kTokenStringKey]; + [encoder encodeObject:_expirationDate forKey:kExpirationDateKey]; +} + +@end diff --git a/GoogleSignIn/Sources/GIDToken_Private.h b/GoogleSignIn/Sources/GIDToken_Private.h new file mode 100644 index 00000000..b078cb5a --- /dev/null +++ b/GoogleSignIn/Sources/GIDToken_Private.h @@ -0,0 +1,32 @@ +/* + * Copyright 2022 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/Public/GoogleSignIn/GIDUserAuth.h" + +NS_ASSUME_NONNULL_BEGIN + +// Private |GIDToken| methods that are used in this SDK. +@interface GIDToken () + +// Private initializer for |GIDToken|. +// @param token The token String. +// @param expirationDate The expiration date of the token. +- (instancetype)initWithTokenString:(NSString *)tokenString + expirationDate:(NSDate *)expirationDate; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h index 27367565..25ad9ea9 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h @@ -115,7 +115,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on /// iOS 13+. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController @@ -136,7 +136,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// iOS 13+. /// @param hint An optional hint for the authorization server, for example the user's ID or email /// address, to be prefilled if possible. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)signInWithConfiguration:(GIDConfiguration *)configuration presentingViewController:(UIViewController *)presentingViewController @@ -176,7 +176,7 @@ typedef void (^GIDDisconnectCallback)(NSError *_Nullable error); /// @param presentingViewController The view controller used to present `SFSafariViewContoller` on /// iOS 9 and 10 and to supply `presentationContextProvider` for `ASWebAuthenticationSession` on /// iOS 13+. -/// @param callback The block that is called on completion. This block will be called asynchronously on +/// @param callback The block that is called on completion. This block will be called asynchronously on /// the main queue. - (void)addScopes:(NSArray *)scopes presentingViewController:(UIViewController *)presentingViewController diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h new file mode 100644 index 00000000..29f5d3b9 --- /dev/null +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h @@ -0,0 +1,38 @@ +/* + * Copyright 2022 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 + +NS_ASSUME_NONNULL_BEGIN + +/// This class represents the basic information of a token. +@interface GIDToken : NSObject + +/// The token string. +@property(nonatomic, copy, readonly) NSString *tokenString; + +/// The estimated expiration date of the token. +@property(nonatomic, readonly, nullable) NSDate *expirationDate; + +/// Unsupported. ++ (instancetype)new NS_UNAVAILABLE; + +/// Unsupported. +- (instancetype)init NS_UNAVAILABLE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h b/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h index df002612..d1b7afda 100644 --- a/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h +++ b/GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h @@ -20,6 +20,7 @@ #import "GIDGoogleUser.h" #import "GIDProfileData.h" #import "GIDSignIn.h" +#import "GIDToken.h" #import "GIDUserAuth.h" #if TARGET_OS_IOS || TARGET_OS_MACCATALYST #import "GIDSignInButton.h" diff --git a/GoogleSignIn/Tests/Unit/GIDTokenTest.m b/GoogleSignIn/Tests/Unit/GIDTokenTest.m new file mode 100644 index 00000000..aa9d1b1a --- /dev/null +++ b/GoogleSignIn/Tests/Unit/GIDTokenTest.m @@ -0,0 +1,56 @@ +// Copyright 2022 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 +#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDToken.h" + +#import "GoogleSignIn/Sources/GIDToken_Private.h" + +static NSString * const tokenString = @"tokenString"; + +@interface GIDTokenTest : XCTestCase { + NSDate *_date; +} +@end + +@implementation GIDTokenTest + +- (void)setUP { + [super setUp]; + _date = [[NSDate alloc]initWithTimeIntervalSince1970:1000]; +} + +- (void)testInitializer { + GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date]; + XCTAssertEqualObjects(token.tokenString, tokenString); + XCTAssertEqualObjects(token.expirationDate, _date); +} + +- (void)testCoding { + if (@available(iOS 11, macOS 10.13, *)) { + GIDToken *token = [[GIDToken alloc]initWithTokenString:tokenString expirationDate:_date]; + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:token requiringSecureCoding:YES error:nil]; + GIDToken *newToken = [NSKeyedUnarchiver unarchivedObjectOfClass:[GIDToken class] + fromData:data + error:nil]; + XCTAssertEqualObjects(token.tokenString, newToken.tokenString); + XCTAssertEqualObjects(token.expirationDate, newToken.expirationDate); + + XCTAssertTrue([GIDToken supportsSecureCoding]); + } else { + XCTSkip(@"Required API is not available for this test."); + } +} + +@end