Exemplo n.º 1
0
//==========================================================================
// 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;
}
Exemplo n.º 2
0
//==========================================================================
// 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;
}
Exemplo n.º 3
0
//==========================================================================
// 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;
}
Exemplo n.º 4
0
//==========================================================================
// 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);
}
Exemplo n.º 5
0
std::string CSimulation2::GetMapSizes()
{
	return ReadJSON(L"simulation/data/map_sizes.json");
}
Exemplo n.º 6
0
std::string CSimulation2::GetPlayerDefaults()
{
	return ReadJSON(L"simulation/data/player_defaults.json");
}