Skip to content

Commit

Permalink
Add GACAppCheckTokenResult type (google#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Aug 30, 2023
1 parent 961b37a commit f191868
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 2 deletions.
2 changes: 0 additions & 2 deletions AppCheckCore/Sources/Core/GACAppCheck.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@

static const NSTimeInterval kTokenExpirationThreshold = 5 * 60; // 5 min.

static NSString *const kDummyFACTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==";

typedef void (^GACAppCheckTokenHandler)(id<GACAppCheckTokenProtocol> _Nullable token,
NSError *_Nullable error);

Expand Down
51 changes: 51 additions & 0 deletions AppCheckCore/Sources/Core/GACAppCheckTokenResult.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 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 "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h"

#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"

/// Placeholder value that indicates failure.
/// This value is `{"error":"UNKNOWN_ERROR"}` encoded as base64.
static NSString *const kPlaceholderTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==";

@implementation GACAppCheckTokenResult

- (instancetype)initWithToken:(GACAppCheckToken *)token error:(NSError *)error {
if (self = [super init]) {
_token = token;
_error = error;
}

return self;
}

- (instancetype)initWithToken:(GACAppCheckToken *)token {
return [self initWithToken:token error:nil];
}

- (instancetype)initWithError:(NSError *)error {
return [self initWithToken:[[self class] placeholderToken] error:error];
}

#pragma mark - Internal

+ (GACAppCheckToken *)placeholderToken {
return [[GACAppCheckToken alloc] initWithToken:kPlaceholderTokenValue
expirationDate:[NSDate distantPast]];
}

@end
1 change: 1 addition & 0 deletions AppCheckCore/Sources/Public/AppCheckCore/AppCheckCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "GACAppCheckSettings.h"
#import "GACAppCheckToken.h"
#import "GACAppCheckTokenDelegate.h"
#import "GACAppCheckTokenResult.h"

// Debug provider
#import "GACAppCheckDebugProvider.h"
Expand Down
43 changes: 43 additions & 0 deletions AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023 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

@class GACAppCheckToken;

NS_SWIFT_NAME(AppCheckCoreTokenResult)
@interface GACAppCheckTokenResult : NSObject

/// An App Check token in the case of success or a placeholder token in the case of a failure.
@property(nonatomic, readonly) GACAppCheckToken *token;

/// A token fetch error in the case of a failure or `nil` in the case of success.
@property(nonatomic, readonly, nullable) NSError *error;

- (instancetype)initWithToken:(GACAppCheckToken *)token;

- (instancetype)initWithError:(NSError *)error;

- (instancetype)initWithToken:(GACAppCheckToken *)token
error:(nullable NSError *)error NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
93 changes: 93 additions & 0 deletions AppCheckCore/Tests/Unit/Core/GACAppCheckTokenResultTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2023 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;

#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"
#import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckTokenResult.h"

static NSString *const kTestTokenValue = @"test-token";
/// Placeholder value that indicates failure: `{"error":"UNKNOWN_ERROR"}` encoded as base64
static NSString *const kPlaceholderTokenValue = @"eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==";
static NSString *const kTestErrorDomain = @"TestErrorDomain";
static NSInteger const kTestErrorCode = 42;

@interface GACAppCheckTokenResult (Tests)

+ (GACAppCheckToken *)placeholderToken;

@end

@interface GACAppCheckTokenResultTests : XCTestCase
@end

@implementation GACAppCheckTokenResultTests

- (void)testInitWithToken {
NSDate *expectedExpirationDate = [NSDate dateWithTimeIntervalSince1970:1693314000.0];
NSDate *expectedReceivedAtDate = [NSDate dateWithTimeIntervalSince1970:1693317600.0];
GACAppCheckToken *expectedToken = [[GACAppCheckToken alloc] initWithToken:kTestTokenValue
expirationDate:expectedExpirationDate
receivedAtDate:expectedReceivedAtDate];

GACAppCheckTokenResult *tokenResult =
[[GACAppCheckTokenResult alloc] initWithToken:expectedToken];

XCTAssertEqualObjects(tokenResult.token, expectedToken);
XCTAssertNil(tokenResult.error);
}

- (void)testInitWithError {
NSError *expectedError = [NSError errorWithDomain:kTestErrorDomain
code:kTestErrorCode
userInfo:nil];

GACAppCheckTokenResult *tokenResult =
[[GACAppCheckTokenResult alloc] initWithError:expectedError];

XCTAssertEqualObjects(tokenResult.token.token, kPlaceholderTokenValue);
XCTAssertNotNil(tokenResult.error);
XCTAssertEqualObjects(tokenResult.error, expectedError);
}

- (void)testInitWithTokenAndError {
GACAppCheckToken *placeholderToken = [GACAppCheckTokenResult placeholderToken];
NSError *expectedError = [NSError errorWithDomain:kTestErrorDomain
code:kTestErrorCode
userInfo:nil];

GACAppCheckTokenResult *tokenResult =
[[GACAppCheckTokenResult alloc] initWithToken:placeholderToken error:expectedError];

XCTAssertEqualObjects(tokenResult.token, placeholderToken);
XCTAssertNotNil(tokenResult.error);
XCTAssertEqualObjects(tokenResult.error, expectedError);
}

- (void)testPlaceholderToken {
NSDate *expectedExpirationDate = [NSDate distantPast];
NSDate *expectedReceivedAtDate = [NSDate date]; // Current time

GACAppCheckToken *placeholderToken = [GACAppCheckTokenResult placeholderToken];

XCTAssertEqualObjects(placeholderToken.token, kPlaceholderTokenValue);
// Verify that the placeholder token's received at time is approximately equal to current time.
XCTAssertEqualWithAccuracy(
[placeholderToken.receivedAtDate timeIntervalSinceDate:expectedReceivedAtDate], 0, 5.0);
XCTAssertEqualObjects(placeholderToken.expirationDate, expectedExpirationDate);
}

@end

0 comments on commit f191868

Please sign in to comment.