-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathcommon_android.cc
More file actions
176 lines (152 loc) · 6.18 KB
/
common_android.cc
File metadata and controls
176 lines (152 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 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.
#include "app_check/src/android/common_android.h"
#include <string>
#include "app/src/app_common.h"
#include "app/src/util.h"
#include "app/src/util_android.h"
#include "app_check/src/include/firebase/app_check.h"
namespace firebase {
namespace app_check {
namespace internal {
METHOD_LOOKUP_DEFINITION(app_check_provider,
PROGUARD_KEEP_CLASS
"com/google/firebase/appcheck/AppCheckProvider",
APP_CHECK_PROVIDER_METHODS)
// Used to setup the cache of AppCheckProvider interface method IDs to reduce
// time spent looking up methods by string.
// clang-format off
#define APP_CHECK_TOKEN_METHODS(X) \
X(GetToken, "getToken", \
"()Ljava/lang/String;"), \
X(GetExpireTimeMillis, "getExpireTimeMillis", \
"()J")
// clang-format on
METHOD_LOOKUP_DECLARATION(app_check_token, APP_CHECK_TOKEN_METHODS)
METHOD_LOOKUP_DEFINITION(app_check_token,
PROGUARD_KEEP_CLASS
"com/google/firebase/appcheck/AppCheckToken",
APP_CHECK_TOKEN_METHODS)
bool CacheCommonAndroidMethodIds(JNIEnv* env, jobject activity) {
// Cache the token and provider classes.
if (!(app_check_token::CacheMethodIds(env, activity) &&
app_check_provider::CacheMethodIds(env, activity))) {
return false;
}
return true;
}
void ReleaseCommonAndroidClasses(JNIEnv* env) {
app_check_token::ReleaseClass(env);
app_check_provider::ReleaseClass(env);
}
// Converts an android AppCheckToken to a C++ AppCheckToken
AppCheckToken CppTokenFromAndroidToken(JNIEnv* env, jobject token_obj) {
AppCheckToken cpp_token;
if (token_obj != nullptr) {
jobject token_str = env->CallObjectMethod(
token_obj, app_check_token::GetMethodId(app_check_token::kGetToken));
util::CheckAndClearJniExceptions(env);
cpp_token.token = util::JStringToString(env, token_str);
jlong millis = env->CallLongMethod(
token_obj,
app_check_token::GetMethodId(app_check_token::kGetExpireTimeMillis));
util::CheckAndClearJniExceptions(env);
cpp_token.expire_time_millis = static_cast<int64_t>(millis);
}
return cpp_token;
}
JNIEnv* GetJniEnv() {
// The JNI environment is the same regardless of App.
App* app = app_common::GetAnyApp();
FIREBASE_ASSERT(app != nullptr);
return app->GetJNIEnv();
}
namespace {
// An object wrapping a token-completion callback method
struct TokenResultCallbackData {
TokenResultCallbackData(
std::function<void(AppCheckToken, int, const std::string&)> callback_)
: callback(callback_) {}
std::function<void(AppCheckToken, int, const std::string&)> callback;
};
void TokenResultCallback(JNIEnv* env, jobject result,
util::FutureResult result_code,
const char* status_message, void* callback_data) {
int result_error_code = kAppCheckErrorNone;
AppCheckToken result_token;
bool success = (result_code == util::kFutureResultSuccess);
std::string result_value = "";
if (success && result) {
result_token = CppTokenFromAndroidToken(env, result);
} else {
// Using error code unknown for failure because Android AppCheck doesnt have
// an error code enum
result_error_code = kAppCheckErrorUnknown;
}
// Evalute the provided callback method.
auto* completion_callback_data =
reinterpret_cast<TokenResultCallbackData*>(callback_data);
completion_callback_data->callback(result_token, result_error_code,
status_message);
delete completion_callback_data;
}
} // anonymous namespace
AndroidAppCheckProvider::AndroidAppCheckProvider(jobject local_provider)
: android_provider_(nullptr) {
static const char* kApiIdentifier = "AppCheckProvider";
jni_task_id_ = CreateApiIdentifier(kApiIdentifier, this);
JNIEnv* env = GetJniEnv();
android_provider_ = env->NewGlobalRef(local_provider);
}
AndroidAppCheckProvider::~AndroidAppCheckProvider() {
JNIEnv* env = GetJniEnv();
util::CancelCallbacks(env, jni_task_id_.c_str());
if (env != nullptr && android_provider_ != nullptr) {
env->DeleteGlobalRef(android_provider_);
}
}
void AndroidAppCheckProvider::GetToken(
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
JNIEnv* env = GetJniEnv();
jobject j_task = env->CallObjectMethod(
android_provider_,
app_check_provider::GetMethodId(app_check_provider::kGetToken));
std::string error = util::GetAndClearExceptionMessage(env);
if (error.empty()) {
// Create an object to wrap the callback function
TokenResultCallbackData* completion_callback_data =
new TokenResultCallbackData(completion_callback);
util::RegisterCallbackOnTask(
env, j_task, TokenResultCallback,
reinterpret_cast<void*>(completion_callback_data),
jni_task_id_.c_str());
} else {
AppCheckToken empty_token;
completion_callback(empty_token, kAppCheckErrorUnknown, error.c_str());
}
env->DeleteLocalRef(j_task);
}
void AndroidAppCheckProvider::GetLimitedUseToken(
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
LogWarning(
"GetLimitedUseToken() was called, but the AppCheckProvider interface on "
"Android does not yet support limited-use tokens. Falling back to "
"GetToken().");
GetToken(completion_callback);
}
} // namespace internal
} // namespace app_check
} // namespace firebase