Skip to content

Commit

Permalink
Revert "Disabling oauth1."
Browse files Browse the repository at this point in the history
This reverts commit aa39c5f.

Conflicts:
	Release/src/http/oauth/oauth1.cpp
  • Loading branch information
stgates committed Aug 18, 2014
1 parent 1e313c5 commit 9cc7758
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 21 deletions.
10 changes: 3 additions & 7 deletions Release/include/cpprest/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace client
{

// credentials and web_proxy class has been moved from web::http::client namespace to web namespace.
// The below using declarations ensure we don't break existing code.
// The below using declarations ensure we dont break existing code.
// Please use the web::credentials and web::web_proxy class going forward.
using web::credentials;
using web::web_proxy;
Expand Down Expand Up @@ -118,9 +118,7 @@ class http_client_config
/// <returns>Shared pointer to OAuth 1.0 configuration.</returns>
const std::shared_ptr<oauth1::experimental::oauth1_config> oauth1() const
{
// CodePlex #230
throw std::runtime_error("oauth1 is not implemented yet.");
//return m_oauth1;
return m_oauth1;
}

/// <summary>
Expand All @@ -129,9 +127,7 @@ class http_client_config
/// <param name="config">OAuth 1.0 configuration to set.</param>
void set_oauth1(oauth1::experimental::oauth1_config config)
{
// CodePlex #230
throw std::runtime_error("oauth1 is not implemented yet.");
//m_oauth1 = std::make_shared<oauth1::experimental::oauth1_config>(std::move(config));
m_oauth1 = std::make_shared<oauth1::experimental::oauth1_config>(std::move(config));
}
#endif

Expand Down
1 change: 0 additions & 1 deletion Release/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ elseif(WIN32)
http/client/http_win7.cpp
http/listener/http_windows_server.cpp
http/oauth/oauth1.cpp
http/oauth/oauth2.cpp
streams/windows/fileio.cpp
streams/windows/ioscheduler.cpp
utilities/dllmain.cpp
Expand Down
2 changes: 1 addition & 1 deletion Release/src/build/sources.proj
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@

<!-- non-TargetXP and non-Phone8.0 files go here-->
<ItemGroup Condition ="'$(TargetXP)' != 'true' and '$(PlatformToolset)' != 'v110_wp80'">
<ClInclude Include="$(CasablancaIncludeDir)\cpprest\oauth1.h">
<ClInclude Include="$(CasablancaIncludeDir)\cpprest\oauth1.h">
<Filter>Header Files\cpprest</Filter>
</ClInclude>
<ClCompile Include="$(CasablancaSrcDir)\http\oauth\oauth1.cpp">
Expand Down
91 changes: 79 additions & 12 deletions Release/src/http/oauth/oauth1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,60 @@ namespace experimental
#include <winternl.h>
#include <bcrypt.h>

std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
{
// CodePlex #230
return std::vector<unsigned char>();
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
{
NTSTATUS status;
BCRYPT_ALG_HANDLE alg_handle = nullptr;
BCRYPT_HASH_HANDLE hash_handle = nullptr;

std::vector<unsigned char> hash;
DWORD hash_len = 0;
ULONG result_len = 0;

auto key_c = conversions::utf16_to_utf8(key);
auto data_c = conversions::utf16_to_utf8(data);

status = BCryptOpenAlgorithmProvider(&alg_handle, BCRYPT_SHA1_ALGORITHM, nullptr, BCRYPT_ALG_HANDLE_HMAC_FLAG);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = BCryptGetProperty(alg_handle, BCRYPT_HASH_LENGTH, (PBYTE) &hash_len, sizeof(hash_len), &result_len, 0);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
hash.resize(hash_len);

status = BCryptCreateHash(alg_handle, &hash_handle, nullptr, 0, (PBYTE) key_c.c_str(), (ULONG) key_c.length(), 0);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = BCryptHashData(hash_handle, (PBYTE) data_c.c_str(), (ULONG) data_c.length(), 0);
if (!NT_SUCCESS(status))
{
goto cleanup;
}
status = BCryptFinishHash(hash_handle, hash.data(), hash_len, 0);
if (!NT_SUCCESS(status))
{
goto cleanup;
}

return hash;

cleanup:
if (hash_handle)
{
BCryptDestroyHash(hash_handle);
}
if (alg_handle)
{
BCryptCloseAlgorithmProvider(alg_handle, 0);
}

return hash;
}

#elif defined(_MS_WINDOWS) && defined(__cplusplus_winrt) // Windows RT
Expand All @@ -71,20 +121,37 @@ using namespace Windows::Security::Cryptography;
using namespace Windows::Security::Cryptography::Core;
using namespace Windows::Storage::Streams;

std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
{
// CodePlex #230
return std::vector<unsigned char>();
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
{
Platform::String^ data_str = ref new Platform::String(data.c_str());
Platform::String^ key_str = ref new Platform::String(key.c_str());

MacAlgorithmProvider^ HMACSha1Provider = MacAlgorithmProvider::OpenAlgorithm(MacAlgorithmNames::HmacSha1);
IBuffer^ content_buffer = CryptographicBuffer::ConvertStringToBinary(data_str, BinaryStringEncoding::Utf8);
IBuffer^ key_buffer = CryptographicBuffer::ConvertStringToBinary(key_str, BinaryStringEncoding::Utf8);

auto signature_key = HMACSha1Provider->CreateKey(key_buffer);
auto signed_buffer = CryptographicEngine::Sign(signature_key, content_buffer);

Platform::Array<unsigned char, 1>^ arr;
CryptographicBuffer::CopyToByteArray(signed_buffer, &arr);
return std::vector<unsigned char>(arr->Data, arr->Data + arr->Length);
}

#else // Linux, Mac OS X

#include <openssl/hmac.h>

std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
{
// CodePlex #230
return std::vector<unsigned char>();
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
{
unsigned char digest[HMAC_MAX_MD_CBLOCK];
unsigned int digest_len = 0;

HMAC(EVP_sha1(), key.c_str(), static_cast<int>(key.length()),
(const unsigned char*) data.c_str(), data.length(),
digest, &digest_len);

return std::vector<unsigned char>(digest, digest + digest_len);
}

#endif
Expand Down
1 change: 1 addition & 0 deletions Release/tests/Functional/http/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(SOURCES
http_client_tests.cpp
http_methods_tests.cpp
multiple_requests.cpp
oauth1_tests.cpp
oauth2_tests.cpp
outside_tests.cpp
pipeline_stage_tests.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<ClCompile Include="..\header_tests.cpp" />
<ClCompile Include="..\http_client_tests.cpp" />
<ClCompile Include="..\http_methods_tests.cpp" />
<ClCompile Include="..\oauth1_tests.cpp" />
<ClCompile Include="..\oauth2_tests.cpp" />
<ClCompile Include="..\outside_tests.cpp" />
<ClCompile Include="..\multiple_requests.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
<ClCompile Include="..\header_tests.cpp" />
<ClCompile Include="..\http_client_tests.cpp" />
<ClCompile Include="..\http_methods_tests.cpp" />
<ClCompile Include="..\oauth1_tests.cpp" />
<ClCompile Include="..\oauth2_tests.cpp" />
<ClCompile Include="..\outside_tests.cpp" />
<ClCompile Include="..\multiple_requests.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<ClCompile Include="..\header_tests.cpp" />
<ClCompile Include="..\http_client_tests.cpp" />
<ClCompile Include="..\http_methods_tests.cpp" />
<ClCompile Include="..\oauth1_tests.cpp" />
<ClCompile Include="..\oauth2_tests.cpp" />
<ClCompile Include="..\outside_tests.cpp" />
<ClCompile Include="..\multiple_requests.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
<ClCompile Include="..\header_tests.cpp" />
<ClCompile Include="..\http_client_tests.cpp" />
<ClCompile Include="..\http_methods_tests.cpp" />
<ClCompile Include="..\oauth1_tests.cpp" />
<ClCompile Include="..\oauth2_tests.cpp" />
<ClCompile Include="..\outside_tests.cpp" />
<ClCompile Include="..\multiple_requests.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
<ClCompile Include="..\header_tests.cpp" />
<ClCompile Include="..\http_client_tests.cpp" />
<ClCompile Include="..\http_methods_tests.cpp" />
<ClCompile Include="..\oauth1_tests.cpp" />
<ClCompile Include="..\oauth2_tests.cpp" />
<ClCompile Include="..\outside_tests.cpp" />
<ClCompile Include="..\multiple_requests.cpp" />
Expand Down

0 comments on commit 9cc7758

Please sign in to comment.