Skip to content

Commit

Permalink
Added json accept content type header and google compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsawicki committed Nov 16, 2023
1 parent 73939da commit 3067544
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/pam_oauth2_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ void make_authorization_request(const char *client_id,
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

std::string json_header = "Accept: application/json";
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, json_header.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
Expand All @@ -152,11 +158,18 @@ void make_authorization_request(const char *client_id,
auto data = json::parse(readBuffer);
response->user_code = data.at("user_code");
response->device_code = data.at("device_code");
response->verification_uri = data.at("verification_uri");
if (data.find("verification_uri_complete") != data.end()) {
response->verification_uri_complete =
data.at("verification_uri_complete");
// NOTE: Google uses verification_url instead of verification_uri
if (data.find("verification_url") != data.end()) {
response->verification_uri = data.at("verification_url");
}
else {
response->verification_uri = data.at("verification_uri");
if (data.find("verification_uri_complete") != data.end()) {
response->verification_uri_complete =
data.at("verification_uri_complete");
}
}

} catch (json::exception &e) {
syslog(LOG_ERR, "make_authorization_request: json parse failed, error=%s",
e.what());
Expand Down Expand Up @@ -198,6 +211,11 @@ void poll_for_token(const char *client_id, const char *client_secret,
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

std::string json_header = "Accept: application/json";
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, json_header.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
Expand Down Expand Up @@ -240,10 +258,12 @@ void get_userinfo(const char *userinfo_endpoint, const char *token,
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

std::string json_header = "Accept: application/json";
std::string auth_header = "Authorization: Bearer ";
auth_header += token;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, auth_header.c_str());
headers = curl_slist_append(headers, json_header.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

res = curl_easy_perform(curl);
Expand Down

0 comments on commit 3067544

Please sign in to comment.