Kaynağa Gözat

Add GIDToken Class (#181)

Add GIDToken class
pinlu 3 yıl önce
ebeveyn
işleme
b6c6b8b508

+ 0 - 1
GoogleSignIn/Sources/GIDSignInInternalOptions.m

@@ -42,7 +42,6 @@ NS_ASSUME_NONNULL_BEGIN
                                      completion:(nullable void (^)(GIDUserAuth *_Nullable userAuth,
                                                                    NSError *_Nullable error))completion {
 #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
-  
   GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
   if (options) {
     options->_interactive = YES;

+ 58 - 0
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

+ 32 - 0
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

+ 38 - 0
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 <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+/// This class represents the basic information of a token.
+@interface GIDToken : NSObject <NSSecureCoding>
+
+/// 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

+ 1 - 0
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"

+ 56 - 0
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 <XCTest/XCTest.h>
+#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