|
| 1 | +// Copyright 2025 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 FirebaseAppCheckInterop |
| 16 | + |
| 17 | +/// Internal helper extension for fetching app check tokens. |
| 18 | +/// |
| 19 | +/// Provides a common means for fetching limited use tokens, and falling back to standard tokens |
| 20 | +/// when it's disabled (or in debug mode). This also centrializes the error, since this method is |
| 21 | +/// used in multiple places. |
| 22 | +extension AppCheckInterop { |
| 23 | + /// Fetch the appcheck token. |
| 24 | + /// |
| 25 | + /// - Parameters: |
| 26 | + /// - limitedUse: Should the token be a limited-use token, or a standard token. |
| 27 | + /// - domain: A string dictating where this method is being called from. Used in any thrown |
| 28 | + /// errors, to avoid hard-to-parse traces. |
| 29 | + func fetchAppCheckToken(limitedUse: Bool, |
| 30 | + domain: String) async throws -> FIRAppCheckTokenResultInterop { |
| 31 | + if limitedUse { |
| 32 | + if let token = await getLimitedUseTokenAsync() { |
| 33 | + return token |
| 34 | + } |
| 35 | + |
| 36 | + let errorMessage = |
| 37 | + "The provided App Check token provider doesn't implement getLimitedUseToken(), but requireLimitedUseTokens was enabled." |
| 38 | + |
| 39 | + #if Debug |
| 40 | + fatalError(errorMessage) |
| 41 | + #else |
| 42 | + throw NSError( |
| 43 | + domain: "\(Constants.baseErrorDomain).\(domain)", |
| 44 | + code: AILog.MessageCode.appCheckTokenFetchFailed.rawValue, |
| 45 | + userInfo: [NSLocalizedDescriptionKey: errorMessage] |
| 46 | + ) |
| 47 | + #endif |
| 48 | + } |
| 49 | + |
| 50 | + return await getToken(forcingRefresh: false) |
| 51 | + } |
| 52 | + |
| 53 | + private func getLimitedUseTokenAsync() async |
| 54 | + -> FIRAppCheckTokenResultInterop? { |
| 55 | + // At the moment, `await` doesn’t get along with Objective-C’s optional protocol methods. |
| 56 | + await withCheckedContinuation { (continuation: CheckedContinuation< |
| 57 | + FIRAppCheckTokenResultInterop?, |
| 58 | + Never |
| 59 | + >) in |
| 60 | + guard |
| 61 | + // `getLimitedUseToken(completion:)` is an optional protocol method. Optional binding |
| 62 | + // is performed to make sure `continuation` is called even if the method’s not implemented. |
| 63 | + let limitedUseTokenClosure = getLimitedUseToken |
| 64 | + else { |
| 65 | + return continuation.resume(returning: nil) |
| 66 | + } |
| 67 | + |
| 68 | + limitedUseTokenClosure { tokenResult in |
| 69 | + // The placeholder token should be used in the case of App Check error. |
| 70 | + continuation.resume(returning: tokenResult) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments