Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nova_token_auth.cpp « wsgate - github.com/FreeRDP/FreeRDP-WebConnect.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3974ec6198c127bba7fc1092235db928c8aded39 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* vim: set et ts=4 sw=4 cindent:
 *
 * Copyright 2013 Cloudbase Solutions Srl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sstream>
#include <cpprest/http_client.h>
#include <cpprest/json.h>

#include "nova_token_auth.hpp"
#include <iostream>

using namespace pplx;
using namespace std;
using namespace web;
using namespace wsgate;
using namespace utility::conversions;

class nova_console_token_auth_impl : public nova_console_token_auth
{
private:
    web::http::http_response execute_request_and_get_response(
        web::http::client::http_client& client,
        web::http::http_request& request);

    web::json::value get_json_from_response(web::http::http_response response);

    std::pair<std::string, std::string> get_auth_token_data_v2(std::string osAuthUrl,
                                                                           std::string osUserName,
                                                                           std::string osPassword,
                                                                           std::string osTenantName,
                                                                           std::string osRegion);

    std::pair<std::string, std::string> get_auth_token_data_v3(std::string osAuthUrl,
                                                                           std::string osUserName,
                                                                           std::string osPassword,
                                                                           std::string osTenantName,
                                                                           std::string osProjectID,
                                                                           std::string osRegion);

    web::json::value get_console_token_data(std::string authToken,
                                            std::string novaUrl,
                                            std::string consoleToken);

public:
    virtual nova_console_info get_console_info(std::string osAuthUrl,
                                               std::string osUserName,
                                               std::string osPassword,
                                               std::string osTenantName,
                                               std::string osProjectID,
                                               std::string consoleToken,
                                               std::string keystoneVersion,
                                               std::string osRegion);
};



http::http_response nova_console_token_auth_impl::execute_request_and_get_response(
    http::client::http_client& client,
    http::http_request& request)
{
    auto response_task = client.request(request);
    response_task.wait();
    auto response = response_task.get();

    if(response.status_code() >= 400)
    {
        throw http_exception(response.status_code(), to_utf8string(response.reason_phrase()));
    }

    return response;
}

json::value nova_console_token_auth_impl::get_json_from_response(
    http::http_response response)
{
    auto json_task = response.extract_json();
    json_task.wait();

    return json_task.get();
}

std::pair<std::string, std::string> nova_console_token_auth_impl::get_auth_token_data_v2(
    string osAuthUrl, string osUserName,
    string osPassword, string osTenantName,
    string osRegion)
{
    auto jsonRequestBody = json::value::object();
    auto auth = json::value::object();
    auto cred = json::value::object();
    cred[U("username")] = json::value::string(to_string_t(osUserName));
    cred[U("password")] = json::value::string(to_string_t(osPassword));
    auth[U("passwordCredentials")] = cred;
    auth[U("tenantName")] = json::value::string(to_string_t(osTenantName));
    jsonRequestBody[U("auth")] = auth;
    http::http_request request(http::methods::POST);
    request.set_request_uri(U("tokens"));
    request.headers().add(http::header_names::accept, U("application/json"));
    request.headers().set_content_type(U("application/json"));
    request.set_body(jsonRequestBody);

    http::client::http_client client(to_string_t(osAuthUrl));
    auto response_json = get_json_from_response(execute_request_and_get_response(client, request));

    utility::string_t authToken;
    utility::string_t novaUrl;
    //get the authentication token
    authToken = response_json[U("access")][U("token")][U("id")].as_string();

    //get the nova api endpoint
    for (auto serviceCatalog : response_json[U("access")][U("serviceCatalog")].as_array())
        if (serviceCatalog[U("name")].as_string() == U("nova")){
            if (osRegion.empty()){
                novaUrl = serviceCatalog[U("endpoints")][0][U("adminURL")].as_string();
            }
            else{
                for (auto endpoint : serviceCatalog[U("endpoints")].as_array()){
                    if (endpoint[U("region")].as_string() == to_string_t(osRegion)){
                        novaUrl = endpoint[U("adminURL")].as_string();
                    }
                }
            }
        }
            

    return std::pair<std::string, std::string>(
        to_utf8string(authToken),
        to_utf8string(novaUrl));
}

std::pair<std::string, std::string> nova_console_token_auth_impl::get_auth_token_data_v3(
    string osAuthUrl, string osUserName,
    string osPassword, string osTenantName,
    string osProjectID,
    string osRegion)
{
    auto jsonRequestBody = json::value::object();
    auto auth = json::value::object();
    auto identity = json::value::object();
    auto methods = json::value::array();
    methods[0] = json::value::string(to_string_t("password"));

    auto password = json::value::object();
    auto user = json::value::object();
    user[U("name")] = json::value::string(to_string_t(osUserName));
    user[U("password")] = json::value::string(to_string_t(osPassword));

    auto domain = json::value::object();
    domain[U("id")] = json::value::string(to_string_t("default"));

    user[U("domain")] = domain;
    password[U("user")] = user;
    identity[U("methods")] = methods;
    identity[U("password")] = password;

    auto scope = json::value::object();
    auto project = json::value::object();
    if (!osTenantName.empty())
    {
        project[U("name")] = json::value::string(to_string_t(osTenantName));
    }
    if (!osProjectID.empty())
    {
        project[U("id")] = json::value::string(to_string_t(osProjectID));
    }
    project[U("domain")] = domain;
    scope[U("project")] = project;

    auth[U("identity")] = identity;
    auth[U("scope")] = scope;
    jsonRequestBody[U("auth")] = auth;

    http::http_request request(http::methods::POST);
    request.set_request_uri(U("auth/tokens"));
    request.headers().add(http::header_names::accept, U("application/json"));
    request.headers().set_content_type(U("application/json"));
    request.set_body(jsonRequestBody);

    http::client::http_client client(to_string_t(osAuthUrl));
    auto response = execute_request_and_get_response(client, request);
    auto response_json = get_json_from_response(response);

    utility::string_t authToken;
    utility::string_t novaUrl;
    //get the authentication token
    authToken = response.headers()[U("X-Subject-Token")];

    //get the nova api endpoint
    for (auto serviceCatalog : response_json[U("token")][U("catalog")].as_array())
        if (serviceCatalog[U("name")].as_string() == U("nova"))
            for (auto endpoint : serviceCatalog[U("endpoints")].as_array())
                if (endpoint[U("interface")].as_string() == U("admin") &&
                    (endpoint[U("region")].as_string() == to_string_t(osRegion) || osRegion.empty())){
                        novaUrl = endpoint[U("url")].as_string();
                }

    return std::pair<std::string, std::string>(
        to_utf8string(authToken),
        to_utf8string(novaUrl));
}

json::value nova_console_token_auth_impl::get_console_token_data(
    string authToken, string novaUrl, string consoleToken)
{
    http::client::http_client client(to_string_t(novaUrl));
    http::uri_builder console_token_uri;
    console_token_uri.append(U("os-console-auth-tokens"));
    console_token_uri.append(to_string_t(consoleToken));

    http::http_request request(http::methods::GET);
    request.set_request_uri(console_token_uri.to_string());
    request.headers().add(U("X-Auth-Token"), to_string_t(authToken));
    request.headers().add(http::header_names::accept, U("application/json"));
    request.headers().set_content_type(U("application/json"));

    return get_json_from_response(execute_request_and_get_response(client, request));
}

nova_console_info nova_console_token_auth_impl::get_console_info(
    std::string osAuthUrl, std::string osUserName,
    std::string osPassword, std::string osTenantName,
    std::string osProjectID,
    std::string consoleToken, std::string keystoneVersion,
    std::string osRegion)
{
    std::string authToken;
    std::string novaUrl;

    if (keystoneVersion == KEYSTONE_V2){
        std::tie(authToken, novaUrl) = get_auth_token_data_v2(osAuthUrl,
                                                              osUserName,
                                                              osPassword,
                                                              osTenantName,
                                                              osRegion);
    }
    else if (keystoneVersion == KEYSTONE_V3){
        std::tie(authToken, novaUrl) = get_auth_token_data_v3(osAuthUrl,
                                                              osUserName,
                                                              osPassword,
                                                              osTenantName,
                                                              osProjectID,
                                                              osRegion);
    }
    else{
        throw std::invalid_argument("Unknown Keystone version");
    }

    nova_console_info info;

    auto consoleTokenData = get_console_token_data(
        authToken,
        novaUrl,
        consoleToken);

    info.host = to_utf8string(consoleTokenData[U("console")][U("host")].as_string());

    auto portValue = consoleTokenData[U("console")][U("port")];
    if(portValue.is_string())
    {
        istringstream(to_utf8string(portValue.as_string())) >> info.port;
    }
    else
    {
        info.port = portValue.as_integer();
    }

    auto internalAccessPathValue = consoleTokenData[U("console")]
        [U("internal_access_path")];

    if(internalAccessPathValue.is_string())
    {
        info.internal_access_path = to_utf8string(internalAccessPathValue.as_string());
    }

    return info;
}


nova_console_token_auth* nova_console_token_auth_factory::instance = NULL;


nova_console_token_auth* nova_console_token_auth_factory::get_instance()
{
    if(!instance)
        instance = new nova_console_token_auth_impl();

    return instance;
}