-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgoogle_oidc.c
271 lines (224 loc) · 8.72 KB
/
google_oidc.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <jwt-cpp/jwt.h>
#include <curl/curl.h>
/*
Generates and verifies google id_token on GCE instance or with GCP service account json file
// https://github.com/salrashid123/google_id_token
apt-get install libcurl4-openssl-dev libssl-dev
git clone https://github.com/Thalhammer/jwt-cpp.git
cd jwt-cpp/cmake
mkdir build
cd build
cmake ../../
make
g++ -std=c++11 -I. -Ijwt-cpp/include -o main -lcrypto -lcurl google_oidc.c
with env or on gce
export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/svc_account.json
./main https://foo.bar
eyJhbGciOiJSUzI1NiIsImtpZ
with svc_json file
./main https://foo.bar `pwd`/svc_account.json
*/
using namespace std;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string *)userp)->append((char *)contents, size * nmemb);
return size * nmemb;
}
bool verifyIdToken(std::string token, std::string audience, std::string certsReadBuffer)
{
auto decoded_jwt = jwt::decode(token);
picojson::value v;
std::string err = picojson::parse(v, certsReadBuffer);
if (!err.empty())
{
cerr << err << std::endl;
}
picojson::object obj = v.get<picojson::object>();
// again, we can't use this JWK format of the endpoint
// since the library i used here for JWK persing looks
// for the x5c value (which doens't exist) jwk.get_x5c_key_value();
// auto jwks = jwt::parse_jwks(certsReadBuffer);
// auto jwk = jwks.get_jwk(decoded_jwt.get_key_id());
// auto issuer = decoded_jwt.get_issuer();
// jwk does not have an x5c claim so we can't do this stuff here:
// auto x5c = jwk.get_x5c_key_value();
// if (!x5c.empty() && !issuer.empty())
// {
// auto verifier =
// jwt::verify()
// .allow_algorithm(jwt::algorithm::rs256(jwt::helper::convert_base64_der_to_pem(x5c), "", "", ""))
// .with_issuer("https://accounts.google.com")
// .leeway(60UL); // value in seconds, add some to compensate timeout
// verifier.verify(decoded_jwt);
// }
// so instead, we're using the PEM format endpoint "https://www.googleapis.com/oauth2/v1/certs";
// parse that and try to verify using the provided key_id
for (const auto e : obj)
{
// todo: if the key_id isn't provided, then iterate over all keys and see
// if there's a match. for now, i'm just expecting one to be there in the headers
// (which will be the case for google issued tokens)
if (e.first == decoded_jwt.get_key_id())
{
auto verifier =
jwt::verify()
.allow_algorithm(jwt::algorithm::rs256(e.second.get<string>(), "", "", ""))
.with_issuer("https://accounts.google.com")
.with_audience(audience)
.leeway(60UL); // value in seconds, add some to compensate timeout
std::error_code ec;
verifier.verify(decoded_jwt, ec);
if (!ec)
{
std::cout << "id_token verified" << endl;
}
else
{
std::cout << "id_token verification Failed " << ec.message() << endl;
return false;
}
}
}
return true;
}
int main(int argc, char *argv[])
{
if (argc == 1 || argc > 3)
{
cerr << "usage: " << argv[0] << " <audience> <optional: /path/to/service_account.json>\n";
exit(EXIT_FAILURE);
}
std::string target_audience = argv[1];
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
std::string adc_file;
if (argc == 3)
{
adc_file = argv[2];
}
else
{
char const *adc_env = std::getenv("GOOGLE_APPLICATION_CREDENTIALS");
if (adc_env != NULL)
{
adc_file = std::string(adc_env);
}
}
// download and cache the certs here if you want to
// test the verification step.
// google's jwk does not have an x5c claim so we can't use JWK endpoint
// std::string url = "https://www.googleapis.com/oauth2/v3/certs";
// we have to instead use the PEM version here
std::string url = "https://www.googleapis.com/oauth2/v1/certs";
std::string certsReadBuffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &certsReadBuffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
std::cout << (stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return -1;
}
// now generate the id_token
if (!adc_file.empty())
{
// std::cout << "Using ADC File: " << adc_file << std::endl;
std::ifstream adc_json;
adc_json.open(adc_file);
if (adc_json.fail())
{
cerr << "Error opening ADC file: " << strerror(errno);
}
// std::cout << adc_json.rdbuf();
stringstream ss;
ss << adc_json.rdbuf();
picojson::value v;
std::string err = picojson::parse(v, ss);
if (!err.empty())
{
cerr << err << std::endl;
}
std::string type = v.get("type").get<string>();
if (type == "service_account")
{
std::string issuer = v.get("client_email").get<string>();
std::string audience = "https://oauth2.googleapis.com/token";
std::string rsa_priv_key = v.get("private_key").get<string>();
auto token = jwt::create()
.set_issuer(issuer)
.set_type("JWT")
.set_issued_at(std::chrono::system_clock::now())
.set_audience(audience)
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{100})
.set_payload_claim("target_audience", jwt::claim(std::string{target_audience}))
.sign(jwt::algorithm::rs256("", rsa_priv_key, "", "notasecret"));
std::string postData = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=" + token;
curl_easy_setopt(curl, CURLOPT_URL, "https://oauth2.googleapis.com/token");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
std::cout << (stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return -1;
}
else
{
// std::cout << readBuffer << std::endl;
picojson::value v;
std::string err = picojson::parse(v, readBuffer);
if (!err.empty())
{
std:
cerr << err << std::endl;
}
cout << v.get("id_token").get<string>().c_str() << endl;
verifyIdToken(v.get("id_token").get<string>(), audience, certsReadBuffer);
}
curl_easy_cleanup(curl);
}
else if (type == "external_account")
{
cerr << "external_account not supported" << std::endl;
// ref https://blog.salrashid.dev/articles/2022/workload_federation_cloudrun_gcf/
}
else
{
cerr << "Unknown credential file type " << type << std::endl;
}
}
else
{
// std::cout << "Using Metadata Server" << std::endl;
std::string url = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=" + target_audience + "&format=full";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
struct curl_slist *list = NULL;
list = curl_slist_append(list, "Metadata-Flavor: Google");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
std::cout << (stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return -1;
}
else
{
cout << readBuffer << endl;
verifyIdToken(readBuffer, target_audience, certsReadBuffer);
}
curl_easy_cleanup(curl);
}
}