-
Notifications
You must be signed in to change notification settings - Fork 137
Limited Use Tokens in Functions #1840
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,18 @@ namespace internal { | |
| "(Ljava/lang/String;)" \ | ||
| "Lcom/google/firebase/functions/HttpsCallableReference;", \ | ||
| util::kMethodTypeInstance), \ | ||
| X(GetHttpsCallableWithOptions, "getHttpsCallable", \ | ||
| "(Ljava/lang/String;Lcom/google/firebase/functions/HttpsCallableOptions;)" \ | ||
| "Lcom/google/firebase/functions/HttpsCallableReference;", \ | ||
| util::kMethodTypeInstance), \ | ||
| X(GetHttpsCallableFromURL, "getHttpsCallableFromUrl", \ | ||
| "(Ljava/net/URL;)" \ | ||
| "Lcom/google/firebase/functions/HttpsCallableReference;", \ | ||
| util::kMethodTypeInstance), \ | ||
| X(GetHttpsCallableFromURLWithOptions, "getHttpsCallableFromUrl", \ | ||
| "(Ljava/net/URL;Lcom/google/firebase/functions/HttpsCallableOptions;)" \ | ||
| "Lcom/google/firebase/functions/HttpsCallableReference;", \ | ||
| util::kMethodTypeInstance), \ | ||
| X(UseFunctionsEmulator, "useFunctionsEmulator", \ | ||
| "(Ljava/lang/String;)V", \ | ||
| util::kMethodTypeInstance) | ||
|
|
@@ -50,6 +58,21 @@ METHOD_LOOKUP_DEFINITION(firebase_functions, | |
| "com/google/firebase/functions/FirebaseFunctions", | ||
| FIREBASE_FUNCTIONS_METHODS) | ||
|
|
||
| // clang-format off | ||
| #define CALLABLE_OPTIONS_METHODS(X) \ | ||
| X(BuilderConstructor, "<init>", "()V"), \ | ||
| X(SetLimitedUseAppCheckTokens, "setLimitedUseAppCheckTokens", \ | ||
| "(Z)Lcom/google/firebase/functions/HttpsCallableOptions$Builder;"), \ | ||
| X(Build, "build", "()Lcom/google/firebase/functions/HttpsCallableOptions;") | ||
| // clang-format on | ||
|
|
||
| METHOD_LOOKUP_DECLARATION(callable_options_builder, CALLABLE_OPTIONS_METHODS) | ||
| METHOD_LOOKUP_DEFINITION( | ||
| callable_options_builder, | ||
| PROGUARD_KEEP_CLASS | ||
| "com/google/firebase/functions/HttpsCallableOptions$Builder", | ||
| CALLABLE_OPTIONS_METHODS) | ||
|
|
||
| // clang-format off | ||
| #define FUNCTIONS_EXCEPTION_METHODS(X) \ | ||
| X(GetMessage, "getMessage", "()Ljava/lang/String;"), \ | ||
|
|
@@ -125,6 +148,7 @@ bool FunctionsInternal::Initialize(App* app) { | |
| functions_exception::CacheMethodIds(env, activity) && | ||
| functions_exception_code::CacheMethodIds(env, activity) && | ||
| functions_exception_code::CacheFieldIds(env, activity) && | ||
| callable_options_builder::CacheMethodIds(env, activity) && | ||
| // Call Initialize on all other Functions internal classes. | ||
| HttpsCallableReferenceInternal::Initialize(app))) { | ||
| return false; | ||
|
|
@@ -144,6 +168,7 @@ void FunctionsInternal::Terminate(App* app) { | |
| firebase_functions::ReleaseClass(env); | ||
| functions_exception::ReleaseClass(env); | ||
| functions_exception_code::ReleaseClass(env); | ||
| callable_options_builder::ReleaseClass(env); | ||
|
|
||
| // Call Terminate on all other Functions internal classes. | ||
| HttpsCallableReferenceInternal::Terminate(app); | ||
|
|
@@ -186,14 +211,40 @@ Error FunctionsInternal::ErrorFromJavaFunctionsException( | |
|
|
||
| HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallable( | ||
| const char* name) const { | ||
| return GetHttpsCallable(name, HttpsCallableOptions()); | ||
| } | ||
|
|
||
| HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallable( | ||
| const char* name, const HttpsCallableOptions& options) const { | ||
| FIREBASE_ASSERT_RETURN(nullptr, name != nullptr); | ||
| JNIEnv* env = app_->GetJNIEnv(); | ||
|
|
||
| // Create HttpsCallableOptions | ||
| jobject builder = | ||
| env->NewObject(callable_options_builder::GetClass(), | ||
| callable_options_builder::GetMethodId( | ||
| callable_options_builder::kBuilderConstructor)); | ||
| jobject builder2 = env->CallObjectMethod( | ||
| builder, | ||
| callable_options_builder::GetMethodId( | ||
| callable_options_builder::kSetLimitedUseAppCheckTokens), | ||
| options.limited_use_app_check_token); | ||
| env->DeleteLocalRef(builder); | ||
| builder = builder2; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this reuse of builder makes it a bit confusing. Maybe rename things to 'empty_builder' and 'builder'? |
||
| jobject java_options = env->CallObjectMethod( | ||
| builder, | ||
| callable_options_builder::GetMethodId(callable_options_builder::kBuild)); | ||
| env->DeleteLocalRef(builder); | ||
|
|
||
| jobject name_string = env->NewStringUTF(name); | ||
| jobject callable_reference_obj = env->CallObjectMethod( | ||
| obj_, | ||
| firebase_functions::GetMethodId(firebase_functions::kGetHttpsCallable), | ||
| name_string); | ||
| firebase_functions::GetMethodId( | ||
| firebase_functions::kGetHttpsCallableWithOptions), | ||
| name_string, java_options); | ||
| env->DeleteLocalRef(name_string); | ||
| env->DeleteLocalRef(java_options); | ||
|
|
||
| if (util::LogException(env, kLogLevelError, | ||
| "Functions::GetHttpsCallable() (name = %s) failed", | ||
| name)) { | ||
|
|
@@ -208,15 +259,40 @@ HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallable( | |
|
|
||
| HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallableFromURL( | ||
| const char* url) const { | ||
| return GetHttpsCallableFromURL(url, HttpsCallableOptions()); | ||
| } | ||
|
|
||
| HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallableFromURL( | ||
| const char* url, const HttpsCallableOptions& options) const { | ||
|
Comment on lines
+265
to
+266
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Android implementation of Recommendation: Implement validation to ensure the URL is secure (HTTPS) and points to a trusted destination. |
||
| FIREBASE_ASSERT_RETURN(nullptr, url != nullptr); | ||
| JNIEnv* env = app_->GetJNIEnv(); | ||
|
|
||
| // Create HttpsCallableOptions | ||
| jobject builder = | ||
| env->NewObject(callable_options_builder::GetClass(), | ||
| callable_options_builder::GetMethodId( | ||
| callable_options_builder::kBuilderConstructor)); | ||
| jobject builder2 = env->CallObjectMethod( | ||
| builder, | ||
| callable_options_builder::GetMethodId( | ||
| callable_options_builder::kSetLimitedUseAppCheckTokens), | ||
| options.limited_use_app_check_token); | ||
| env->DeleteLocalRef(builder); | ||
| builder = builder2; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| jobject java_options = env->CallObjectMethod( | ||
| builder, | ||
| callable_options_builder::GetMethodId(callable_options_builder::kBuild)); | ||
| env->DeleteLocalRef(builder); | ||
|
|
||
| jobject url_object = util::CharsToURL(env, url); | ||
| jobject callable_reference_obj = | ||
| env->CallObjectMethod(obj_, | ||
| firebase_functions::GetMethodId( | ||
| firebase_functions::kGetHttpsCallableFromURL), | ||
| url_object); | ||
| jobject callable_reference_obj = env->CallObjectMethod( | ||
| obj_, | ||
| firebase_functions::GetMethodId( | ||
| firebase_functions::kGetHttpsCallableFromURLWithOptions), | ||
| url_object, java_options); | ||
| env->DeleteLocalRef(url_object); | ||
| env->DeleteLocalRef(java_options); | ||
|
|
||
| if (util::LogException( | ||
| env, kLogLevelError, | ||
| "Functions::GetHttpsCallableFromURL() (url = %s) failed", url)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this test will be a bit weird, but I'm not sure what the best approach is. Currently, App Check isn't linked in with the testapp when it is built, so it would should check the registry, get nothing, and proceed without it. Beyond that, the Function itself isn't expecting an App Check token.
If we wanted a test that actually verified the app check token, we would likely want to make a different function that actually required the App Check token to run.