-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathdebug_provider_desktop.cc
More file actions
173 lines (147 loc) · 5.5 KB
/
debug_provider_desktop.cc
File metadata and controls
173 lines (147 loc) · 5.5 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
// Copyright 2022 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/desktop/debug_provider_desktop.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "app/rest/response.h"
#include "app/rest/transport_builder.h"
#include "app/rest/transport_curl.h"
#include "app/rest/util.h"
#include "app/src/log.h"
#include "app/src/scheduler.h"
#include "app_check/src/desktop/debug_token_request.h"
#include "app_check/src/desktop/token_response.h"
#include "firebase/app_check/debug_provider.h"
namespace firebase {
namespace app_check {
namespace internal {
class DebugAppCheckProvider : public AppCheckProvider {
public:
DebugAppCheckProvider(App* app, const std::string& token);
~DebugAppCheckProvider() override;
void GetToken(std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) override;
void GetLimitedUseToken(
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) override;
private:
void GetTokenInternal(
bool limited_use,
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback);
App* app_;
scheduler::Scheduler scheduler_;
std::string debug_token_;
};
DebugAppCheckProvider::DebugAppCheckProvider(App* app, const std::string& token)
: app_(app), scheduler_(), debug_token_(token) {
firebase::rest::util::Initialize();
firebase::rest::InitTransportCurl();
}
DebugAppCheckProvider::~DebugAppCheckProvider() {
firebase::rest::CleanupTransportCurl();
firebase::rest::util::Terminate();
}
// Performs the given rest request, and calls the callback based on the
// response.
void GetTokenAsync(std::shared_ptr<DebugTokenRequest> request,
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
TokenResponse response;
firebase::rest::CreateTransport()->Perform(*request, &response);
if (response.status() == firebase::rest::util::HttpSuccess) {
// Call the callback with the response token
AppCheckToken token;
token.token = std::move(response.token());
// Expected response is in seconds
int64_t extra_time = strtol(response.ttl().c_str(), nullptr, 10);
token.expire_time_millis = (response.fetch_time() + extra_time) * 1000;
completion_callback(token, kAppCheckErrorNone, "");
} else {
// Create an error message, and pass it along instead.
AppCheckToken token;
char error_message[1000];
snprintf(error_message, sizeof(error_message),
"The server responded with an error.\n"
"HTTP status code: %d \n"
"Response body: %s\n",
response.status(), response.GetBody());
completion_callback(token, kAppCheckErrorUnknown, error_message);
}
}
void DebugAppCheckProvider::GetToken(
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
GetTokenInternal(false, completion_callback);
}
void DebugAppCheckProvider::GetLimitedUseToken(
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
GetTokenInternal(true, completion_callback);
}
void DebugAppCheckProvider::GetTokenInternal(
bool limited_use,
std::function<void(AppCheckToken, int, const std::string&)>
completion_callback) {
// Identify the user's debug token
const char* debug_token_cstr;
if (!debug_token_.empty()) {
debug_token_cstr = debug_token_.c_str();
} else {
debug_token_cstr = std::getenv("APP_CHECK_DEBUG_TOKEN");
}
if (!debug_token_cstr) {
completion_callback({}, kAppCheckErrorInvalidConfiguration,
"Missing debug token");
return;
}
// Exchange debug token with the backend to get a proper attestation token.
auto request = std::make_shared<DebugTokenRequest>(app_);
request->SetDebugToken(debug_token_cstr);
request->SetLimitedUse(limited_use);
// Use an async call, since we don't want to block on the server response.
auto async_call =
callback::NewCallback(GetTokenAsync, request, completion_callback);
scheduler_.Schedule(async_call);
}
DebugAppCheckProviderFactoryInternal::DebugAppCheckProviderFactoryInternal()
: provider_map_(), debug_token_() {}
DebugAppCheckProviderFactoryInternal::~DebugAppCheckProviderFactoryInternal() {
// Clear the map
for (auto it : provider_map_) {
delete it.second;
}
provider_map_.clear();
}
AppCheckProvider* DebugAppCheckProviderFactoryInternal::CreateProvider(
App* app) {
// Check the map
std::map<App*, AppCheckProvider*>::iterator it = provider_map_.find(app);
if (it != provider_map_.end()) {
return it->second;
}
// Create a new provider and cache it
AppCheckProvider* provider = new DebugAppCheckProvider(app, debug_token_);
provider_map_[app] = provider;
return provider;
}
void DebugAppCheckProviderFactoryInternal::SetDebugToken(
const std::string& token) {
debug_token_ = token;
}
} // namespace internal
} // namespace app_check
} // namespace firebase