Skip to content

Commit 5edabe6

Browse files
authored
Add the public App Check API files (#673)
1 parent 0344327 commit 5edabe6

File tree

7 files changed

+270
-2
lines changed

7 files changed

+270
-2
lines changed

app_check/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
include(build_shared)
1818

1919
# Firebase App Check Swig input files
20-
set(firebase_app_check_swig
20+
set(firebase_app_check_swig_files
2121
src/swig/app_check.i
2222
)
2323

2424
# Firebase App Check CSharp files
2525
set(firebase_app_check_src
26+
src/AppCheckError.cs
27+
src/AppCheckToken.cs
28+
src/FirebaseAppCheck.cs
29+
src/IAppCheckProvider.cs
30+
src/IAppCheckProviderFactory.cs
31+
src/TokenChangedEventArgs.cs
2632
)
2733

2834
if(NOT FIREBASE_UNI_LIBRARY AND APPLE AND NOT IOS)
@@ -40,7 +46,7 @@ firebase_swig_add_library(firebase_app_check_swig
4046
MODULE
4147
FirebaseCppAppCheck
4248
SOURCES
43-
${firebase_app_check_swig}
49+
${firebase_app_check_swig_files}
4450
DEPENDS
4551
firebase_app_check
4652
SYSTEM_DEPS

app_check/src/AppCheckError.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
namespace Firebase.AppCheck {
18+
19+
/// @brief Error codes used by App Check.
20+
public enum AppCheckError {
21+
/// The operation was a success, no error occurred.
22+
None = 0,
23+
/// A network connection error.
24+
ServerUnreachable = 1,
25+
/// Invalid configuration error. Currently, an exception is thrown but this
26+
/// error is reserved for future implementations of invalid configuration
27+
/// detection.
28+
InvalidConfiguration = 2,
29+
/// System keychain access error. Ensure that the app has proper keychain
30+
/// access.
31+
SystemKeychain = 3,
32+
/// Selected AppCheckProvider is not supported on the current platform
33+
/// or OS version.
34+
UnsupportedProvider = 4,
35+
/// An unknown error occurred.
36+
Unknown = 5,
37+
}
38+
39+
}

app_check/src/AppCheckToken.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespace Firebase.AppCheck {
18+
19+
/// @brief Token used by the Firebase App Check service.
20+
///
21+
/// Struct to hold tokens emitted by the Firebase App Check service which are
22+
/// minted upon a successful application verification. These tokens are the
23+
/// federated output of a verification flow, the structure of which is
24+
/// independent of the mechanism by which the application was verified.
25+
public struct AppCheckToken {
26+
/// A Firebase App Check token.
27+
public string Token { get; set; }
28+
29+
/// The time at which the token will expire.
30+
public System.DateTime ExpireTime { get; set; }
31+
}
32+
33+
}

app_check/src/FirebaseAppCheck.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

app_check/src/IAppCheckProvider.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
namespace Firebase.AppCheck {
18+
19+
/// @brief Interface for a provider that generates {@link AppCheckToken}s.
20+
///
21+
/// This provider can be called at any time by any Firebase library that depends
22+
/// (optionally or otherwise) on {@link AppCheckToken}s. This provider is
23+
/// responsible for determining if it can create a new token at the time of the
24+
/// call and returning that new token if it can.
25+
public interface IAppCheckProvider {
26+
/// Returns an AppCheckToken or throws an exception with an error code and
27+
/// error message.
28+
System.Threading.Tasks.Task<AppCheckToken> GetTokenAsync();
29+
}
30+
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
namespace Firebase.AppCheck {
18+
19+
/// @brief Interface for a factory that generates AppCheckProviders.
20+
public interface IAppCheckProviderFactory {
21+
/// Gets the {@link AppCheckProvider} associated with the given
22+
/// {@link FirebaseApp} instance, or creates one if none already exists.
23+
IAppCheckProvider CreateProvider(FirebaseApp app);
24+
}
25+
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
namespace Firebase.AppCheck {
18+
19+
/// @brief Passed to the FirebaseAppCheck.TokenChanged event.
20+
///
21+
/// The AppCheck Token is passed via these arguments to the
22+
/// FirebaseAppCheck.TokenChanged event.
23+
public sealed class TokenChangedEventArgs : System.EventArgs {
24+
/// A Firebase App Check token.
25+
public AppCheckToken Token { get; set; }
26+
}
27+
28+
}

0 commit comments

Comments
 (0)