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

Add the public App Check API files #673

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app_check/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
include(build_shared)

# Firebase App Check Swig input files
set(firebase_app_check_swig
set(firebase_app_check_swig_files
src/swig/app_check.i
)

# Firebase App Check CSharp files
set(firebase_app_check_src
src/AppCheckError.cs
src/AppCheckToken.cs
src/FirebaseAppCheck.cs
src/IAppCheckProvider.cs
src/IAppCheckProviderFactory.cs
src/TokenChangedEventArgs.cs
)

if(NOT FIREBASE_UNI_LIBRARY AND APPLE AND NOT IOS)
Expand All @@ -40,7 +46,7 @@ firebase_swig_add_library(firebase_app_check_swig
MODULE
FirebaseCppAppCheck
SOURCES
${firebase_app_check_swig}
${firebase_app_check_swig_files}
DEPENDS
firebase_app_check
SYSTEM_DEPS
Expand Down
39 changes: 39 additions & 0 deletions app_check/src/AppCheckError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 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.
*/

namespace Firebase.AppCheck {

/// @brief Error codes used by App Check.
public enum AppCheckError {
/// The operation was a success, no error occurred.
None = 0,
/// A network connection error.
ServerUnreachable = 1,
/// Invalid configuration error. Currently, an exception is thrown but this
/// error is reserved for future implementations of invalid configuration
/// detection.
InvalidConfiguration = 2,
/// System keychain access error. Ensure that the app has proper keychain
/// access.
SystemKeychain = 3,
/// Selected AppCheckProvider is not supported on the current platform
/// or OS version.
UnsupportedProvider = 4,
/// An unknown error occurred.
Unknown = 5,
}

}
33 changes: 33 additions & 0 deletions app_check/src/AppCheckToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 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.
*/

namespace Firebase.AppCheck {

/// @brief Token used by the Firebase App Check service.
///
/// Struct to hold tokens emitted by the Firebase App Check service which are
/// minted upon a successful application verification. These tokens are the
/// federated output of a verification flow, the structure of which is
/// independent of the mechanism by which the application was verified.
public struct AppCheckToken {
/// A Firebase App Check token.
public string Token { get; set; }

/// The time at which the token will expire.
public System.DateTime ExpireTime { get; set; }
}

}
105 changes: 105 additions & 0 deletions app_check/src/FirebaseAppCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2023 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.
*/

using System;
using System.Collections.Generic;

namespace Firebase.AppCheck {

/// @brief Firebase App Check object.
public sealed class FirebaseAppCheck {
// The C++ object that this wraps.
private AppCheckInternal appCheckInternal;

private static Dictionary<FirebaseApp, FirebaseAppCheck> appCheckMap =
new Dictionary<FirebaseApp, FirebaseAppCheck>();
// The user provided Factory.
private static IAppCheckProviderFactory appCheckFactory;
private static Dictionary<FirebaseApp, IAppCheckProvider> providerMap =
new Dictionary<FirebaseApp, IAppCheckProvider>();

// Make the constructor private, since users aren't meant to make it.
private FirebaseAppCheck(AppCheckInternal internalObject) {
appCheckInternal = internalObject;
}

private void ThrowIfNull() {
if (appCheckInternal == null ||
AppCheckInternal.getCPtr(appCheckInternal).Handle == System.IntPtr.Zero) {
throw new System.NullReferenceException();
}
}

/// Gets the instance of FirebaseAppCheck associated with the default
/// {@link FirebaseApp} instance.
public static FirebaseAppCheck DefaultInstance {
get {
return GetInstance(FirebaseApp.DefaultInstance);
}
}

/// Gets the instance of FirebaseAppCheck associated with the given
/// {@link FirebaseApp} instance.
public static FirebaseAppCheck GetInstance(FirebaseApp app) {
FirebaseAppCheck result;
if (!appCheckMap.TryGetValue(app, out result)) {
AppCheckInternal internalObject = AppCheckInternal.GetInstance(app);
result = new FirebaseAppCheck(internalObject);
appCheckMap[app] = result;
// TODO(amaurice): Logic to remove from map when App is destroyed?
}
return result;
}

/// Installs the given {@link AppCheckProviderFactory}, overwriting any that
/// were previously associated with this {@code FirebaseAppCheck} instance.
/// Any {@link AppCheckTokenListener}s attached to this
/// {@code FirebaseAppCheck} instance will be transferred from existing
/// factories to the newly installed one.
///
/// <p>Automatic token refreshing will only occur if the global {@code
/// isDataCollectionDefaultEnabled} flag is set to true. To allow automatic
/// token refreshing for Firebase App Check without changing the {@code
/// isDataCollectionDefaultEnabled} flag for other Firebase SDKs, call
/// {@link #setTokenAutoRefreshEnabled(bool)} after installing the {@code
/// factory}.
///
/// This method should be called before initializing the Firebase App.
public static void SetAppCheckProviderFactory(IAppCheckProviderFactory factory) {
appCheckFactory = factory;
// TODO(amaurice): Clear the provider map when the factory changes?
}

/// Sets the {@code isTokenAutoRefreshEnabled} flag.
public void SetTokenAutoRefreshEnabled(bool isTokenAutoRefreshEnabled) {
ThrowIfNull();
appCheckInternal.SetTokenAutoRefreshEnabled(isTokenAutoRefreshEnabled);
}

/// Requests a Firebase App Check token. This method should be used ONLY if you
/// need to authorize requests to a non-Firebase backend. Requests to Firebase
/// backends are authorized automatically if configured.
public System.Threading.Tasks.Task<AppCheckToken>
GetAppCheckTokenAsync(bool forceRefresh) {
ThrowIfNull();
throw new NotImplementedException();
}

/// Called on the client when an AppCheckToken is created or changed.
public System.EventHandler<TokenChangedEventArgs> TokenChanged;
}

}
31 changes: 31 additions & 0 deletions app_check/src/IAppCheckProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 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.
*/

namespace Firebase.AppCheck {

/// @brief Interface for a provider that generates {@link AppCheckToken}s.
///
/// This provider can be called at any time by any Firebase library that depends
/// (optionally or otherwise) on {@link AppCheckToken}s. This provider is
/// responsible for determining if it can create a new token at the time of the
/// call and returning that new token if it can.
public interface IAppCheckProvider {
/// Returns an AppCheckToken or throws an exception with an error code and
/// error message.
System.Threading.Tasks.Task<AppCheckToken> GetTokenAsync();
}

}
26 changes: 26 additions & 0 deletions app_check/src/IAppCheckProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023 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.
*/

namespace Firebase.AppCheck {

/// @brief Interface for a factory that generates AppCheckProviders.
public interface IAppCheckProviderFactory {
/// Gets the {@link AppCheckProvider} associated with the given
/// {@link FirebaseApp} instance, or creates one if none already exists.
IAppCheckProvider CreateProvider(FirebaseApp app);
}

}
28 changes: 28 additions & 0 deletions app_check/src/TokenChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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.
*/

namespace Firebase.AppCheck {

/// @brief Passed to the FirebaseAppCheck.TokenChanged event.
///
/// The AppCheck Token is passed via these arguments to the
/// FirebaseAppCheck.TokenChanged event.
public sealed class TokenChangedEventArgs : System.EventArgs {
/// A Firebase App Check token.
public AppCheckToken Token { get; set; }
}

}