Skip to content

Commit d292b1f

Browse files
authored
Change SignOut method to a sync task (#1289)
Change using Network object directly to using OlpClient's CallApi method to make requests. Additionally added a check for a network_request_handler variable. Move the request to a lambda function wich is executed by TaskScheduler. Replace client::OlpClient type with auto. Mark credentials object as unused. Add a test for checking SignOut method with an empty access_token as a parameter. Remove a test which checks Network class(send method) as it isn't a part of AuthenticationClientImpl now. Relates-To: OLPEDGE-2021 Signed-off-by: Yevhenii Dudnyk <[email protected]>
1 parent ad2c28b commit d292b1f

File tree

2 files changed

+60
-75
lines changed

2 files changed

+60
-75
lines changed

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ client::OlpClient::RequestBodyType GenerateAppleSignInBody(
140140
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
141141
}
142142

143-
std::string GenerateBearerHeader(const std::string& bearer_token) {
144-
return std::string(http::kBearer + std::string(" ") + bearer_token);
145-
}
146-
147143
client::HttpResponse CallApi(const client::OlpClient& client,
148144
const std::string& endpoint,
149145
client::CancellationContext context,
@@ -305,7 +301,7 @@ client::CancellationToken AuthenticationClientImpl::SignInClient(
305301
return client::ApiError::Cancelled();
306302
}
307303

308-
client::OlpClient client = CreateOlpClient(settings_, boost::none, false);
304+
auto client = CreateOlpClient(settings_, boost::none, false);
309305

310306
RequestTimer timer = CreateRequestTimer(client, context);
311307

@@ -438,7 +434,7 @@ client::CancellationToken AuthenticationClientImpl::SignInApple(
438434
return client::ApiError::Cancelled();
439435
}
440436

441-
client::OlpClient client = CreateOlpClient(settings_, boost::none);
437+
auto client = CreateOlpClient(settings_, boost::none);
442438

443439
auto auth_response = CallApi(client, kOauthEndpoint, context,
444440
properties.GetAccessToken(), request_body);
@@ -499,7 +495,7 @@ client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
499495
return client::ApiError::Cancelled();
500496
}
501497

502-
client::OlpClient client = CreateOlpClient(settings_, boost::none, false);
498+
auto client = CreateOlpClient(settings_, boost::none, false);
503499

504500
RequestTimer timer = CreateRequestTimer(client, context);
505501

@@ -567,7 +563,7 @@ client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
567563
return client::ApiError::Cancelled();
568564
}
569565

570-
client::OlpClient client = CreateOlpClient(settings_, {}, false);
566+
auto client = CreateOlpClient(settings_, {}, false);
571567

572568
const auto url = settings_.token_endpoint_url + kUserEndpoint;
573569
auto auth_header = GenerateAuthorizationHeader(
@@ -600,76 +596,46 @@ client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
600596
client::CancellationToken AuthenticationClientImpl::SignOut(
601597
const AuthenticationCredentials& credentials,
602598
const std::string& access_token, const SignOutUserCallback& callback) {
603-
if (!settings_.network_request_handler) {
604-
thread::ExecuteOrSchedule(settings_.task_scheduler, [callback] {
605-
callback(
606-
client::ApiError::NetworkConnection("Cannot sign out while offline"));
607-
});
608-
return client::CancellationToken();
609-
}
610-
std::weak_ptr<http::Network> weak_network(settings_.network_request_handler);
611-
std::string url = settings_.token_endpoint_url;
612-
url.append(kSignoutEndpoint);
613-
http::NetworkRequest request(url);
614-
auto network_settings =
615-
http::NetworkSettings()
616-
.WithTransferTimeout(settings_.retry_settings.timeout)
617-
.WithConnectionTimeout(settings_.retry_settings.timeout)
618-
.WithProxySettings(settings_.network_proxy_settings.get_value_or({}));
619-
620-
request.WithVerb(http::NetworkRequest::HttpVerb::POST);
621-
request.WithHeader(http::kAuthorizationHeader,
622-
GenerateBearerHeader(access_token));
623-
request.WithHeader(http::kUserAgentHeader, http::kOlpSdkUserAgent);
624-
request.WithSettings(std::move(network_settings));
625-
626-
std::shared_ptr<std::stringstream> payload =
627-
std::make_shared<std::stringstream>();
628-
auto send_outcome = settings_.network_request_handler->Send(
629-
request, payload,
630-
[callback, payload,
631-
credentials](const http::NetworkResponse& network_response) {
632-
auto response_status = network_response.GetStatus();
633-
auto error_msg = network_response.GetError();
634-
635-
if (response_status < 0) {
636-
// Network error response not available
637-
AuthenticationError error(response_status, error_msg);
638-
callback(error);
639-
return;
640-
}
599+
OLP_SDK_CORE_UNUSED(credentials);
600+
using ResponseType = client::ApiResponse<SignOutResult, client::ApiError>;
641601

642-
auto document = std::make_shared<rapidjson::Document>();
643-
rapidjson::IStreamWrapper stream(*payload);
644-
document->ParseStream(stream);
602+
auto sign_out_task =
603+
[=](client::CancellationContext context) -> ResponseType {
604+
if (!settings_.network_request_handler) {
605+
return client::ApiError::NetworkConnection(
606+
"Cannot sign out while offline");
607+
}
645608

646-
std::shared_ptr<SignOutResultImpl> resp_impl =
647-
std::make_shared<SignOutResultImpl>(response_status, error_msg,
648-
document);
649-
SignOutResult response(resp_impl);
650-
callback(response);
651-
});
609+
if (context.IsCancelled()) {
610+
return client::ApiError::Cancelled();
611+
}
652612

653-
if (!send_outcome.IsSuccessful()) {
654-
thread::ExecuteOrSchedule(settings_.task_scheduler, [send_outcome,
655-
callback] {
656-
std::string error_message =
657-
ErrorCodeToString(send_outcome.GetErrorCode());
658-
AuthenticationError result({static_cast<int>(send_outcome.GetErrorCode()),
659-
std::move(error_message)});
660-
callback(result);
661-
});
662-
return client::CancellationToken();
663-
}
613+
client::AuthenticationSettings auth_settings;
614+
auth_settings.token_provider =
615+
[&access_token](client::CancellationContext&) {
616+
return client::OauthToken(access_token, kMaxTime);
617+
};
618+
619+
auto client = CreateOlpClient(settings_, auth_settings, false);
664620

665-
auto request_id = send_outcome.GetRequestId();
666-
return client::CancellationToken([weak_network, request_id]() {
667-
auto network = weak_network.lock();
621+
auto signout_response = client.CallApi(kSignoutEndpoint, "POST", {}, {}, {},
622+
{}, {}, std::move(context));
668623

669-
if (network) {
670-
network->Cancel(request_id);
624+
const auto status = signout_response.GetStatus();
625+
std::string response_text;
626+
signout_response.GetResponse(response_text);
627+
if (status < 0) {
628+
return client::ApiError(status, response_text);
671629
}
672-
});
630+
631+
auto document = std::make_shared<rapidjson::Document>();
632+
document->Parse(response_text.c_str());
633+
return {std::make_shared<SignOutResultImpl>(
634+
status, olp::http::HttpErrorToString(status), document)};
635+
};
636+
637+
return AddTask(settings_.task_scheduler, pending_requests_,
638+
std::move(sign_out_task), std::move(callback));
673639
}
674640

675641
client::CancellationToken AuthenticationClientImpl::IntrospectApp(
@@ -690,7 +656,7 @@ client::CancellationToken AuthenticationClientImpl::IntrospectApp(
690656
return client::OauthToken(access_token, kMaxTime);
691657
};
692658

693-
client::OlpClient client = CreateOlpClient(settings_, auth_settings);
659+
auto client = CreateOlpClient(settings_, auth_settings);
694660

695661
auto http_result = client.CallApi(kIntrospectAppEndpoint, "GET", {}, {}, {},
696662
nullptr, {}, context);
@@ -736,7 +702,7 @@ client::CancellationToken AuthenticationClientImpl::Authorize(
736702
return client::OauthToken(access_token, kMaxTime);
737703
};
738704

739-
client::OlpClient client = CreateOlpClient(settings_, auth_settings);
705+
auto client = CreateOlpClient(settings_, auth_settings);
740706

741707
auto http_result = client.CallApi(kDecisionEndpoint, "POST", {}, {}, {},
742708
GenerateAuthorizeBody(request),
@@ -795,7 +761,7 @@ client::CancellationToken AuthenticationClientImpl::GetMyAccount(
795761
return client::OauthToken(access_token, kMaxTime);
796762
};
797763

798-
client::OlpClient client = CreateOlpClient(settings_, auth_settings);
764+
auto client = CreateOlpClient(settings_, auth_settings);
799765

800766
auto http_result =
801767
client.CallApi(kMyAccountEndpoint, "GET", {}, {}, {}, {}, {}, context);

olp-cpp-sdk-authentication/tests/AuthenticationClientTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ TEST(AuthenticationClientTest, SignUpWithUnsuccessfulSend) {
117117
});
118118
}
119119

120+
TEST(AuthenticationClientTest, SignOutAccessDenied) {
121+
using testing::_;
122+
123+
auth::AuthenticationSettings settings;
124+
settings.network_request_handler = std::make_shared<NetworkMock>();
125+
126+
AuthenticationClientImplTestable auth_impl(settings);
127+
128+
const auth::AuthenticationCredentials credentials("", "");
129+
130+
auth_impl.SignOut(
131+
credentials, {},
132+
[=](const auth::AuthenticationClient::SignOutUserResponse& response) {
133+
EXPECT_FALSE(response.IsSuccessful());
134+
EXPECT_EQ(response.GetError().GetErrorCode(),
135+
client::ErrorCode::AccessDenied);
136+
});
137+
}
138+
120139
TEST(AuthenticationClientTest, Timestamp) {
121140
using testing::_;
122141

0 commit comments

Comments
 (0)