Skip to content

Commit 9cc7758

Browse files
committed
Revert "Disabling oauth1."
This reverts commit aa39c5f. Conflicts: Release/src/http/oauth/oauth1.cpp
1 parent 1e313c5 commit 9cc7758

File tree

10 files changed

+89
-21
lines changed

10 files changed

+89
-21
lines changed

Release/include/cpprest/http_client.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace client
7373
{
7474

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

126124
/// <summary>
@@ -129,9 +127,7 @@ class http_client_config
129127
/// <param name="config">OAuth 1.0 configuration to set.</param>
130128
void set_oauth1(oauth1::experimental::oauth1_config config)
131129
{
132-
// CodePlex #230
133-
throw std::runtime_error("oauth1 is not implemented yet.");
134-
//m_oauth1 = std::make_shared<oauth1::experimental::oauth1_config>(std::move(config));
130+
m_oauth1 = std::make_shared<oauth1::experimental::oauth1_config>(std::move(config));
135131
}
136132
#endif
137133

Release/src/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ elseif(WIN32)
6262
http/client/http_win7.cpp
6363
http/listener/http_windows_server.cpp
6464
http/oauth/oauth1.cpp
65-
http/oauth/oauth2.cpp
6665
streams/windows/fileio.cpp
6766
streams/windows/ioscheduler.cpp
6867
utilities/dllmain.cpp

Release/src/build/sources.proj

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221

222222
<!-- non-TargetXP and non-Phone8.0 files go here-->
223223
<ItemGroup Condition ="'$(TargetXP)' != 'true' and '$(PlatformToolset)' != 'v110_wp80'">
224-
<ClInclude Include="$(CasablancaIncludeDir)\cpprest\oauth1.h">
224+
<ClInclude Include="$(CasablancaIncludeDir)\cpprest\oauth1.h">
225225
<Filter>Header Files\cpprest</Filter>
226226
</ClInclude>
227227
<ClCompile Include="$(CasablancaSrcDir)\http\oauth\oauth1.cpp">

Release/src/http/oauth/oauth1.cpp

+79-12
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,60 @@ namespace experimental
5959
#include <winternl.h>
6060
#include <bcrypt.h>
6161

62-
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
63-
{
64-
// CodePlex #230
65-
return std::vector<unsigned char>();
62+
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
63+
{
64+
NTSTATUS status;
65+
BCRYPT_ALG_HANDLE alg_handle = nullptr;
66+
BCRYPT_HASH_HANDLE hash_handle = nullptr;
67+
68+
std::vector<unsigned char> hash;
69+
DWORD hash_len = 0;
70+
ULONG result_len = 0;
71+
72+
auto key_c = conversions::utf16_to_utf8(key);
73+
auto data_c = conversions::utf16_to_utf8(data);
74+
75+
status = BCryptOpenAlgorithmProvider(&alg_handle, BCRYPT_SHA1_ALGORITHM, nullptr, BCRYPT_ALG_HANDLE_HMAC_FLAG);
76+
if (!NT_SUCCESS(status))
77+
{
78+
goto cleanup;
79+
}
80+
status = BCryptGetProperty(alg_handle, BCRYPT_HASH_LENGTH, (PBYTE) &hash_len, sizeof(hash_len), &result_len, 0);
81+
if (!NT_SUCCESS(status))
82+
{
83+
goto cleanup;
84+
}
85+
hash.resize(hash_len);
86+
87+
status = BCryptCreateHash(alg_handle, &hash_handle, nullptr, 0, (PBYTE) key_c.c_str(), (ULONG) key_c.length(), 0);
88+
if (!NT_SUCCESS(status))
89+
{
90+
goto cleanup;
91+
}
92+
status = BCryptHashData(hash_handle, (PBYTE) data_c.c_str(), (ULONG) data_c.length(), 0);
93+
if (!NT_SUCCESS(status))
94+
{
95+
goto cleanup;
96+
}
97+
status = BCryptFinishHash(hash_handle, hash.data(), hash_len, 0);
98+
if (!NT_SUCCESS(status))
99+
{
100+
goto cleanup;
101+
}
102+
103+
return hash;
104+
105+
cleanup:
106+
if (hash_handle)
107+
{
108+
BCryptDestroyHash(hash_handle);
109+
}
110+
if (alg_handle)
111+
{
112+
BCryptCloseAlgorithmProvider(alg_handle, 0);
113+
}
114+
115+
return hash;
66116
}
67117

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

74-
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
75-
{
76-
// CodePlex #230
77-
return std::vector<unsigned char>();
124+
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
125+
{
126+
Platform::String^ data_str = ref new Platform::String(data.c_str());
127+
Platform::String^ key_str = ref new Platform::String(key.c_str());
128+
129+
MacAlgorithmProvider^ HMACSha1Provider = MacAlgorithmProvider::OpenAlgorithm(MacAlgorithmNames::HmacSha1);
130+
IBuffer^ content_buffer = CryptographicBuffer::ConvertStringToBinary(data_str, BinaryStringEncoding::Utf8);
131+
IBuffer^ key_buffer = CryptographicBuffer::ConvertStringToBinary(key_str, BinaryStringEncoding::Utf8);
132+
133+
auto signature_key = HMACSha1Provider->CreateKey(key_buffer);
134+
auto signed_buffer = CryptographicEngine::Sign(signature_key, content_buffer);
135+
136+
Platform::Array<unsigned char, 1>^ arr;
137+
CryptographicBuffer::CopyToByteArray(signed_buffer, &arr);
138+
return std::vector<unsigned char>(arr->Data, arr->Data + arr->Length);
78139
}
79140

80141
#else // Linux, Mac OS X
81142

82143
#include <openssl/hmac.h>
83144

84-
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t&, const utility::string_t&)
85-
{
86-
// CodePlex #230
87-
return std::vector<unsigned char>();
145+
std::vector<unsigned char> oauth1_config::_hmac_sha1(const utility::string_t& key, const utility::string_t& data)
146+
{
147+
unsigned char digest[HMAC_MAX_MD_CBLOCK];
148+
unsigned int digest_len = 0;
149+
150+
HMAC(EVP_sha1(), key.c_str(), static_cast<int>(key.length()),
151+
(const unsigned char*) data.c_str(), data.length(),
152+
digest, &digest_len);
153+
154+
return std::vector<unsigned char>(digest, digest + digest_len);
88155
}
89156

90157
#endif

Release/tests/Functional/http/client/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(SOURCES
1212
http_client_tests.cpp
1313
http_methods_tests.cpp
1414
multiple_requests.cpp
15+
oauth1_tests.cpp
1516
oauth2_tests.cpp
1617
outside_tests.cpp
1718
pipeline_stage_tests.cpp

Release/tests/Functional/http/client/VS11.winrt/HttpClient110_test.winrt.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<ClCompile Include="..\header_tests.cpp" />
218218
<ClCompile Include="..\http_client_tests.cpp" />
219219
<ClCompile Include="..\http_methods_tests.cpp" />
220+
<ClCompile Include="..\oauth1_tests.cpp" />
220221
<ClCompile Include="..\oauth2_tests.cpp" />
221222
<ClCompile Include="..\outside_tests.cpp" />
222223
<ClCompile Include="..\multiple_requests.cpp" />

Release/tests/Functional/http/client/VS11.xp/HttpClient110_test.xp.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<ClCompile Include="..\header_tests.cpp" />
200200
<ClCompile Include="..\http_client_tests.cpp" />
201201
<ClCompile Include="..\http_methods_tests.cpp" />
202+
<ClCompile Include="..\oauth1_tests.cpp" />
202203
<ClCompile Include="..\oauth2_tests.cpp" />
203204
<ClCompile Include="..\outside_tests.cpp" />
204205
<ClCompile Include="..\multiple_requests.cpp" />

Release/tests/Functional/http/client/VS11/HttpClient110_test.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
<ClCompile Include="..\header_tests.cpp" />
206206
<ClCompile Include="..\http_client_tests.cpp" />
207207
<ClCompile Include="..\http_methods_tests.cpp" />
208+
<ClCompile Include="..\oauth1_tests.cpp" />
208209
<ClCompile Include="..\oauth2_tests.cpp" />
209210
<ClCompile Include="..\outside_tests.cpp" />
210211
<ClCompile Include="..\multiple_requests.cpp" />

Release/tests/Functional/http/client/VS12.winrt/HttpClient120_test.winrt.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<ClCompile Include="..\header_tests.cpp" />
238238
<ClCompile Include="..\http_client_tests.cpp" />
239239
<ClCompile Include="..\http_methods_tests.cpp" />
240+
<ClCompile Include="..\oauth1_tests.cpp" />
240241
<ClCompile Include="..\oauth2_tests.cpp" />
241242
<ClCompile Include="..\outside_tests.cpp" />
242243
<ClCompile Include="..\multiple_requests.cpp" />

Release/tests/Functional/http/client/VS12/HttpClient120_test.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<ClCompile Include="..\header_tests.cpp" />
218218
<ClCompile Include="..\http_client_tests.cpp" />
219219
<ClCompile Include="..\http_methods_tests.cpp" />
220+
<ClCompile Include="..\oauth1_tests.cpp" />
220221
<ClCompile Include="..\oauth2_tests.cpp" />
221222
<ClCompile Include="..\outside_tests.cpp" />
222223
<ClCompile Include="..\multiple_requests.cpp" />

0 commit comments

Comments
 (0)