Skip to content

Commit f3ce673

Browse files
authored
Add FIRAppCheckTokenProtocol for use in FIRAppCheckProtocol (#13035)
1 parent 2fd6844 commit f3ce673

File tree

12 files changed

+326
-11
lines changed

12 files changed

+326
-11
lines changed

FirebaseAppCheck.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Pod::Spec.new do |s|
4545
s.tvos.weak_framework = 'DeviceCheck'
4646

4747
s.dependency 'AppCheckCore', '~> 10.19'
48-
s.dependency 'FirebaseAppCheckInterop', '~> 10.17'
48+
s.dependency 'FirebaseAppCheckInterop', '~> 10.28'
4949
s.dependency 'FirebaseCore', '~> 10.18'
5050
s.dependency 'PromisesObjC', '~> 2.1'
5151
s.dependency 'GoogleUtilities/Environment', '~> 7.13'

FirebaseAppCheck/Interop/Public/FirebaseAppCheckInterop/FIRAppCheckInterop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
2323
NS_SWIFT_NAME(AppCheckTokenHandlerInterop)
2424
typedef void (^FIRAppCheckTokenHandlerInterop)(id<FIRAppCheckTokenResultInterop> tokenResult);
2525

26-
NS_SWIFT_NAME(AppCheckInterop) @protocol FIRAppCheckInterop
26+
NS_SWIFT_NAME(AppCheckInterop) @protocol FIRAppCheckInterop<NSObject>
2727

2828
/// Retrieve a cached or generate a new FAA Token. If forcingRefresh == YES always generates a new
2929
/// token and updates the cache.

FirebaseAppCheck/Interop/Public/FirebaseAppCheckInterop/FIRAppCheckProtocol.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
@class FIRAppCheckToken;
19+
@protocol FIRAppCheckTokenProtocol;
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

@@ -38,8 +38,8 @@ NS_SWIFT_NAME(AppCheckProtocol)
3838
/// @param handler The completion handler. Includes the app check token if the request succeeds,
3939
/// or an error if the request fails.
4040
- (void)tokenForcingRefresh:(BOOL)forcingRefresh
41-
completion:
42-
(void (^)(FIRAppCheckToken *_Nullable token, NSError *_Nullable error))handler
41+
completion:(void (^)(id<FIRAppCheckTokenProtocol> _Nullable token,
42+
NSError *_Nullable error))handler
4343
NS_SWIFT_NAME(token(forcingRefresh:completion:));
4444

4545
/// Requests a limited-use Firebase App Check token. This method should be used only if you need to
@@ -50,7 +50,7 @@ NS_SWIFT_NAME(AppCheckProtocol)
5050
/// Protection](https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection).
5151
/// This method does not affect the token generation behavior of the
5252
/// ``tokenForcingRefresh()`` method.
53-
- (void)limitedUseTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
53+
- (void)limitedUseTokenWithCompletion:(void (^)(id<FIRAppCheckTokenProtocol> _Nullable token,
5454
NSError *_Nullable error))handler;
5555

5656
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
NS_ASSUME_NONNULL_BEGIN
17+
18+
NS_SWIFT_NAME(AppCheckTokenProtocol)
19+
@protocol FIRAppCheckTokenProtocol <NSObject>
20+
21+
/// A Firebase App Check token.
22+
@property(nonatomic, readonly) NSString *token;
23+
24+
/// The App Check token's expiration date in the device's local time.
25+
@property(nonatomic, readonly) NSDate *expirationDate;
26+
27+
@end
28+
NS_ASSUME_NONNULL_END

FirebaseAppCheck/Interop/Public/FirebaseAppCheckInterop/FirebaseAppCheckInterop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
#import "FIRAppCheckInterop.h"
1818
#import "FIRAppCheckProtocol.h"
19+
#import "FIRAppCheckTokenProtocol.h"
1920
#import "FIRAppCheckTokenResultInterop.h"

FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheck.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ NS_SWIFT_NAME(AppCheck)
7474
/// set explicitly, the value will be persisted and used as a default value on next app launches.
7575
@property(nonatomic, assign) BOOL isTokenAutoRefreshEnabled;
7676

77+
/// Requests Firebase app check token. This method should *only* be used if you need to authorize
78+
/// requests to a non-Firebase backend. Requests to Firebase backend are authorized automatically if
79+
/// configured.
80+
///
81+
/// If your non-Firebase backend exposes sensitive or expensive endpoints that have low traffic
82+
/// volume, consider protecting it with [Replay
83+
/// Protection](https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection).
84+
/// In this case, use the ``limitedUseToken(completion:)`` instead to obtain a limited-use token.
85+
/// @param forcingRefresh If `YES`, a new Firebase app check token is requested and the token
86+
/// cache is ignored. If `NO`, the cached token is used if it exists and has not expired yet. In
87+
/// most cases, `NO` should be used. `YES` should only be used if the server explicitly returns an
88+
/// error, indicating a revoked token.
89+
/// @param handler The completion handler. Includes the app check token if the request succeeds,
90+
/// or an error if the request fails.
91+
- (void)tokenForcingRefresh:(BOOL)forcingRefresh
92+
completion:
93+
(void (^)(FIRAppCheckToken *_Nullable token, NSError *_Nullable error))handler
94+
NS_SWIFT_NAME(token(forcingRefresh:completion:));
95+
96+
/// Requests a limited-use Firebase App Check token. This method should be used only if you need to
97+
/// authorize requests to a non-Firebase backend.
98+
///
99+
/// Returns limited-use tokens that are intended for use with your non-Firebase backend endpoints
100+
/// that are protected with [Replay
101+
/// Protection](https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection).
102+
/// This method does not affect the token generation behavior of the
103+
/// ``tokenForcingRefresh()`` method.
104+
- (void)limitedUseTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token,
105+
NSError *_Nullable error))handler;
106+
77107
@end
78108

79109
NS_ASSUME_NONNULL_END

FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
#import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
20+
1921
NS_ASSUME_NONNULL_BEGIN
2022

2123
/// An object representing a Firebase App Check token.
2224
NS_SWIFT_NAME(AppCheckToken)
23-
@interface FIRAppCheckToken : NSObject
25+
@interface FIRAppCheckToken : NSObject <FIRAppCheckTokenProtocol>
2426

2527
/// A Firebase App Check token.
2628
@property(nonatomic, readonly) NSString *token;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <Foundation/Foundation.h>
16+
17+
#import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@interface FIRAppCheckInteropAPITests : NSObject
22+
@end
23+
24+
@implementation FIRAppCheckInteropAPITests
25+
26+
- (void)usage {
27+
id<FIRAppCheckInterop> appCheckInterop;
28+
29+
[appCheckInterop getTokenForcingRefresh:NO
30+
completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
31+
NSString *__unused token = tokenResult.token;
32+
NSError *__unused _Nullable error = tokenResult.error;
33+
}];
34+
35+
NSString *__unused tokenDidChangeNotificationName =
36+
[appCheckInterop tokenDidChangeNotificationName];
37+
38+
NSString *__unused notificationTokenKey = [appCheckInterop notificationTokenKey];
39+
40+
NSString *__unused notificationAppNameKey = [appCheckInterop notificationAppNameKey];
41+
42+
if ([appCheckInterop respondsToSelector:@selector(getLimitedUseTokenWithCompletion:)]) {
43+
[appCheckInterop
44+
getLimitedUseTokenWithCompletion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
45+
NSString *__unused token = tokenResult.token;
46+
NSError *__unused _Nullable error = tokenResult.error;
47+
}];
48+
}
49+
}
50+
51+
@end
52+
53+
NS_ASSUME_NONNULL_END
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <Foundation/Foundation.h>
16+
17+
#import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@interface FIRAppCheckProtocolAPITests : NSObject
22+
@end
23+
24+
@implementation FIRAppCheckProtocolAPITests
25+
26+
- (void)usage {
27+
id<FIRAppCheckProtocol> appCheck;
28+
29+
[appCheck tokenForcingRefresh:NO
30+
completion:^(id<FIRAppCheckTokenProtocol> _Nullable token,
31+
NSError *_Nullable error) {
32+
if (token) {
33+
NSString *__unused tokenValue = token.token;
34+
NSDate *__unused expirationDate = token.expirationDate;
35+
}
36+
}];
37+
38+
if ([appCheck respondsToSelector:@selector(limitedUseTokenWithCompletion:)]) {
39+
[appCheck limitedUseTokenWithCompletion:^(id<FIRAppCheckTokenProtocol> _Nullable token,
40+
NSError *_Nullable error) {
41+
if (token) {
42+
NSString *__unused tokenValue = token.token;
43+
NSDate *__unused expirationDate = token.expirationDate;
44+
}
45+
}];
46+
}
47+
}
48+
49+
@end
50+
51+
NS_ASSUME_NONNULL_END
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// MARK: This file is used to evaluate the AppCheckInterop API in Swift.
16+
17+
import Foundation
18+
19+
// MARK: Do not import `FirebaseAppCheck`, this file is for `FirebaseAppCheckInterop` only.
20+
21+
import FirebaseAppCheckInterop
22+
23+
final class AppCheckInteropAPITests {
24+
let appCheckInterop: AppCheckInterop! = nil
25+
26+
func usage() {
27+
let _: Void = appCheckInterop.getToken(forcingRefresh: false) { result in
28+
let _: FIRAppCheckTokenResultInterop = result
29+
let _: String = result.token
30+
if let error = result.error {
31+
let _: String = error.localizedDescription
32+
}
33+
}
34+
35+
let _: String = appCheckInterop.tokenDidChangeNotificationName()
36+
37+
let _: String = appCheckInterop.notificationTokenKey()
38+
39+
let _: String = appCheckInterop.notificationAppNameKey()
40+
41+
guard let getLimitedUseToken: (@escaping AppCheckTokenHandlerInterop) -> Void =
42+
appCheckInterop.getLimitedUseToken else { return }
43+
let _: Void = getLimitedUseToken { result in
44+
let _: FIRAppCheckTokenResultInterop = result
45+
let _: String = result.token
46+
if let error = result.error {
47+
let _: String = error.localizedDescription
48+
}
49+
}
50+
}
51+
52+
@available(iOS 13, macOS 10.15, macCatalyst 13, tvOS 13, *)
53+
func usage_async() async {
54+
let result: FIRAppCheckTokenResultInterop =
55+
await appCheckInterop.getToken(forcingRefresh: false)
56+
let _: String = result.token
57+
if let error = result.error {
58+
let _: String = error.localizedDescription
59+
}
60+
61+
// The following fails to compile with "Command SwiftCompile failed with a nonzero exit code".
62+
// let _ = await appCheckInterop.getLimitedUseToken?()
63+
}
64+
}

0 commit comments

Comments
 (0)