//========================================================================== // Class: OAuth2Interface // Function: HandleAccessRequestResponse // // Description: Processes JSON responses from server. // // Input Arguments: // buffer = const UString::String & containing JSON UString:: // // Output Arguments: // None // // Return Value: // bool, true for success, false otherwise // //========================================================================== bool OAuth2Interface::HandleAccessRequestResponse(const UString::String &buffer) { cJSON *root = cJSON_Parse(UString::ToNarrowString(buffer).c_str()); if (!root) { Cerr << "Failed to parse returned string (HandleAccessRequestResponse())\n"; return false; } UString::String tokenType; unsigned int tokenValidDuration;// [sec] if (!ReadJSON(root, _T("access_token"), accessToken) || !ReadJSON(root, _T("token_type"), tokenType) || !ReadJSON(root, _T("expires_in"), tokenValidDuration)) { Cerr << "Failed to read all required fields from server\n"; cJSON_Delete(root); return false; } if (tokenType.compare(_T("Bearer")) != 0) { Cerr << "Expected token type 'Bearer', received '" << tokenType << "'\n"; cJSON_Delete(root); return false; } accessTokenValidUntilTime = std::chrono::system_clock::now() + std::chrono::seconds(tokenValidDuration); cJSON_Delete(root); return true; }
//========================================================================== // Class: OAuth2Interface // Function: HandleAuthorizationRequestResponse // // Description: Processes JSON responses from server. Used for input-limited // devices only. // // Input Arguments: // buffer = const UString::String & containing JSON UString:: // // Output Arguments: // response = AuthorizationResponse& // // Return Value: // bool, true for success, false otherwise // //========================================================================== bool OAuth2Interface::HandleAuthorizationRequestResponse( const UString::String &buffer, AuthorizationResponse &response) { assert(IsLimitedInput()); cJSON *root(cJSON_Parse(UString::ToNarrowString(buffer).c_str())); if (!root) { Cerr << "Failed to parse returned string (HandleAuthorizationRequestResponse())\n"; return false; } UString::String userCode, verificationURL; // TODO: Check state key? if (!ReadJSON(root, _T("device_code"), response.deviceCode) || !ReadJSON(root, _T("user_code"), userCode) || !ReadJSON(root, _T("verification_url"), verificationURL) || !ReadJSON(root, _T("expires_in"), response.expiresIn) || !ReadJSON(root, _T("interval"), response.interval)) { cJSON_Delete(root); return false; } Cout << "Please visit this URL: " << std::endl << verificationURL << std::endl; Cout << "And enter this code (case sensitive):" << std::endl << userCode << std::endl; cJSON_Delete(root); return true; }
//========================================================================== // Class: OAuth2Interface // Function: ResponseContainsError // // Description: Checks JSON array to see if there is an error entry. // "Authorization pending" errors are not considered errors. // // Input Arguments: // buffer = const UString::String & containing JSON UString:: // // Output Arguments: // None // // Return Value: // bool, true for error, false otherwise // //========================================================================== bool OAuth2Interface::ResponseContainsError(const UString::String &buffer) { cJSON *root(cJSON_Parse(UString::ToNarrowString(buffer).c_str())); if (!root) { Cerr << "Failed to parse returned string (ResponseContainsError())\n"; if (verbose) Cerr << buffer << '\n'; return true; } UString::String error; if (ReadJSON(root, _T("error"), error)) { if (error.compare(_T("authorization_pending")) != 0) { Cerr << "Recieved error from OAuth server: " << error; UString::String description; if (ReadJSON(root, _T("error_description"), description)) Cerr << " - " << description; Cerr << '\n'; cJSON_Delete(root); return true; } cJSON_Delete(root); } return false; }
//========================================================================== // Class: OAuth2Interface // Function: HandleRefreshRequestResponse // // Description: Processes JSON responses from server. // // Input Arguments: // buffer = const UString::String & containing JSON UString:: // // Output Arguments: // None // // Return Value: // bool, true for success, false otherwise // //========================================================================== bool OAuth2Interface::HandleRefreshRequestResponse(const UString::String &buffer, const bool &silent) { cJSON *root = cJSON_Parse(UString::ToNarrowString(buffer).c_str()); if (!root) { if (!silent) Cerr << "Failed to parse returned string (HandleRefreshRequsetResponse())\n"; return false; } UString::String tokenType; if (!ReadJSON(root, _T("refresh_token"), refreshToken)) { if (!silent) Cerr << "Failed to read refresh token field from server" << std::endl; cJSON_Delete(root); return false; } cJSON_Delete(root); return HandleAccessRequestResponse(buffer); }
std::string CSimulation2::GetMapSizes() { return ReadJSON(L"simulation/data/map_sizes.json"); }
std::string CSimulation2::GetPlayerDefaults() { return ReadJSON(L"simulation/data/player_defaults.json"); }