Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Check: optimize auto-refresh #8232

Merged
merged 51 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ff204f3
App Attest provider: attestation sequence (#7971)
maksymmalyhin Apr 26, 2021
869f87c
Implement app attest key ID storage (#8014)
ncooke3 May 3, 2021
f2b77f0
Merge branch 'master' into appcheck-appattest-main
maksymmalyhin May 4, 2021
39ae2d3
App Check App Attest workflow updates: initial handshake (#8032)
maksymmalyhin May 5, 2021
ccac49c
FIRAppAttestArtifactStorage implementation and tests (#8041)
maksymmalyhin May 5, 2021
c7558b5
Implement App Attest `getRandomChallenge` (#8033)
ncooke3 May 6, 2021
e7f1ff8
Merge branch 'master' into appcheck-appattest-main
maksymmalyhin May 6, 2021
ed07e4b
Define Exchange AppAttest Assertion for FAC token API (#8058)
ncooke3 May 11, 2021
67b5c3a
Merge branch 'master' into appcheck-appattest-main
maksymmalyhin May 11, 2021
786f613
App Check App Attest: attestation request (#8059)
maksymmalyhin May 12, 2021
6742e33
Merge branch 'master' into appcheck-appattest-main
maksymmalyhin May 12, 2021
a35d7bd
App Check App Attest initial handshake adjustments (#8067)
maksymmalyhin May 12, 2021
2b16a5f
Merge remote-tracking branch 'origin/master' into appcheck-appattest-…
maksymmalyhin May 13, 2021
c2bb06b
Implement API for ExchangeAppAttestAssertionRequest endpoint (#8065)
ncooke3 May 13, 2021
cd53ea8
Merge remote-tracking branch 'origin/master' into appcheck-appattest-…
maksymmalyhin May 14, 2021
e04a789
Merge branch 'appcheck-appattest-main' of github.com:firebase/firebas…
maksymmalyhin May 14, 2021
ac17a23
App Check App Attest assertion flow (#8083)
maksymmalyhin May 17, 2021
b9d638e
Merge remote-tracking branch 'origin/master' into appcheck-appattest-…
maksymmalyhin May 17, 2021
cea2f68
App Check: store App Attest artifact per key ID (#8097)
maksymmalyhin May 18, 2021
2b70bf0
App Check: prevent concurrent token requests (#8117)
maksymmalyhin May 21, 2021
f134778
Merge remote-tracking branch 'origin/master' into appcheck-appattest-…
maksymmalyhin May 21, 2021
2102187
Changelog
maksymmalyhin May 21, 2021
c0650aa
Merge branch 'master' into appcheck-appattest-main
maksymmalyhin May 31, 2021
b8f80f3
App Check App Attest: handle attestation rejection (#8170)
maksymmalyhin Jun 1, 2021
76486af
Add FIRAppAttestProvider.h the umbrella header
maksymmalyhin Jun 1, 2021
67c6d91
Add receivedAtDate property to the FAC token
maksymmalyhin Jun 2, 2021
a7c4792
Update tests to check receivedAtDate field where important
maksymmalyhin Jun 2, 2021
0317855
[WIP] Use FIRAppCheckTokenRefreshResult instead in the refresher API
maksymmalyhin Jun 3, 2021
e24afb9
Merge remote-tracking branch 'origin/master' into mm/appcheck-refresh
maksymmalyhin Jun 4, 2021
8c40b6d
[WIP] Fix refgresher usage
maksymmalyhin Jun 4, 2021
e0e570c
Update tests with new API, keep old logic
maksymmalyhin Jun 4, 2021
e30ba15
Merge remote-tracking branch 'origin/master' into mm/appcheck-refresh
maksymmalyhin Jun 7, 2021
6b846fc
Update tests with new logic
maksymmalyhin Jun 7, 2021
847fb98
WIP
maksymmalyhin Jun 7, 2021
813c671
WIP
maksymmalyhin Jun 8, 2021
5987d2e
Initial refresh tests and fixes.
maksymmalyhin Jun 9, 2021
8894e82
Add #import <TargetConditionals.h> where needed.
maksymmalyhin Jun 9, 2021
4391ce9
Formatting
maksymmalyhin Jun 9, 2021
93836e3
Cleanup and test updates
maksymmalyhin Jun 10, 2021
5276c56
style
maksymmalyhin Jun 10, 2021
40fc64b
Comments
maksymmalyhin Jun 10, 2021
5c312bc
Merge remote-tracking branch 'origin/master' into mm/appcheck-refresh
maksymmalyhin Jun 10, 2021
f4a7c7f
Cleanup and comments
maksymmalyhin Jun 10, 2021
a9812ca
Fix catalyst
maksymmalyhin Jun 10, 2021
4713d08
Changelog
maksymmalyhin Jun 10, 2021
9825b6f
Changelog version fix
maksymmalyhin Jun 11, 2021
3ec8edc
Typo
maksymmalyhin Jun 11, 2021
26241c6
Cleanup
maksymmalyhin Jun 11, 2021
f9d140d
Typo
maksymmalyhin Jun 11, 2021
d62661c
Merge branch 'mm/appcheck-refresh' of github.com:firebase/firebase-io…
maksymmalyhin Jun 11, 2021
c6aa5ca
Imports order
maksymmalyhin Jun 11, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FirebaseAppCheck/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 8.1.0 -- M97
# 8.1.0 -- M98
- [added] Apple's App Attest attestation provider support. (#8133)
- [changed] Token auto-refresh optimizations. (#8232)
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
# v8.0.0 -- M95
- [added] Firebase abuse reduction support SDK. (#7928, #7937, #7948)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <TargetConditionals.h>

// Currently DCAppAttestService is available on iOS only.
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST // Catalyst should be possible with Xcode 12.5+

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#import "FirebaseAppCheck/Sources/AppAttestProvider/DCAppAttestService+FIRAppAttestService.h"

// Currently DCAppAttestService is available on iOS only.
#if TARGET_OS_IOS
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST // Catalyst should be possible with Xcode 12.5+

@implementation DCAppAttestService (FIRAppAttestService)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckToken+APIResponse.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"

#if __has_include(<FBLPromises/FBLPromises.h>)
#import <FBLPromises/FBLPromises.h>
Expand Down Expand Up @@ -81,7 +82,7 @@ - (nullable instancetype)initWithResponseDict:(NSDictionary<NSString *, id> *)re

NSDate *expirationDate = [requestDate dateByAddingTimeInterval:secondsToLive];

return [self initWithToken:token expirationDate:expirationDate];
return [self initWithToken:token expirationDate:expirationDate receivedAtDate:requestDate];
}

@end
22 changes: 15 additions & 7 deletions FirebaseAppCheck/Sources/Core/FIRAppCheck.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
#import "FBLPromises.h"
#endif

#import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
maksymmalyhin marked this conversation as resolved.
Show resolved Hide resolved
#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckProvider.h"
#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckProviderFactory.h"
#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"

#import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckSettings.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckTokenResult.h"
#import "FirebaseAppCheck/Sources/Core/Storage/FIRAppCheckStorage.h"
#import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTokenRefreshResult.h"
#import "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTokenRefresher.h"

#import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h"
Expand Down Expand Up @@ -124,10 +125,10 @@ - (nullable instancetype)initWithApp:(FIRApp *)app {
[[FIRAppCheckSettings alloc] initWithApp:app
userDefault:[NSUserDefaults standardUserDefaults]
mainBundle:[NSBundle mainBundle]];
FIRAppCheckTokenRefreshResult *refreshResult =
[[FIRAppCheckTokenRefreshResult alloc] initWithStatusNever];
FIRAppCheckTokenRefresher *tokenRefresher =
[[FIRAppCheckTokenRefresher alloc] initWithTokenExpirationDate:[NSDate date]
tokenExpirationThreshold:kTokenExpirationThreshold
settings:settings];
[[FIRAppCheckTokenRefresher alloc] initWithRefreshResult:refreshResult settings:settings];

FIRAppCheckStorage *storage = [[FIRAppCheckStorage alloc] initWithAppName:app.name
appID:app.options.googleAppID
Expand Down Expand Up @@ -306,7 +307,10 @@ - (nonnull NSString *)notificationTokenKey {
// TODO: Make sure the self.tokenRefresher is updated only once. Currently the timer will be
// updated twice in the case when the refresh triggered by self.tokenRefresher, but it
// should be fine for now as it is a relatively cheap operation.
[self.tokenRefresher updateTokenExpirationDate:token.expirationDate];
__auto_type refreshResult = [[FIRAppCheckTokenRefreshResult alloc]
initWithStatusSuccessAndExpirationDate:token.expirationDate
receivedAtDate:token.receivedAtDate];
[self.tokenRefresher updateWithRefreshResult:refreshResult];
[self postTokenUpdateNotificationWithToken:token];
return token;
});
Expand All @@ -317,11 +321,15 @@ - (nonnull NSString *)notificationTokenKey {
- (void)periodicTokenRefreshWithCompletion:(FIRAppCheckTokenRefreshCompletion)completion {
[self retrieveOrRefreshTokenForcingRefresh:NO]
.then(^id _Nullable(FIRAppCheckToken *_Nullable token) {
completion(YES, token.expirationDate);
__auto_type refreshResult = [[FIRAppCheckTokenRefreshResult alloc]
initWithStatusSuccessAndExpirationDate:token.expirationDate
receivedAtDate:token.receivedAtDate];
completion(refreshResult);
return nil;
})
.catch(^(NSError *error) {
completion(NO, nil);
__auto_type refreshResult = [[FIRAppCheckTokenRefreshResult alloc] initWithStatusFailure];
completion(refreshResult);
});
}

Expand Down
39 changes: 39 additions & 0 deletions FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2021 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>

#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAppCheckToken ()
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved

/// A date when the Firebase App Check token was received in the device's local time.
@property(nonatomic) NSDate *receivedAtDate;

/// The designated initializer.
/// @param token A Firebase App Check token.
/// @param expirationDate A Firebase App Check token expiration date in the device local time.
/// @param receivedAtDate A date when the Firebase App Check token was received in the device's
/// local time.
- (instancetype)initWithToken:(NSString *)token
expirationDate:(NSDate *)expirationDate
receivedAtDate:(NSDate *)receivedAtDate NS_DESIGNATED_INITIALIZER;

@end

NS_ASSUME_NONNULL_END
15 changes: 13 additions & 2 deletions FirebaseAppCheck/Sources/Core/FIRAppCheckToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,28 @@
* limitations under the License.
*/

#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"

NS_ASSUME_NONNULL_BEGIN

@implementation FIRAppCheckToken

- (instancetype)initWithToken:(NSString *)token expirationDate:(NSDate *)expirationDate {
- (instancetype)initWithToken:(NSString *)token
expirationDate:(NSDate *)expirationDate
receivedAtDate:(NSDate *)receivedAtDate {
self = [super init];
if (self) {
_token = [token copy];
_expirationDate = expirationDate;
_receivedAtDate = receivedAtDate;
}
return self;
}

- (instancetype)initWithToken:(NSString *)token expirationDate:(NSDate *)expirationDate {
return [self initWithToken:token expirationDate:expirationDate receivedAtDate:[NSDate date]];
}

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@

#import "FirebaseAppCheck/Sources/Core/Storage/FIRAppCheckStoredToken+FIRAppCheckToken.h"

#import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"
#import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"

@implementation FIRAppCheckStoredToken (FIRAppCheckToken)

- (void)updateWithToken:(FIRAppCheckToken *)token {
self.token = token.token;
self.expirationDate = token.expirationDate;
self.receivedAtDate = token.receivedAtDate;
}

- (FIRAppCheckToken *)appCheckToken {
return [[FIRAppCheckToken alloc] initWithToken:self.token expirationDate:self.expirationDate];
return [[FIRAppCheckToken alloc] initWithToken:self.token
expirationDate:self.expirationDate
receivedAtDate:self.receivedAtDate];
}

@end
12 changes: 8 additions & 4 deletions FirebaseAppCheck/Sources/Core/Storage/FIRAppCheckStoredToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ NS_ASSUME_NONNULL_BEGIN

@interface FIRAppCheckStoredToken : NSObject <NSSecureCoding>

/// FAA token.
@property(nonatomic, copy) NSString *token;
/// FAA token expiration date in the device local time.
@property(nonatomic, strong) NSDate *expirationDate;
/// The Firebase App Check token.
@property(nonatomic, copy, nullable) NSString *token;

/// The Firebase App Check token expiration date in the device local time.
@property(nonatomic, strong, nullable) NSDate *expirationDate;

/// The date when the Firebase App Check token was received in the device's local time.
@property(nonatomic, strong, nullable) NSDate *receivedAtDate;

/// The version of local storage.
@property(nonatomic, readonly) NSInteger storageVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

static NSString *const kTokenKey = @"token";
static NSString *const kExpirationDateKey = @"expirationDate";
static NSString *const kReceivedAtDateKey = @"receivedAtDate";
static NSString *const kStorageVersionKey = @"storageVersion";

static const NSInteger kStorageVersion = 1;
static const NSInteger kStorageVersion = 2;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -37,19 +38,21 @@ + (BOOL)supportsSecureCoding {
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.token forKey:kTokenKey];
[coder encodeObject:self.expirationDate forKey:kExpirationDateKey];
[coder encodeObject:self.receivedAtDate forKey:kReceivedAtDateKey];
[coder encodeInteger:self.storageVersion forKey:kStorageVersionKey];
}

- (nullable instancetype)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
NSInteger storageVersion = [coder decodeIntegerForKey:kStorageVersionKey];
if (storageVersion > kStorageVersion) {
NSInteger decodedStorageVersion = [coder decodeIntegerForKey:kStorageVersionKey];
if (decodedStorageVersion > kStorageVersion) {
// TODO: Log a message.
}

_token = [coder decodeObjectOfClass:[NSString class] forKey:kTokenKey];
_expirationDate = [coder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey];
_receivedAtDate = [coder decodeObjectOfClass:[NSDate class] forKey:kReceivedAtDateKey];
}
return self;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 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

/// Represents possible results of a Firebase App Check token refresh attempt that matter for
/// `FIRAppCheckTokenRefresher`.
typedef NS_ENUM(NSInteger, FIRAppCheckTokenRefreshStatus) {
// The token has not been refreshed.
FIRAppCheckTokenRefreshStatusNever,

// The token was successfully refreshed.
FIRAppCheckTokenRefreshStatusSuccess,

// The token refresh failed.
FIRAppCheckTokenRefreshStatusFailure
};

/// An object to pass the possible results of a Firebase App Check token refresh attempt and
/// supplementary data.
@interface FIRAppCheckTokenRefreshResult : NSObject

/// Status of the refresh.
@property(nonatomic, readonly) FIRAppCheckTokenRefreshStatus status;

/// A date when the new Firebase App Check token is expiring.
@property(nonatomic, readonly, nullable) NSDate *tokenExpirationDate;

/// A date when the new Firebase App Check token was received from the server.
@property(nonatomic, readonly, nullable) NSDate *tokenReceivedAtDate;

- (instancetype)init NS_UNAVAILABLE;

/// Initializes the instance with `FIRAppCheckTokenRefreshStatusNever`.
- (instancetype)initWithStatusNever;

/// Initializes the instance with `FIRAppCheckTokenRefreshStatusFailure`.
- (instancetype)initWithStatusFailure;

/// Initializes the instance with `FIRAppCheckTokenRefreshStatusFailure`.
maksymmalyhin marked this conversation as resolved.
Show resolved Hide resolved
/// @param tokenExpirationDate See `tokenExpirationDate` property.
/// @param tokenReceivedAtDate See `tokenReceivedAtDate` property.
- (instancetype)initWithStatusSuccessAndExpirationDate:(NSDate *)tokenExpirationDate
receivedAtDate:(NSDate *)tokenReceivedAtDate;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 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 "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTokenRefreshResult.h"

NS_ASSUME_NONNULL_BEGIN

@interface FIRAppCheckTokenRefreshResult ()

- (instancetype)initWithStatus:(FIRAppCheckTokenRefreshStatus)status
expirationDate:(nullable NSDate *)tokenExpirationDate
receivedAtDate:(nullable NSDate *)tokenReceivedAtDate NS_DESIGNATED_INITIALIZER;

@end

@implementation FIRAppCheckTokenRefreshResult

- (instancetype)initWithStatus:(FIRAppCheckTokenRefreshStatus)status
expirationDate:(nullable NSDate *)tokenExpirationDate
receivedAtDate:(nullable NSDate *)tokenReceivedAtDate {
self = [super init];
if (self) {
_status = status;
_tokenExpirationDate = tokenExpirationDate;
_tokenReceivedAtDate = tokenReceivedAtDate;
}
return self;
}

- (instancetype)initWithStatusNever {
return [self initWithStatus:FIRAppCheckTokenRefreshStatusNever
expirationDate:nil
receivedAtDate:nil];
}

- (instancetype)initWithStatusFailure {
return [self initWithStatus:FIRAppCheckTokenRefreshStatusFailure
expirationDate:nil
receivedAtDate:nil];
}

- (instancetype)initWithStatusSuccessAndExpirationDate:(NSDate *)tokenExpirationDate
receivedAtDate:(NSDate *)tokenReceivedAtDate {
return [self initWithStatus:FIRAppCheckTokenRefreshStatusSuccess
expirationDate:tokenExpirationDate
receivedAtDate:tokenReceivedAtDate];
}

@end

NS_ASSUME_NONNULL_END
Loading