|
| 1 | +/* |
| 2 | + * Copyright 2023 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 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | + |
| 20 | +namespace Firebase.AppCheck { |
| 21 | + |
| 22 | +/// @brief Firebase App Check object. |
| 23 | +public sealed class FirebaseAppCheck { |
| 24 | + // The C++ object that this wraps. |
| 25 | + private AppCheckInternal appCheckInternal; |
| 26 | + |
| 27 | + private static Dictionary<FirebaseApp, FirebaseAppCheck> appCheckMap = |
| 28 | + new Dictionary<FirebaseApp, FirebaseAppCheck>(); |
| 29 | + // The user provided Factory. |
| 30 | + private static IAppCheckProviderFactory appCheckFactory; |
| 31 | + private static Dictionary<FirebaseApp, IAppCheckProvider> providerMap = |
| 32 | + new Dictionary<FirebaseApp, IAppCheckProvider>(); |
| 33 | + |
| 34 | + // Make the constructor private, since users aren't meant to make it. |
| 35 | + private FirebaseAppCheck(AppCheckInternal internalObject) { |
| 36 | + appCheckInternal = internalObject; |
| 37 | + } |
| 38 | + |
| 39 | + private void ThrowIfNull() { |
| 40 | + if (appCheckInternal == null || |
| 41 | + AppCheckInternal.getCPtr(appCheckInternal).Handle == System.IntPtr.Zero) { |
| 42 | + throw new System.NullReferenceException(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Gets the instance of FirebaseAppCheck associated with the default |
| 47 | + /// {@link FirebaseApp} instance. |
| 48 | + public static FirebaseAppCheck DefaultInstance { |
| 49 | + get { |
| 50 | + return GetInstance(FirebaseApp.DefaultInstance); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Gets the instance of FirebaseAppCheck associated with the given |
| 55 | + /// {@link FirebaseApp} instance. |
| 56 | + public static FirebaseAppCheck GetInstance(FirebaseApp app) { |
| 57 | + FirebaseAppCheck result; |
| 58 | + if (!appCheckMap.TryGetValue(app, out result)) { |
| 59 | + AppCheckInternal internalObject = AppCheckInternal.GetInstance(app); |
| 60 | + result = new FirebaseAppCheck(internalObject); |
| 61 | + appCheckMap[app] = result; |
| 62 | + // TODO(amaurice): Logic to remove from map when App is destroyed? |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + /// Installs the given {@link AppCheckProviderFactory}, overwriting any that |
| 68 | + /// were previously associated with this {@code FirebaseAppCheck} instance. |
| 69 | + /// Any {@link AppCheckTokenListener}s attached to this |
| 70 | + /// {@code FirebaseAppCheck} instance will be transferred from existing |
| 71 | + /// factories to the newly installed one. |
| 72 | + /// |
| 73 | + /// <p>Automatic token refreshing will only occur if the global {@code |
| 74 | + /// isDataCollectionDefaultEnabled} flag is set to true. To allow automatic |
| 75 | + /// token refreshing for Firebase App Check without changing the {@code |
| 76 | + /// isDataCollectionDefaultEnabled} flag for other Firebase SDKs, call |
| 77 | + /// {@link #setTokenAutoRefreshEnabled(bool)} after installing the {@code |
| 78 | + /// factory}. |
| 79 | + /// |
| 80 | + /// This method should be called before initializing the Firebase App. |
| 81 | + public static void SetAppCheckProviderFactory(IAppCheckProviderFactory factory) { |
| 82 | + appCheckFactory = factory; |
| 83 | + // TODO(amaurice): Clear the provider map when the factory changes? |
| 84 | + } |
| 85 | + |
| 86 | + /// Sets the {@code isTokenAutoRefreshEnabled} flag. |
| 87 | + public void SetTokenAutoRefreshEnabled(bool isTokenAutoRefreshEnabled) { |
| 88 | + ThrowIfNull(); |
| 89 | + appCheckInternal.SetTokenAutoRefreshEnabled(isTokenAutoRefreshEnabled); |
| 90 | + } |
| 91 | + |
| 92 | + /// Requests a Firebase App Check token. This method should be used ONLY if you |
| 93 | + /// need to authorize requests to a non-Firebase backend. Requests to Firebase |
| 94 | + /// backends are authorized automatically if configured. |
| 95 | + public System.Threading.Tasks.Task<AppCheckToken> |
| 96 | + GetAppCheckTokenAsync(bool forceRefresh) { |
| 97 | + ThrowIfNull(); |
| 98 | + throw new NotImplementedException(); |
| 99 | + } |
| 100 | + |
| 101 | + /// Called on the client when an AppCheckToken is created or changed. |
| 102 | + public System.EventHandler<TokenChangedEventArgs> TokenChanged; |
| 103 | +} |
| 104 | + |
| 105 | +} |
0 commit comments