1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/http/HttpProxyStrategy.h>
#include <aws/common/string.h>
#include <aws/crt/http/HttpConnection.h>
#include <aws/http/proxy.h>
namespace Aws
{
namespace Crt
{
namespace Http
{
HttpProxyStrategy::HttpProxyStrategy(struct aws_http_proxy_strategy *strategy) : m_strategy(strategy) {}
HttpProxyStrategy::~HttpProxyStrategy() { aws_http_proxy_strategy_release(m_strategy); }
HttpProxyStrategyBasicAuthConfig::HttpProxyStrategyBasicAuthConfig()
: ConnectionType(AwsHttpProxyConnectionType::Legacy), Username(), Password()
{
}
std::shared_ptr<HttpProxyStrategy> HttpProxyStrategy::CreateBasicHttpProxyStrategy(
const HttpProxyStrategyBasicAuthConfig &config,
Allocator *allocator)
{
struct aws_http_proxy_strategy_basic_auth_options basicConfig;
AWS_ZERO_STRUCT(basicConfig);
basicConfig.proxy_connection_type = (enum aws_http_proxy_connection_type)config.ConnectionType;
basicConfig.user_name = aws_byte_cursor_from_c_str(config.Username.c_str());
basicConfig.password = aws_byte_cursor_from_c_str(config.Password.c_str());
struct aws_http_proxy_strategy *strategy =
aws_http_proxy_strategy_new_basic_auth(allocator, &basicConfig);
if (strategy == NULL)
{
return NULL;
}
return Aws::Crt::MakeShared<HttpProxyStrategy>(allocator, strategy);
}
class AdaptiveHttpProxyStrategy : public HttpProxyStrategy
{
public:
AdaptiveHttpProxyStrategy(
Allocator *allocator,
const KerberosGetTokenFunction &kerberosGetToken,
const KerberosGetTokenFunction &ntlmGetCredential,
const NtlmGetTokenFunction &ntlmGetToken)
: HttpProxyStrategy(nullptr), m_Allocator(allocator), m_KerberosGetToken(kerberosGetToken),
m_NtlmGetCredential(ntlmGetCredential), m_NtlmGetToken(ntlmGetToken)
{
}
void SetStrategy(struct aws_http_proxy_strategy *strategy)
{
aws_http_proxy_strategy_release(m_strategy);
m_strategy = strategy;
}
static struct aws_string *NtlmGetCredential(void *user_data, int *error_code)
{
AdaptiveHttpProxyStrategy *strategy = reinterpret_cast<AdaptiveHttpProxyStrategy *>(user_data);
String ntlmCredential;
if (strategy->m_NtlmGetCredential(ntlmCredential))
{
struct aws_string *token =
aws_string_new_from_c_str(strategy->m_Allocator, ntlmCredential.c_str());
if (token != NULL)
{
return token;
}
*error_code = aws_last_error();
}
else
{
*error_code = AWS_ERROR_HTTP_PROXY_STRATEGY_TOKEN_RETRIEVAL_FAILURE;
}
return NULL;
}
static struct aws_string *KerberosGetToken(void *user_data, int *error_code)
{
AdaptiveHttpProxyStrategy *strategy = reinterpret_cast<AdaptiveHttpProxyStrategy *>(user_data);
String kerberosToken;
if (strategy->m_KerberosGetToken(kerberosToken))
{
struct aws_string *token =
aws_string_new_from_c_str(strategy->m_Allocator, kerberosToken.c_str());
if (token != NULL)
{
return token;
}
*error_code = aws_last_error();
}
else
{
*error_code = AWS_ERROR_HTTP_PROXY_STRATEGY_TOKEN_RETRIEVAL_FAILURE;
}
return NULL;
}
static struct aws_string *NtlmGetToken(
void *user_data,
const struct aws_byte_cursor *challenge_cursor,
int *error_code)
{
AdaptiveHttpProxyStrategy *strategy = reinterpret_cast<AdaptiveHttpProxyStrategy *>(user_data);
String ntlmToken;
String challengeToken((const char *)challenge_cursor->ptr, challenge_cursor->len);
if (strategy->m_NtlmGetToken(challengeToken, ntlmToken))
{
struct aws_string *token = aws_string_new_from_c_str(strategy->m_Allocator, ntlmToken.c_str());
if (token != NULL)
{
return token;
}
*error_code = aws_last_error();
}
else
{
*error_code = AWS_ERROR_HTTP_PROXY_STRATEGY_TOKEN_RETRIEVAL_FAILURE;
}
return NULL;
}
private:
Allocator *m_Allocator;
KerberosGetTokenFunction m_KerberosGetToken;
KerberosGetTokenFunction m_NtlmGetCredential;
NtlmGetTokenFunction m_NtlmGetToken;
};
std::shared_ptr<HttpProxyStrategy> HttpProxyStrategy::CreateAdaptiveHttpProxyStrategy(
const HttpProxyStrategyAdaptiveConfig &config,
Allocator *allocator)
{
std::shared_ptr<AdaptiveHttpProxyStrategy> adaptiveStrategy =
Aws::Crt::MakeShared<AdaptiveHttpProxyStrategy>(
allocator, allocator, config.KerberosGetToken, config.NtlmGetCredential, config.NtlmGetToken);
struct aws_http_proxy_strategy_tunneling_kerberos_options kerberosConfig;
AWS_ZERO_STRUCT(kerberosConfig);
kerberosConfig.get_token = AdaptiveHttpProxyStrategy::KerberosGetToken;
kerberosConfig.get_token_user_data = adaptiveStrategy.get();
struct aws_http_proxy_strategy_tunneling_ntlm_options ntlmConfig;
AWS_ZERO_STRUCT(ntlmConfig);
ntlmConfig.get_challenge_token = AdaptiveHttpProxyStrategy::NtlmGetToken;
ntlmConfig.get_token = AdaptiveHttpProxyStrategy::NtlmGetCredential;
ntlmConfig.get_challenge_token_user_data = adaptiveStrategy.get();
struct aws_http_proxy_strategy_tunneling_adaptive_options adaptiveConfig;
AWS_ZERO_STRUCT(adaptiveConfig);
if (config.KerberosGetToken)
{
adaptiveConfig.kerberos_options = &kerberosConfig;
}
if (config.NtlmGetToken)
{
adaptiveConfig.ntlm_options = &ntlmConfig;
}
struct aws_http_proxy_strategy *strategy =
aws_http_proxy_strategy_new_tunneling_adaptive(allocator, &adaptiveConfig);
if (strategy == NULL)
{
return NULL;
}
adaptiveStrategy->SetStrategy(strategy);
return adaptiveStrategy;
}
} // namespace Http
} // namespace Crt
} // namespace Aws
|