Skip to content

Commit

Permalink
Return GACAppCheckTokenResult in limitedUseTokenWithCompletion (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Aug 31, 2023
1 parent f2cc90d commit f77879c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
10 changes: 4 additions & 6 deletions AppCheckCore/Sources/Core/GACAppCheck.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@

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

typedef void (^GACAppCheckTokenHandler)(GACAppCheckToken *_Nullable token,
NSError *_Nullable error);
typedef void (^GACAppCheckTokenHandler)(GACAppCheckTokenResult *result);

@interface GACAppCheck ()

Expand Down Expand Up @@ -110,8 +109,7 @@ - (instancetype)initWithServiceName:(NSString *)serviceName
tokenDelegate:tokenDelegate];
}

- (void)tokenForcingRefresh:(BOOL)forcingRefresh
completion:(void (^)(GACAppCheckTokenResult *result))handler {
- (void)tokenForcingRefresh:(BOOL)forcingRefresh completion:(GACAppCheckTokenHandler)handler {
[self retrieveOrRefreshTokenForcingRefresh:forcingRefresh]
.then(^id _Nullable(GACAppCheckToken *token) {
handler([[GACAppCheckTokenResult alloc] initWithToken:token]);
Expand All @@ -125,11 +123,11 @@ - (void)tokenForcingRefresh:(BOOL)forcingRefresh
- (void)limitedUseTokenWithCompletion:(GACAppCheckTokenHandler)handler {
[self limitedUseToken]
.then(^id _Nullable(GACAppCheckToken *token) {
handler(token, nil);
handler([[GACAppCheckTokenResult alloc] initWithToken:token]);
return token;
})
.catch(^(NSError *_Nonnull error) {
handler(nil, error);
handler([[GACAppCheckTokenResult alloc] initWithError:error]);
});
}

Expand Down
9 changes: 6 additions & 3 deletions AppCheckCore/Sources/Public/AppCheckCore/GACAppCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ NS_SWIFT_NAME(AppCheckCoreProtocol) @protocol GACAppCheckProtocol
/// most cases, `NO` should be used. `YES` should only be used if the server explicitly returns an
/// error, indicating a revoked token.
/// @param handler The completion handler to call when the token fetch request completes. The
/// `result` parameter Includes the App Check token if the request succeeds, or a placeholder token
/// `result` parameter includes the App Check token if the request succeeds, or a placeholder token
/// and an error if the request fails.
- (void)tokenForcingRefresh:(BOOL)forcingRefresh
completion:(void (^)(GACAppCheckTokenResult *result))handler
Expand All @@ -43,8 +43,11 @@ NS_SWIFT_NAME(AppCheckCoreProtocol) @protocol GACAppCheckProtocol
///
/// This method does not affect the token generation behavior of the
/// ``tokenForcingRefresh()`` method.
- (void)limitedUseTokenWithCompletion:(void (^)(GACAppCheckToken *_Nullable token,
NSError *_Nullable error))handler;
///
/// @param handler The completion handler to call when the token fetch request completes. The
/// `result` parameter includes the App Check token if the request succeeds, or a placeholder token
/// and an error if the request fails.
- (void)limitedUseTokenWithCompletion:(void (^)(GACAppCheckTokenResult *result))handler;

@end

Expand Down
27 changes: 12 additions & 15 deletions AppCheckCore/Tests/Unit/Core/GACAppCheckTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,11 @@ - (void)testLimitedUseTokenWithSuccess {
// 5. Expect token request to be completed.
XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];

[self.appCheck
limitedUseTokenWithCompletion:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
[getTokenExpectation fulfill];
XCTAssertNotNil(token);
XCTAssertEqualObjects(token.token, expectedToken.token);
XCTAssertNil(error);
}];
[self.appCheck limitedUseTokenWithCompletion:^(GACAppCheckTokenResult *result) {
[getTokenExpectation fulfill];
XCTAssertEqualObjects(result.token, expectedToken);
XCTAssertNil(result.error);
}];
[self waitForExpectations:@[ getTokenExpectation ] timeout:0.5];
[self verifyAllMocks];
}
Expand All @@ -374,14 +372,13 @@ - (void)testLimitedUseToken_WhenTokenGenerationErrors {
// 5. Expect token request to be completed.
XCTestExpectation *getTokenExpectation = [self expectationWithDescription:@"getToken"];

[self.appCheck
limitedUseTokenWithCompletion:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
[getTokenExpectation fulfill];
XCTAssertNotNil(error);
XCTAssertNil(token.token);
XCTAssertEqualObjects(error, providerError);
XCTAssertEqualObjects(error.domain, GACAppCheckErrorDomain);
}];
[self.appCheck limitedUseTokenWithCompletion:^(GACAppCheckTokenResult *result) {
[getTokenExpectation fulfill];
XCTAssertEqualObjects(result.token.token, kPlaceholderTokenValue);
XCTAssertNotNil(result.error);
XCTAssertEqualObjects(result.error, providerError);
XCTAssertEqualObjects(result.error.domain, GACAppCheckErrorDomain);
}];

[self waitForExpectations:@[ getTokenExpectation ] timeout:0.5];
[self verifyAllMocks];
Expand Down
26 changes: 26 additions & 0 deletions AppCheckCore/Tests/Unit/Swift/AppCheckAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ final class AppCheckAPITests {
}
}

// Get limited-use token
appCheck.limitedUseToken { result in
if let _ /* error */ = result.error {
_ /* placeholder token */ = result.token
// ...
} else {
_ /* token */ = result.token
// ...
}
}

// Get limited-use token (async/await)
if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 7.0, *) {
// async/await is only available on iOS 13+
Task {
let result = await appCheck.limitedUseToken()
if let _ /* error */ = result.error {
_ /* placeholder token */ = result.token
// ...
} else {
_ /* token */ = result.token
// ...
}
}
}

// MARK: - `AppCheckDebugProvider`

// `AppCheckDebugProvider` initializer
Expand Down

0 comments on commit f77879c

Please sign in to comment.