@@ -40,16 +40,103 @@ public class UIHandler : MonoBehaviour {
4040
4141 private DependencyStatus dependencyStatus = DependencyStatus . UnavailableOther ;
4242
43+ // Your Firebase project's Debug token goes here.
44+ // You can get this from Firebase Console, in the App Check settings.
45+ private string appCheckDebugToken = "REPLACE_WITH_APP_CHECK_TOKEN" ;
46+
47+ // If the App Check factory has been set
48+ protected bool factoryConfigured = false ;
49+
50+ // If the testapp is running automated tests
51+ protected bool runningAutomatedTests = false ;
52+
53+ public class TestAppCheckProvider : IAppCheckProvider {
54+ public TestAppCheckProvider ( ) { }
55+
56+ public System . Threading . Tasks . Task < AppCheckToken > GetTokenAsync ( ) {
57+ // In a normal app, you would connect to the attestation service,
58+ // and get a valid token to return.
59+ AppCheckToken token = new AppCheckToken ( ) {
60+ Token = "TEST_TOKEN" ,
61+ ExpireTime = DateTime . UtcNow . AddMinutes ( 60 )
62+ } ;
63+ return Task < AppCheckToken > . FromResult ( token ) ;
64+ }
65+ }
66+
67+ public class TestAppCheckProviderFactory : IAppCheckProviderFactory {
68+ public TestAppCheckProvider provider ;
69+
70+ public TestAppCheckProviderFactory ( ) {
71+ provider = new TestAppCheckProvider ( ) ;
72+ }
73+
74+ public IAppCheckProvider CreateProvider ( FirebaseApp app ) {
75+ return provider ;
76+ }
77+ }
78+
79+ protected void PrintToken ( string prefix , AppCheckToken token ) {
80+ DebugLog ( prefix + "\n " +
81+ " " + token . Token + "\n " +
82+ " " + token . ExpireTime ) ;
83+ }
84+
85+ void OnTokenChanged ( object sender , TokenChangedEventArgs tokenArgs ) {
86+ PrintToken ( "OnTokenChanged called:" , tokenArgs . Token ) ;
87+ }
88+
4389 // When the app starts, check to make sure that we have
4490 // the required dependencies to use Firebase, and if not,
4591 // add them if possible.
4692 protected virtual void Start ( ) {
47- // TODO(amaurice): Add App Check initialization logic here
93+ UIEnabled = true ;
94+
95+ // Configure the Debug Factory with the Token
96+ DebugAppCheckProviderFactory . Instance . SetDebugToken ( appCheckDebugToken ) ;
97+ }
98+
99+ void UseTestFactory ( ) {
100+ DebugLog ( "Using Test Factory" ) ;
101+ FirebaseAppCheck . SetAppCheckProviderFactory ( new TestAppCheckProviderFactory ( ) ) ;
102+ InitializeFirebase ( ) ;
103+ factoryConfigured = true ;
104+ }
48105
106+ void UseDebugFactory ( ) {
107+ DebugLog ( "Using Debug Factory" ) ;
108+ FirebaseAppCheck . SetAppCheckProviderFactory ( DebugAppCheckProviderFactory . Instance ) ;
109+ InitializeFirebase ( ) ;
110+ factoryConfigured = true ;
111+ }
112+
113+ void GetTokenFromDebug ( ) {
114+ DebugLog ( "Getting token from Debug factory" ) ;
115+ IAppCheckProvider provider = DebugAppCheckProviderFactory . Instance . CreateProvider ( FirebaseApp . DefaultInstance ) ;
116+ provider . GetTokenAsync ( ) . ContinueWithOnMainThread ( task => {
117+ if ( task . IsFaulted ) {
118+ DebugLog ( "GetTokenFromDebug failed: " + task . Exception ) ;
119+ } else {
120+ PrintToken ( "GetTokenFromDebug:" , task . Result ) ;
121+ }
122+ } ) ;
123+ }
124+
125+ void AddTokenChangedListener ( ) {
126+ DebugLog ( "Adding token changed listener" ) ;
127+ FirebaseAppCheck . DefaultInstance . TokenChanged += OnTokenChanged ;
128+ }
129+
130+ void RemoveTokenChangedListener ( ) {
131+ DebugLog ( "Removing token changed listener" ) ;
132+ FirebaseAppCheck . DefaultInstance . TokenChanged -= OnTokenChanged ;
133+ }
134+
135+ protected void InitializeFirebase ( ) {
49136 FirebaseApp . CheckAndFixDependenciesAsync ( ) . ContinueWithOnMainThread ( task => {
50137 dependencyStatus = task . Result ;
51138 if ( dependencyStatus == DependencyStatus . Available ) {
52- UIEnabled = true ;
139+ DebugLog ( "Firebase Ready: " + FirebaseApp . DefaultInstance ) ;
53140 } else {
54141 Debug . LogError (
55142 "Could not resolve all Firebase dependencies: " + dependencyStatus ) ;
@@ -84,17 +171,54 @@ void GUIDisplayLog() {
84171 GUILayout . EndScrollView ( ) ;
85172 }
86173
174+ void HandleGetAppCheckToken ( Task < AppCheckToken > task ) {
175+ if ( task . IsFaulted ) {
176+ DebugLog ( "GetAppCheckToken failed: " + task . Exception ) ;
177+ } else {
178+ PrintToken ( "GetAppCheckToken succeeded:" , task . Result ) ;
179+ }
180+ }
181+
87182 // Render the buttons and other controls.
88183 void GUIDisplayControls ( ) {
89184 if ( UIEnabled ) {
90185
186+ if ( runningAutomatedTests ) {
187+ GUILayout . Label ( "Running automated tests" ) ;
188+ return ;
189+ }
190+
91191 controlsScrollViewVector = GUILayout . BeginScrollView ( controlsScrollViewVector ) ;
92192
93193 GUILayout . BeginVertical ( ) ;
94194
95- if ( GUILayout . Button ( "Get App Check Token" ) ) {
96- // TODO(amaurice): That
97- DebugLog ( "GetAppCheckToken unimplemented!" ) ;
195+ if ( ! factoryConfigured ) {
196+ if ( GUILayout . Button ( "Use Test Provider" ) ) {
197+ UseTestFactory ( ) ;
198+ }
199+ if ( GUILayout . Button ( "Use Debug Provider" ) ) {
200+ UseDebugFactory ( ) ;
201+ }
202+ } else {
203+ if ( GUILayout . Button ( "Get App Check Token" ) ) {
204+ DebugLog ( "GetAppCheckTokenAsync(false) triggered!" ) ;
205+ FirebaseAppCheck . DefaultInstance . GetAppCheckTokenAsync ( false ) . ContinueWithOnMainThread ( HandleGetAppCheckToken ) ;
206+ }
207+ if ( GUILayout . Button ( "Force New App Check Token" ) ) {
208+ DebugLog ( "GetAppCheckTokenAsync(true) triggered!" ) ;
209+ FirebaseAppCheck . DefaultInstance . GetAppCheckTokenAsync ( true ) . ContinueWithOnMainThread ( HandleGetAppCheckToken ) ;
210+ }
211+ if ( GUILayout . Button ( "Add Token Changed Listener" ) ) {
212+ AddTokenChangedListener ( ) ;
213+ }
214+ if ( GUILayout . Button ( "Remove Token Changed Listener" ) ) {
215+ RemoveTokenChangedListener ( ) ;
216+ }
217+ }
218+
219+ // Can be called regardless of Factory status
220+ if ( GUILayout . Button ( "Get App Check Token from Debug Provider" ) ) {
221+ GetTokenFromDebug ( ) ;
98222 }
99223
100224 GUILayout . EndVertical ( ) ;
@@ -105,11 +229,6 @@ void GUIDisplayControls() {
105229 // Render the GUI:
106230 void OnGUI ( ) {
107231 GUI . skin = fb_GUISkin ;
108- if ( dependencyStatus != Firebase . DependencyStatus . Available ) {
109- GUILayout . Label ( "One or more Firebase dependencies are not present." ) ;
110- GUILayout . Label ( "Current dependency status: " + dependencyStatus . ToString ( ) ) ;
111- return ;
112- }
113232
114233 GUI . skin . textArea . fontSize = GUI . skin . textField . fontSize ;
115234 // Reduce the text size on the desktop.
0 commit comments