Skip to content

Commit

Permalink
URL-encode form's POST values.
Browse files Browse the repository at this point in the history
CILogon uses some reserved characters in its device ID (particularly, the `&`);
URL-encode these so they can be passed along safely.
  • Loading branch information
bbockelm committed Aug 2, 2021
1 parent 0271f80 commit 21a4536
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/pam_oauth2_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <iterator>
#include <iostream>
#include <iomanip>
#include <string>
#include <regex>

Expand All @@ -20,6 +21,28 @@

using json = nlohmann::json;

std::string url_encode(const std::string &value) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;

for (const auto c : value) {

// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}

// Any other characters are percent-encoded
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int((unsigned char) c);
escaped << std::nouppercase;
}

return escaped.str();
}

class BaseError : public std::exception
{
public:
Expand Down Expand Up @@ -215,8 +238,8 @@ void poll_for_token(const Config config,
std::string params;

oss << "grant_type=urn:ietf:params:oauth:grant-type:device_code"
<< "&device_code=" << device_code
<< "&client_id=" << client_id;
<< "&device_code=" << url_encode(device_code)
<< "&client_id=" << url_encode(client_id);
if (!config.http_basic_auth)
oss << "&client_secret=" << client_secret;
//
Expand Down

0 comments on commit 21a4536

Please sign in to comment.