11/*
2- * Copyright (C) 2019 HERE Europe B.V.
2+ * Copyright (C) 2019-2026 HERE Europe B.V.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919
2020#include " AuthenticationTestUtils.h"
2121
22- #include < thread>
2322#include < future>
23+ #include < thread>
2424
2525#ifndef WIN32
2626#include < unistd.h>
2727#endif
2828
29- #include < rapidjson/document.h >
30- #include < rapidjson/istreamwrapper.h >
31- #include < rapidjson/stringbuffer.h >
32- # include < rapidjson/writer.h >
29+ #include < boost/json/parse.hpp >
30+ #include < boost/json/serialize.hpp >
31+ #include < boost/json/value.hpp >
32+
3333#include < olp/core/http/HttpStatusCode.h>
3434#include < olp/core/http/Network.h>
3535#include < olp/core/logging/Log.h>
3636#include < testutils/CustomParameters.hpp>
3737#include " TestConstants.h"
3838
39- using namespace ::olp::authentication;
40-
4139namespace {
40+
41+ using olp::authentication::kAccessToken ;
42+ using olp::authentication::kAndParam ;
43+ using olp::authentication::kEqualsParam ;
44+ using olp::authentication::kHereAccountStagingURL ;
45+ using olp::authentication::kMaxRetryCount ;
46+ using olp::authentication::kQuestionParam ;
47+ using olp::authentication::kRetryDelayInSecs ;
48+ using olp::authentication::kTestUserName ;
49+
4250constexpr auto kFacebookUrl = " https://graph.facebook.com/v2.12" ;
4351constexpr auto kId = " id" ;
4452
@@ -79,8 +87,8 @@ bool AuthenticationTestUtils::CreateFacebookTestUser(
7987 unsigned int retry = 0u ;
8088 do {
8189 if (retry > 0u ) {
82- OLP_SDK_LOG_WARNING (__func__, " Request retry attempted ( " << retry
83- << " )" );
90+ OLP_SDK_LOG_WARNING (__func__,
91+ " Request retry attempted ( " << retry << " )" );
8492 std::this_thread::sleep_for (
8593 std::chrono::seconds (retry * kRetryDelayInSecs ));
8694 }
@@ -89,27 +97,26 @@ bool AuthenticationTestUtils::CreateFacebookTestUser(
8997
9098 std::promise<void > promise;
9199 auto future = promise.get_future ();
92- network.Send (request, payload,
93- [payload, &promise,
94- &user](const olp::http::NetworkResponse &network_response) {
95- user.token .status = network_response.GetStatus ();
96- if (user.token .status == olp::http::HttpStatusCode::OK) {
97- auto document = std::make_shared<rapidjson::Document>();
98- rapidjson::IStreamWrapper stream (*payload);
99- document->ParseStream (stream);
100- const bool is_valid =
101- !document->HasParseError () &&
102- document->HasMember (kAccessToken .c_str ()) &&
103- document->HasMember (kId );
104-
105- if (is_valid) {
106- user.token .access_token =
107- (*document)[kAccessToken .c_str ()].GetString ();
108- user.id = (*document)[kId ].GetString ();
109- }
110- }
111- promise.set_value ();
112- });
100+ network.Send (
101+ request, payload,
102+ [payload, &promise,
103+ &user](const olp::http::NetworkResponse &network_response) {
104+ user.token .status = network_response.GetStatus ();
105+ if (user.token .status == olp::http::HttpStatusCode::OK) {
106+ boost::json::error_code ec;
107+ auto document = boost::json::parse (*payload, ec);
108+ const bool is_valid = !ec.failed () && document.is_object () &&
109+ document.as_object ().contains (kAccessToken ) &&
110+ document.as_object ().contains (kId );
111+
112+ if (is_valid) {
113+ user.token .access_token =
114+ document.as_object ()[kAccessToken ].get_string ().c_str ();
115+ user.id = document.as_object ()[kId ].get_string ().c_str ();
116+ }
117+ }
118+ promise.set_value ();
119+ });
113120 future.wait ();
114121 } while ((user.token .status < 0 ) && (++retry < kMaxRetryCount ));
115122
@@ -132,8 +139,8 @@ bool AuthenticationTestUtils::DeleteFacebookTestUser(
132139 unsigned int retry = 0u ;
133140 do {
134141 if (retry > 0u ) {
135- OLP_SDK_LOG_WARNING (__func__, " Request retry attempted ( " << retry
136- << " )" );
142+ OLP_SDK_LOG_WARNING (__func__,
143+ " Request retry attempted ( " << retry << " )" );
137144 std::this_thread::sleep_for (
138145 std::chrono::seconds (retry * kRetryDelayInSecs ));
139146 }
@@ -220,8 +227,8 @@ bool AuthenticationTestUtils::GetAccessTokenImpl(
220227 unsigned int retry = 0u ;
221228 do {
222229 if (retry > 0u ) {
223- OLP_SDK_LOG_WARNING (__func__, " Request retry attempted ( " << retry
224- << " )" );
230+ OLP_SDK_LOG_WARNING (__func__,
231+ " Request retry attempted ( " << retry << " )" );
225232 std::this_thread::sleep_for (
226233 std::chrono::seconds (retry * kRetryDelayInSecs ));
227234 }
@@ -230,23 +237,23 @@ bool AuthenticationTestUtils::GetAccessTokenImpl(
230237
231238 std::promise<void > promise;
232239 auto future = promise.get_future ();
233- network.Send (request, payload,
234- [payload, &promise ,
235- &token]( const olp::http::NetworkResponse &network_response) {
236- token. status = network_response. GetStatus ();
237- if ( token.status == olp::http::HttpStatusCode::OK) {
238- auto document = std::make_shared<rapidjson::Document>();
239- rapidjson::IStreamWrapper stream (*payload) ;
240- document-> ParseStream (stream );
241- bool is_valid = !document-> HasParseError () &&
242- document-> HasMember ( kAccessToken . c_str () );
243- if (is_valid) {
244- token.access_token =
245- (* document)[kAccessToken . c_str ()]. GetString ();
246- }
247- }
248- promise.set_value ();
249- });
240+ network.Send (
241+ request, payload ,
242+ [payload, &promise,
243+ & token]( const olp::http::NetworkResponse & network_response) {
244+ token.status = network_response. GetStatus ();
245+ if (token. status == olp::http::HttpStatusCode::OK) {
246+ boost::json::error_code ec ;
247+ auto document = boost::json::parse (*payload, ec );
248+ const bool is_valid = !ec. failed () && document. is_object () &&
249+ document. as_object (). contains ( kAccessToken );
250+ if (is_valid) {
251+ token.access_token =
252+ document. as_object ( )[kAccessToken ]. get_string (). c_str ();
253+ }
254+ }
255+ promise.set_value ();
256+ });
250257 future.wait ();
251258 } while ((token.status < 0 ) && (++retry < kMaxRetryCount ));
252259
0 commit comments