-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathAuthenticationClientImpl.cpp
More file actions
1081 lines (909 loc) · 36.8 KB
/
AuthenticationClientImpl.cpp
File metadata and controls
1081 lines (909 loc) · 36.8 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2020-2025 HERE Europe B.V.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "AuthenticationClientImpl.h"
#include <chrono>
#include <iomanip>
#include <limits>
#include <sstream>
#include <thread>
#include <utility>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "AuthenticationClientUtils.h"
#include "Constants.h"
#include "SignInResultImpl.h"
#include "SignInUserResultImpl.h"
#include "SignOutResultImpl.h"
#include "SignUpResultImpl.h"
#include "olp/core/http/Network.h"
#include "olp/core/http/NetworkConstants.h"
#include "olp/core/http/NetworkResponse.h"
#include "olp/core/http/NetworkUtils.h"
#include "olp/core/logging/Log.h"
#include "olp/core/thread/TaskScheduler.h"
#include "olp/core/utils/Url.h"
namespace olp {
namespace authentication {
namespace {
using RequestBodyData = client::OlpClient::RequestBodyType::element_type;
// Tags
const std::string kOauthEndpoint = "/oauth2/token";
const std::string kSignoutEndpoint = "/logout";
const std::string kTermsEndpoint = "/terms";
const std::string kUserEndpoint = "/user";
const std::string kMyAccountEndpoint = "/user/me";
constexpr auto kTimestampEndpoint = "/timestamp";
constexpr auto kIntrospectAppEndpoint = "/app/me";
constexpr auto kDecisionEndpoint = "/decision/authorize";
// JSON fields
constexpr auto kCountryCode = "countryCode";
constexpr auto kDateOfBirth = "dob";
constexpr auto kEmail = "email";
constexpr auto kFirstName = "firstname";
constexpr auto kGrantType = "grantType";
constexpr auto kScope = "scope";
constexpr auto kDeviceId = "deviceId";
constexpr auto kInviteToken = "inviteToken";
constexpr auto kLanguage = "language";
constexpr auto kLastName = "lastname";
constexpr auto kMarketingEnabled = "marketingEnabled";
constexpr auto kPassword = "password";
constexpr auto kPhoneNumber = "phoneNumber";
constexpr auto kRealm = "realm";
constexpr auto kTermsReacceptanceToken = "termsReacceptanceToken";
constexpr auto kClientId = "clientId";
constexpr auto kGivenName = "givenName";
constexpr auto kFamilyName = "familyName";
constexpr auto kClientGrantType = "client_credentials";
constexpr auto kUserGrantType = "password";
constexpr auto kFacebookGrantType = "facebook";
constexpr auto kArcgisGrantType = "arcgis";
constexpr auto kAppleGrantType = "jwtIssNotHERE";
constexpr auto kRefreshGrantType = "refresh_token";
constexpr auto kServiceId = "serviceId";
constexpr auto kActions = "actions";
constexpr auto kAction = "action";
constexpr auto kResource = "resource";
constexpr auto kDiagnostics = "diagnostics";
constexpr auto kOperator = "operator";
// Values
constexpr auto kErrorWrongTimestamp = 401204;
constexpr auto kLogTag = "AuthenticationClient";
const auto kMaxTime = std::numeric_limits<time_t>::max();
bool HasWrongTimestamp(const SignInResult& result) {
const auto& error_response = result.GetErrorResponse();
const auto status = result.GetStatus();
return status == http::HttpStatusCode::UNAUTHORIZED &&
error_response.code == kErrorWrongTimestamp;
}
void RetryDelay(const client::RetrySettings& retry_settings, size_t retry) {
std::this_thread::sleep_for(retry_settings.backdown_strategy(
std::chrono::milliseconds(retry_settings.initial_backdown_period),
retry));
}
client::OlpClient::RequestBodyType GenerateAppleSignInBody(
const AppleSignInProperties& sign_in_properties) {
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
writer.Key(kGrantType);
writer.String(kAppleGrantType);
auto write_field = [&writer](const char* key, const std::string& value) {
if (!value.empty()) {
writer.Key(key);
writer.String(value.c_str());
}
};
write_field(kClientId, sign_in_properties.GetClientId());
write_field(kRealm, sign_in_properties.GetRealm());
write_field(kGivenName, sign_in_properties.GetFirstname());
write_field(kFamilyName, sign_in_properties.GetLastname());
write_field(kCountryCode, sign_in_properties.GetCountryCode());
write_field(kLanguage, sign_in_properties.GetLanguage());
writer.EndObject();
auto content = data.GetString();
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
}
client::HttpResponse CallApi(const client::OlpClient& client,
const std::string& endpoint,
client::CancellationContext context,
const std::string& auth_header,
client::OlpClient::RequestBodyType body) {
client::OlpClient::ParametersType headers{
{http::kAuthorizationHeader, auth_header}};
return client.CallApi(
endpoint, "POST", {}, std::move(headers), {}, std::move(body),
AuthenticationClientImpl::kApplicationJson, std::move(context));
}
std::string DeduceContentType(const SignInProperties& properties) {
if (properties.custom_body.has_value()) {
return "";
}
return AuthenticationClientImpl::kApplicationJson;
}
} // namespace
AuthenticationClientImpl::RequestTimer::RequestTimer()
: timer_start_{std::chrono::steady_clock::now()},
time_{std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now())} {}
AuthenticationClientImpl::RequestTimer::RequestTimer(std::time_t server_time)
: timer_start_{std::chrono::steady_clock::now()}, time_{server_time} {}
std::time_t AuthenticationClientImpl::RequestTimer::GetRequestTime() const {
const auto now = std::chrono::steady_clock::now();
const auto time_since_start =
std::chrono::duration_cast<std::chrono::seconds>(now - timer_start_);
return time_ + time_since_start.count();
}
AuthenticationClientImpl::AuthenticationClientImpl(
AuthenticationSettings settings)
: client_token_cache_(
std::make_shared<SignInCacheType>(settings.token_cache_limit)),
user_token_cache_(
std::make_shared<SignInUserCacheType>(settings.token_cache_limit)),
settings_(std::move(settings)),
pending_requests_(std::make_shared<client::PendingRequests>()) {}
AuthenticationClientImpl::~AuthenticationClientImpl() {
pending_requests_->CancelAllAndWait();
}
olp::client::HttpResponse AuthenticationClientImpl::CallAuth(
const client::OlpClient& client, const std::string& endpoint,
client::CancellationContext context,
const AuthenticationCredentials& credentials,
client::OlpClient::RequestBodyType body, std::time_t timestamp,
const std::string& content_type) {
// When credentials specify authentication endpoint, it means that
// Authorization header must be created for the corresponding host.
const auto url = [&]() {
if (!credentials.GetEndpointUrl().empty()) {
return credentials.GetEndpointUrl();
}
return settings_.token_endpoint_url + endpoint;
}();
auto auth_header =
GenerateAuthorizationHeader(credentials, url, timestamp, GenerateUid());
client::OlpClient::ParametersType headers = {
{http::kAuthorizationHeader, std::move(auth_header)}};
return client.CallApi(endpoint, "POST", {}, std::move(headers), {},
std::move(body), content_type, std::move(context));
}
SignInResult AuthenticationClientImpl::ParseAuthResponse(
int status, std::stringstream& auth_response) {
auto document = std::make_shared<rapidjson::Document>();
rapidjson::IStreamWrapper stream(auth_response);
document->ParseStream(stream);
return std::make_shared<SignInResultImpl>(
status, olp::http::HttpErrorToString(status), document);
}
SignInUserResult AuthenticationClientImpl::ParseUserAuthResponse(
int status, std::stringstream& auth_response) {
auto document = std::make_shared<rapidjson::Document>();
rapidjson::IStreamWrapper stream(auth_response);
document->ParseStream(stream);
return std::make_shared<SignInUserResultImpl>(
status, olp::http::HttpErrorToString(status), document);
}
template <typename SignInResponseType>
Response<SignInResponseType> AuthenticationClientImpl::GetSignInResponse(
const client::HttpResponse& auth_response,
const client::CancellationContext& context, const std::string& key) {
const auto status = auth_response.GetStatus();
// If a timeout occurred, the cancellation is done through the context.
// So this case needs to be handled independently of context state.
if (status != static_cast<int>(http::ErrorCode::TIMEOUT_ERROR) &&
context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto result = FindInCache<SignInResponseType>(key);
if (result) {
return *result;
}
// Auth response message may be empty in case of unknown errors.
// Fill in the message as a status string representation in this case.
std::string message;
auth_response.GetResponse(message);
if (message.empty()) {
message = http::HttpErrorToString(status);
}
return client::ApiError(status, message);
}
template <>
boost::optional<SignInResult> AuthenticationClientImpl::FindInCache(
const std::string& key) {
return client_token_cache_->locked(
[&](utils::LruCache<std::string, SignInResult>& cache) {
auto it = cache.Find(key);
return it != cache.end() ? boost::make_optional(it->value())
: boost::none;
});
}
template <>
boost::optional<SignInUserResult> AuthenticationClientImpl::FindInCache(
const std::string& key) {
return user_token_cache_->locked(
[&](utils::LruCache<std::string, SignInUserResult>& cache) {
auto it = cache.Find(key);
return it != cache.end() ? boost::make_optional(it->value())
: boost::none;
});
}
template <>
void AuthenticationClientImpl::StoreInCache(const std::string& key,
SignInResult response) {
// Cache the response
client_token_cache_->locked(
[&](utils::LruCache<std::string, SignInResult>& cache) {
return cache.InsertOrAssign(key, response);
});
}
template <>
void AuthenticationClientImpl::StoreInCache(const std::string& key,
SignInUserResult response) {
// Cache the response
user_token_cache_->locked(
[&](utils::LruCache<std::string, SignInUserResult>& cache) {
return cache.InsertOrAssign(key, response);
});
}
client::CancellationToken AuthenticationClientImpl::SignInClient(
AuthenticationCredentials credentials, SignInProperties properties,
SignInClientCallback callback) {
auto task = [=](client::CancellationContext context) -> SignInClientResponse {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot sign in while offline");
}
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto olp_client_host = settings_.token_endpoint_url;
auto endpoint = kOauthEndpoint;
// If credentials contain URL for the token endpoint then override default
// endpoint with it. Construction of the `OlpClient` requires the host part
// of URL, while `CallAuth` method - the rest of URL, hence we need to split
// URL passed in the Credentials object.
const auto credentials_endpoint = credentials.GetEndpointUrl();
const auto maybe_host_and_rest =
olp::utils::Url::ParseHostAndRest(credentials_endpoint);
if (maybe_host_and_rest.has_value()) {
const auto& host_and_rest = maybe_host_and_rest.value();
olp_client_host = host_and_rest.first;
endpoint = host_and_rest.second;
}
// To pass correct URL we need to create and modify local copy of shared
// settings object.
auto settings = settings_;
settings.token_endpoint_url = olp_client_host;
auto client = CreateOlpClient(settings, boost::none, false);
RequestTimer timer = CreateRequestTimer(client, context);
const auto request_body = GenerateClientBody(properties);
const auto content_type = DeduceContentType(properties);
SignInClientResponse response;
const auto& retry_settings = settings_.retry_settings;
for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto auth_response =
CallAuth(client, endpoint, context, credentials, request_body,
timer.GetRequestTime(), content_type);
const auto status = auth_response.GetStatus();
if (status < 0) {
response = GetSignInResponse<SignInResult>(auth_response, context,
credentials.GetKey());
} else {
response = ParseAuthResponse(status, auth_response.GetRawResponse());
}
if (retry_settings.retry_condition(auth_response)) {
RetryDelay(retry_settings, retry);
continue;
}
// In case we can't authorize with system time, retry with the server
// time from response headers (if available).
if (HasWrongTimestamp(response.GetResult())) {
auto server_time = GetTimestampFromHeaders(auth_response.GetHeaders());
if (server_time) {
timer = RequestTimer(*server_time);
continue;
}
}
if (status == http::HttpStatusCode::OK) {
StoreInCache(credentials.GetKey(), response.GetResult());
}
break;
}
return response;
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
std::move(callback));
}
TimeResponse AuthenticationClientImpl::ParseTimeResponse(
std::stringstream& payload) {
rapidjson::Document document;
rapidjson::IStreamWrapper stream(payload);
document.ParseStream(stream);
if (!document.IsObject()) {
return client::ApiError(client::ErrorCode::InternalFailure,
"JSON document root is not an Object type");
}
const auto timestamp_it = document.FindMember("timestamp");
if (timestamp_it == document.MemberEnd() || !timestamp_it->value.IsUint()) {
return client::ApiError(
client::ErrorCode::InternalFailure,
"JSON document must contain timestamp integer field");
}
return timestamp_it->value.GetUint();
}
TimeResponse AuthenticationClientImpl::GetTimeFromServer(
client::CancellationContext context,
const client::OlpClient& client) const {
auto http_result = client.CallApi(kTimestampEndpoint, "GET", {}, {}, {},
nullptr, {}, context);
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
auto response = http_result.GetResponseAsString();
OLP_SDK_LOG_WARNING_F(
kLogTag, "Failed to get time from server, status=%d, response='%s'",
http_result.GetStatus(), response.c_str());
return client::ApiError(http_result.GetStatus(), response);
}
auto server_time = ParseTimeResponse(http_result.GetRawResponse());
if (!server_time) {
const auto& error = server_time.GetError();
const auto& message = error.GetMessage();
OLP_SDK_LOG_WARNING_F(kLogTag,
"Failed to decode time from server, message='%s'",
message.c_str());
}
return server_time;
}
client::CancellationToken AuthenticationClientImpl::SignInHereUser(
const AuthenticationCredentials& credentials,
const UserProperties& properties, const SignInUserCallback& callback) {
return HandleUserRequest(credentials, kOauthEndpoint,
GenerateUserBody(properties), callback);
}
client::CancellationToken AuthenticationClientImpl::SignInFederated(
AuthenticationCredentials credentials, std::string request_body,
SignInUserCallback callback) {
auto payload = std::make_shared<RequestBodyData>(request_body.size());
std::memcpy(payload->data(), request_body.data(), payload->size());
return HandleUserRequest(credentials, kOauthEndpoint, payload, callback);
}
client::CancellationToken AuthenticationClientImpl::SignInApple(
AppleSignInProperties properties, SignInUserCallback callback) {
auto request_body = GenerateAppleSignInBody(properties);
auto task = [=](client::CancellationContext context) -> SignInUserResponse {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot handle user request while offline");
}
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto client = CreateOlpClient(settings_, boost::none);
auto auth_response = CallApi(client, kOauthEndpoint, context,
properties.GetAccessToken(), request_body);
auto status = auth_response.GetStatus();
if (status < 0) {
return GetSignInResponse<SignInUserResult>(auth_response, context,
properties.GetClientId());
}
auto response =
ParseUserAuthResponse(status, auth_response.GetRawResponse());
if (status == http::HttpStatusCode::OK) {
StoreInCache(properties.GetClientId(), response);
}
return response;
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
std::move(callback));
}
client::CancellationToken AuthenticationClientImpl::SignInRefresh(
const AuthenticationCredentials& credentials,
const RefreshProperties& properties, const SignInUserCallback& callback) {
return HandleUserRequest(credentials, kOauthEndpoint,
GenerateRefreshBody(properties), callback);
}
client::CancellationToken AuthenticationClientImpl::SignInFederated(
const AuthenticationCredentials& credentials,
const FederatedSignInType& type, const FederatedProperties& properties,
const SignInUserCallback& callback) {
return HandleUserRequest(credentials, kOauthEndpoint,
GenerateFederatedBody(type, properties), callback);
}
client::CancellationToken AuthenticationClientImpl::AcceptTerms(
const AuthenticationCredentials& credentials,
const std::string& reacceptance_token, const SignInUserCallback& callback) {
return HandleUserRequest(credentials, kTermsEndpoint,
GenerateAcceptTermBody(reacceptance_token),
callback);
}
client::CancellationToken AuthenticationClientImpl::HandleUserRequest(
const AuthenticationCredentials& credentials, const std::string& endpoint,
const client::OlpClient::RequestBodyType& request_body,
const SignInUserCallback& callback) {
auto task = [=](client::CancellationContext context) -> SignInUserResponse {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot handle user request while offline");
}
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto client = CreateOlpClient(settings_, boost::none, false);
RequestTimer timer = CreateRequestTimer(client, context);
SignInUserResult response;
const auto& retry_settings = settings_.retry_settings;
for (auto retry = 0; retry < retry_settings.max_attempts; ++retry) {
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto auth_response = CallAuth(client, endpoint, context, credentials,
request_body, timer.GetRequestTime());
auto status = auth_response.GetStatus();
if (status < 0) {
return GetSignInResponse<SignInUserResult>(auth_response, context,
credentials.GetKey());
}
response = ParseUserAuthResponse(status, auth_response.GetRawResponse());
if (retry_settings.retry_condition(auth_response)) {
RetryDelay(retry_settings, retry);
continue;
}
// In case we can't authorize with system time, retry with the server
// time from response headers (if available).
if (HasWrongTimestamp(response)) {
auto server_time = GetTimestampFromHeaders(auth_response.GetHeaders());
if (server_time) {
timer = RequestTimer(*server_time);
continue;
}
}
if (status == http::HttpStatusCode::OK) {
StoreInCache(credentials.GetKey(), response);
}
break;
}
return response;
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
callback);
}
client::CancellationToken AuthenticationClientImpl::SignUpHereUser(
const AuthenticationCredentials& credentials,
const SignUpProperties& properties, const SignUpCallback& callback) {
using ResponseType = client::ApiResponse<SignUpResult, client::ApiError>;
auto signup_task = [=](client::CancellationContext context) -> ResponseType {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot sign up while offline");
}
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
auto client = CreateOlpClient(settings_, {}, false);
const auto url = settings_.token_endpoint_url + kUserEndpoint;
auto auth_header = GenerateAuthorizationHeader(
credentials, url, std::time(nullptr), GenerateUid());
client::OlpClient::ParametersType headers = {
{http::kAuthorizationHeader, std::move(auth_header)}};
auto signup_response = client.CallApi(kUserEndpoint, "POST", {}, headers,
{}, GenerateSignUpBody(properties),
kApplicationJson, std::move(context));
const auto status = signup_response.GetStatus();
std::string response_text;
signup_response.GetResponse(response_text);
if (status < 0) {
return client::ApiError(status, response_text);
}
auto document = std::make_shared<rapidjson::Document>();
document->Parse(response_text.c_str());
return {std::make_shared<SignUpResultImpl>(
status, olp::http::HttpErrorToString(status), document)};
};
return AddTask(settings_.task_scheduler, pending_requests_,
std::move(signup_task), callback);
}
client::CancellationToken AuthenticationClientImpl::SignOut(
const AuthenticationCredentials& credentials,
const std::string& access_token, const SignOutUserCallback& callback) {
OLP_SDK_CORE_UNUSED(credentials);
using ResponseType = client::ApiResponse<SignOutResult, client::ApiError>;
auto sign_out_task =
[=](client::CancellationContext context) -> ResponseType {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot sign out while offline");
}
if (context.IsCancelled()) {
return client::ApiError::Cancelled();
}
client::AuthenticationSettings auth_settings;
auth_settings.token_provider =
[&access_token](client::CancellationContext&) {
return client::OauthToken(access_token, kMaxTime);
};
auto client = CreateOlpClient(settings_, auth_settings, false);
auto signout_response = client.CallApi(kSignoutEndpoint, "POST", {}, {}, {},
{}, {}, std::move(context));
const auto status = signout_response.GetStatus();
std::string response_text;
signout_response.GetResponse(response_text);
if (status < 0) {
return client::ApiError(status, response_text);
}
auto document = std::make_shared<rapidjson::Document>();
document->Parse(response_text.c_str());
return {std::make_shared<SignOutResultImpl>(
status, olp::http::HttpErrorToString(status), document)};
};
return AddTask(settings_.task_scheduler, pending_requests_,
std::move(sign_out_task), callback);
}
client::CancellationToken AuthenticationClientImpl::IntrospectApp(
std::string access_token, IntrospectAppCallback callback) {
using ResponseType =
client::ApiResponse<IntrospectAppResult, client::ApiError>;
auto introspect_app_task =
[=](client::CancellationContext context) -> ResponseType {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Cannot introspect app while offline");
}
client::AuthenticationSettings auth_settings;
auth_settings.token_provider =
[&access_token](client::CancellationContext&) {
return client::OauthToken(access_token, kMaxTime);
};
auto client = CreateOlpClient(settings_, auth_settings);
auto http_result = client.CallApi(kIntrospectAppEndpoint, "GET", {}, {}, {},
nullptr, {}, context);
rapidjson::Document document;
rapidjson::IStreamWrapper stream(http_result.GetRawResponse());
document.ParseStream(stream);
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
// HttpResult response can be error message or valid json with it.
std::string msg = http_result.GetResponseAsString();
if (!document.HasParseError() && document.HasMember(Constants::MESSAGE)) {
msg = document[Constants::MESSAGE].GetString();
}
return client::ApiError({http_result.GetStatus(), msg});
}
if (document.HasParseError()) {
return client::ApiError({static_cast<int>(http::ErrorCode::UNKNOWN_ERROR),
"Failed to parse response"});
}
return GetIntrospectAppResult(document);
};
return AddTask(settings_.task_scheduler, pending_requests_,
std::move(introspect_app_task), std::move(callback));
}
client::CancellationToken AuthenticationClientImpl::Authorize(
std::string access_token, AuthorizeRequest request,
AuthorizeCallback callback) {
using ResponseType = client::ApiResponse<AuthorizeResult, client::ApiError>;
auto task = [=](client::CancellationContext context) -> ResponseType {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Can not send request while offline");
}
client::AuthenticationSettings auth_settings;
auth_settings.token_provider =
[&access_token](client::CancellationContext&) {
return client::OauthToken(access_token, kMaxTime);
};
auto client = CreateOlpClient(settings_, auth_settings);
auto http_result = client.CallApi(kDecisionEndpoint, "POST", {}, {}, {},
GenerateAuthorizeBody(request),
kApplicationJson, context);
rapidjson::Document document;
rapidjson::IStreamWrapper stream(http_result.GetRawResponse());
document.ParseStream(stream);
if (http_result.GetStatus() != http::HttpStatusCode::OK) {
// HttpResult response can be error message or valid json with it.
std::string msg = http_result.GetResponseAsString();
if (!document.HasParseError() && document.HasMember(Constants::MESSAGE)) {
msg = document[Constants::MESSAGE].GetString();
}
return client::ApiError({http_result.GetStatus(), msg});
} else if (!document.HasParseError() &&
document.HasMember(Constants::ERROR_CODE) &&
document[Constants::ERROR_CODE].IsInt()) {
std::string msg =
"Error code: " +
std::to_string(document[Constants::ERROR_CODE].GetInt());
if (document.HasMember(Constants::MESSAGE)) {
msg.append(" (");
msg.append(document[Constants::MESSAGE].GetString());
msg.append(")");
}
return client::ApiError(
{static_cast<int>(http::ErrorCode::UNKNOWN_ERROR), msg});
}
if (document.HasParseError()) {
return client::ApiError({static_cast<int>(http::ErrorCode::UNKNOWN_ERROR),
"Failed to parse response"});
}
return GetAuthorizeResult(document);
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
std::move(callback));
}
client::CancellationToken AuthenticationClientImpl::GetMyAccount(
std::string access_token, UserAccountInfoCallback callback) {
auto task =
[=](client::CancellationContext context) -> UserAccountInfoResponse {
if (!settings_.network_request_handler) {
return client::ApiError::NetworkConnection(
"Can not send request while offline");
}
client::AuthenticationSettings auth_settings;
auth_settings.token_provider =
[&access_token](client::CancellationContext&) {
return client::OauthToken(access_token, kMaxTime);
};
auto client = CreateOlpClient(settings_, auth_settings);
auto http_result =
client.CallApi(kMyAccountEndpoint, "GET", {}, {}, {}, {}, {}, context);
return GetUserAccountInfoResponse(http_result);
};
return AddTask(settings_.task_scheduler, pending_requests_, std::move(task),
std::move(callback));
}
client::OlpClient::RequestBodyType AuthenticationClientImpl::GenerateClientBody(
const SignInProperties& properties) {
if (properties.custom_body.has_value()) {
const auto& content = properties.custom_body.value();
return std::make_shared<RequestBodyData>(content.data(),
content.data() + content.size());
};
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
writer.Key(kGrantType);
writer.String(kClientGrantType);
auto expires_in = static_cast<unsigned int>(properties.expires_in.count());
if (expires_in > 0) {
writer.Key(Constants::EXPIRES_IN);
writer.Uint(expires_in);
}
if (properties.scope) {
writer.Key(kScope);
writer.String(properties.scope.get().c_str());
}
if (properties.device_id) {
writer.Key(kDeviceId);
writer.String(properties.device_id.get().c_str());
}
writer.EndObject();
auto content = data.GetString();
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
}
client::OlpClient::RequestBodyType AuthenticationClientImpl::GenerateUserBody(
const UserProperties& properties) {
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
writer.Key(kGrantType);
writer.String(kUserGrantType);
if (!properties.email.empty()) {
writer.Key(kEmail);
writer.String(properties.email.c_str());
}
if (!properties.password.empty()) {
writer.Key(kPassword);
writer.String(properties.password.c_str());
}
if (properties.expires_in > 0) {
writer.Key(Constants::EXPIRES_IN);
writer.Uint(properties.expires_in);
}
writer.EndObject();
auto content = data.GetString();
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
}
client::OlpClient::RequestBodyType
AuthenticationClientImpl::GenerateFederatedBody(
const FederatedSignInType type, const FederatedProperties& properties) {
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
writer.Key(kGrantType);
switch (type) {
case FederatedSignInType::FacebookSignIn:
writer.String(kFacebookGrantType);
break;
case FederatedSignInType::ArcgisSignIn:
writer.String(kArcgisGrantType);
break;
default:
return nullptr;
}
if (!properties.access_token.empty()) {
writer.Key(Constants::ACCESS_TOKEN);
writer.String(properties.access_token.c_str());
}
if (!properties.country_code.empty()) {
writer.Key(kCountryCode);
writer.String(properties.country_code.c_str());
}
if (!properties.language.empty()) {
writer.Key(kLanguage);
writer.String(properties.language.c_str());
}
if (!properties.email.empty()) {
writer.Key(kEmail);
writer.String(properties.email.c_str());
}
if (properties.expires_in > 0) {
writer.Key(Constants::EXPIRES_IN);
writer.Uint(properties.expires_in);
}
writer.EndObject();
auto content = data.GetString();
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
}
client::OlpClient::RequestBodyType
AuthenticationClientImpl::GenerateRefreshBody(
const RefreshProperties& properties) {
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
writer.Key(kGrantType);
writer.String(kRefreshGrantType);
if (!properties.access_token.empty()) {
writer.Key(Constants::ACCESS_TOKEN);
writer.String(properties.access_token.c_str());
}
if (!properties.refresh_token.empty()) {
writer.Key(Constants::REFRESH_TOKEN);
writer.String(properties.refresh_token.c_str());
}
if (properties.expires_in > 0) {
writer.Key(Constants::EXPIRES_IN);
writer.Uint(properties.expires_in);
}
writer.EndObject();
auto content = data.GetString();
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
}
client::OlpClient::RequestBodyType AuthenticationClientImpl::GenerateSignUpBody(
const SignUpProperties& properties) {
rapidjson::StringBuffer data;
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
writer.StartObject();
if (!properties.email.empty()) {
writer.Key(kEmail);
writer.String(properties.email.c_str());
}
if (!properties.password.empty()) {
writer.Key(kPassword);
writer.String(properties.password.c_str());
}
if (!properties.date_of_birth.empty()) {
writer.Key(kDateOfBirth);
writer.String(properties.date_of_birth.c_str());
}
if (!properties.first_name.empty()) {
writer.Key(kFirstName);
writer.String(properties.first_name.c_str());
}
if (!properties.last_name.empty()) {
writer.Key(kLastName);
writer.String(properties.last_name.c_str());
}
if (!properties.country_code.empty()) {
writer.Key(kCountryCode);
writer.String(properties.country_code.c_str());
}
if (!properties.language.empty()) {
writer.Key(kLanguage);
writer.String(properties.language.c_str());
}
if (properties.marketing_enabled) {
writer.Key(kMarketingEnabled);
writer.Bool(true);
}
if (!properties.phone_number.empty()) {
writer.Key(kPhoneNumber);
writer.String(properties.phone_number.c_str());
}
if (!properties.realm.empty()) {
writer.Key(kRealm);
writer.String(properties.realm.c_str());
}
if (!properties.invite_token.empty()) {
writer.Key(kInviteToken);
writer.String(properties.invite_token.c_str());
}
writer.EndObject();
auto content = data.GetString();