-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathSignInResultImpl.cpp
More file actions
147 lines (120 loc) · 4.6 KB
/
SignInResultImpl.cpp
File metadata and controls
147 lines (120 loc) · 4.6 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
/*
* Copyright (C) 2019-2021 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 "SignInResultImpl.h"
#include "Constants.h"
#include "olp/core/http/HttpStatusCode.h"
namespace olp {
namespace authentication {
namespace {
constexpr auto kTokenType = "tokenType";
constexpr auto kUserId = "userId";
constexpr auto kScope = "scope";
constexpr auto kTokenTypeSnakeCase = "token_type";
constexpr auto kAccessTokenSnakeCase = "access_token";
constexpr auto kExpiresInSnakeCase = "expires_in";
bool HasAccessToken(const rapidjson::Document& document) {
return document.HasMember(Constants::ACCESS_TOKEN) ||
document.HasMember(kAccessTokenSnakeCase);
}
std::string ParseAccessToken(const rapidjson::Document& document) {
if (document.HasMember(Constants::ACCESS_TOKEN)) {
return document[Constants::ACCESS_TOKEN].GetString();
}
return document[kAccessTokenSnakeCase].GetString();
}
bool HasExpiresIn(const rapidjson::Document& document) {
return document.HasMember(Constants::EXPIRES_IN) ||
document.HasMember(kExpiresInSnakeCase);
}
unsigned ParseExpiresIn(const rapidjson::Document& document) {
if (document.HasMember(Constants::EXPIRES_IN)) {
return document[Constants::EXPIRES_IN].GetUint();
}
return document[kExpiresInSnakeCase].GetUint();
}
bool HasTokenType(const rapidjson::Document& document) {
return document.HasMember(kTokenType) ||
document.HasMember(kTokenTypeSnakeCase);
}
std::string ParseTokenType(const rapidjson::Document& document) {
if (document.HasMember(kTokenType)) {
return document[kTokenType].GetString();
}
return document[kTokenTypeSnakeCase].GetString();
}
bool IsDocumentValid(const rapidjson::Document& document) {
return HasAccessToken(document) && HasExpiresIn(document) &&
HasTokenType(document);
}
} // namespace
SignInResultImpl::SignInResultImpl() noexcept
: SignInResultImpl(http::HttpStatusCode::SERVICE_UNAVAILABLE,
Constants::ERROR_HTTP_SERVICE_UNAVAILABLE) {}
SignInResultImpl::SignInResultImpl(
int status, std::string error,
std::shared_ptr<rapidjson::Document> json_document) noexcept
: BaseResult(status, std::move(error), json_document),
expiry_time_(),
expires_in_() {
is_valid_ = this->BaseResult::IsValid() && IsDocumentValid(*json_document);
// Extra response data if no errors reported
if (!HasError()) {
if (!IsValid()) {
status_ = http::HttpStatusCode::SERVICE_UNAVAILABLE;
error_.message = Constants::ERROR_HTTP_SERVICE_UNAVAILABLE;
} else {
if (HasAccessToken(*json_document))
access_token_ = ParseAccessToken(*json_document);
if (HasTokenType(*json_document))
token_type_ = ParseTokenType(*json_document);
if (json_document->HasMember(Constants::REFRESH_TOKEN))
refresh_token_ = (*json_document)[Constants::REFRESH_TOKEN].GetString();
if (HasExpiresIn(*json_document)) {
const auto expires_in = ParseExpiresIn(*json_document);
expiry_time_ = std::time(nullptr) + expires_in;
expires_in_ = std::chrono::seconds(expires_in);
}
if (json_document->HasMember(kUserId))
user_identifier_ = (*json_document)[kUserId].GetString();
if (json_document->HasMember(kScope))
scope_ = (*json_document)[kScope].GetString();
}
}
}
SignInResultImpl::~SignInResultImpl() = default;
const std::string& SignInResultImpl::GetAccessToken() const {
return access_token_;
}
const std::string& SignInResultImpl::GetTokenType() const {
return token_type_;
}
const std::string& SignInResultImpl::GetRefreshToken() const {
return refresh_token_;
}
std::chrono::seconds SignInResultImpl::GetExpiresIn() const {
return expires_in_;
}
time_t SignInResultImpl::GetExpiryTime() const { return expiry_time_; }
const std::string& SignInResultImpl::GetUserIdentifier() const {
return user_identifier_;
}
const std::string& SignInResultImpl::GetScope() const { return scope_; }
bool SignInResultImpl::IsValid() const { return is_valid_; }
} // namespace authentication
} // namespace olp