Skip to content

Commit c1f2ba7

Browse files
Refactor Permission structure in Decision API (#887)
Change Permission from pair to a struct and add missing resource field. Resolves: OLPSUP-10514 Signed-off-by: Serhii Lozynskyi <ext-serhii.lozynskyi@here.com>
1 parent 7563ed4 commit c1f2ba7

File tree

8 files changed

+164
-81
lines changed

8 files changed

+164
-81
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/AuthorizeResult.h

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,60 @@ namespace authentication {
3232
*/
3333
enum class DecisionType { kAllow, kDeny };
3434

35+
/**
36+
* @brief Represents the permission with the action, policy decision, and
37+
* associated resource.
38+
*/
39+
class AUTHENTICATION_API Permission {
40+
public:
41+
/**
42+
* @brief Sets the action associated with the resource.
43+
*
44+
* @param The action to associate with.
45+
*/
46+
void SetAction(std::string action) { action_ = std::move(action); }
47+
48+
/**
49+
* @brief Gets the action that is associated with the resource.
50+
*
51+
* @return A string that represents the action.
52+
*/
53+
const std::string& GetAction() const { return action_; }
54+
55+
/**
56+
* @brief Sets the resource with which the action and decision are associated.
57+
*
58+
* @param The resource to associate with the decision and action.
59+
*/
60+
void SetResource(std::string resource) { resource_ = std::move(resource); }
61+
62+
/**
63+
* @brief Gets the resource with which the action and decision are associated.
64+
*
65+
* @return The resource name.
66+
*/
67+
const std::string& GetResource() const { return resource_; }
68+
69+
/**
70+
* @brief Sets the decision associated with the resource.
71+
*
72+
* @param The decision to associate with the resource.
73+
*/
74+
void SetDecision(DecisionType decision) { decision_ = decision; }
75+
76+
/**
77+
* @brief Gets the decision associated with the resource.
78+
*
79+
* @return The decision for the associated resource.
80+
*/
81+
DecisionType GetDecision() const { return decision_; }
82+
83+
private:
84+
std::string action_;
85+
std::string resource_;
86+
DecisionType decision_;
87+
};
88+
3589
/**
3690
* @brief Represents each action-resource pair response with an individual
3791
* policy decision for that action: DENY or ALLOW.
@@ -41,11 +95,6 @@ enum class DecisionType { kAllow, kDeny };
4195
*/
4296
class AUTHENTICATION_API ActionResult {
4397
public:
44-
/**
45-
* @brief Represents the permission pair with the action and policy decision.
46-
*/
47-
using Permissions = std::pair<std::string, DecisionType>;
48-
4998
/**
5099
* @brief Gets the overall policy decision.
51100
*
@@ -77,20 +126,20 @@ class AUTHENTICATION_API ActionResult {
77126
*
78127
* @return The list of permissions.
79128
*/
80-
const std::vector<Permissions>& GetPermissions() const { return permissions_; }
129+
const std::vector<Permission>& GetPermissions() const { return permissions_; }
81130

82131
/**
83132
* @brief Sets the list of permissions.
84133
*
85134
* @param permissions The vector of the action-decision pair.
86135
*/
87-
void SetPermissions(std::vector<Permissions> permissions) {
136+
void SetPermissions(std::vector<Permission> permissions) {
88137
permissions_ = std::move(permissions);
89138
}
90139

91140
private:
92141
DecisionType decision_{DecisionType::kDeny};
93-
std::vector<Permissions> permissions_;
142+
std::vector<Permission> permissions_;
94143
};
95144

96145
/**

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ IntrospectAppResult GetIntrospectAppResult(const rapidjson::Document& doc) {
193193
return result;
194194
}
195195

196-
DecisionType GetPermission(const std::string& str) {
196+
DecisionType GetDecision(const std::string& str) {
197197
return (str.compare("allow") == 0) ? DecisionType::kAllow
198198
: DecisionType::kDeny;
199199
}
@@ -205,22 +205,26 @@ std::vector<ActionResult> GetDiagnostics(rapidjson::Document& doc) {
205205
ActionResult action;
206206
if (element.HasMember(Constants::DECISION)) {
207207
action.SetDecision(
208-
GetPermission(element[Constants::DECISION].GetString()));
208+
GetDecision(element[Constants::DECISION].GetString()));
209209
// get permissions if avialible
210210
if (element.HasMember(Constants::PERMISSIONS) &&
211211
element[Constants::PERMISSIONS].IsArray()) {
212-
std::vector<ActionResult::Permissions> permissions;
212+
std::vector<Permission> permissions;
213213
const auto& permissions_array =
214214
element[Constants::PERMISSIONS].GetArray();
215215
for (auto& permission_element : permissions_array) {
216-
ActionResult::Permissions permission;
216+
Permission permission;
217217
if (permission_element.HasMember(Constants::ACTION)) {
218-
permission.first =
219-
permission_element[Constants::ACTION].GetString();
218+
permission.SetAction(
219+
permission_element[Constants::ACTION].GetString());
220220
}
221221
if (permission_element.HasMember(Constants::DECISION)) {
222-
permission.second = GetPermission(
223-
permission_element[Constants::DECISION].GetString());
222+
permission.SetDecision(GetDecision(
223+
permission_element[Constants::DECISION].GetString()));
224+
}
225+
if (permission_element.HasMember(Constants::RESOURCE)) {
226+
permission.SetResource(
227+
permission_element[Constants::RESOURCE].GetString());
224228
}
225229
permissions.push_back(std::move(permission));
226230
}
@@ -247,7 +251,7 @@ AuthorizeResult GetAuthorizeResult(rapidjson::Document& doc) {
247251
}
248252

249253
if (doc.HasMember(Constants::DECISION)) {
250-
result.SetDecision(GetPermission(doc[Constants::DECISION].GetString()));
254+
result.SetDecision(GetDecision(doc[Constants::DECISION].GetString()));
251255
}
252256

253257
// get diagnostics if available

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ IntrospectAppResult GetIntrospectAppResult(const rapidjson::Document& doc);
9595
* @param str string representation of decision.
9696
* @return result DecisionType.
9797
*/
98-
DecisionType GetPermission(const std::string& str);
98+
DecisionType GetDecision(const std::string& str);
9999

100100
/*
101101
* @brief Parse json document to vector of ActionResults.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const char* Constants::IDENTITY = "identity";
5858
const char* Constants::USER_ID = "userId";
5959
const char* Constants::DECISION = "decision";
6060
const char* Constants::ACTION = "action";
61+
const char* Constants::RESOURCE = "resource";
6162
const char* Constants::PERMISSIONS = "permissions";
6263
const char* Constants::DIAGNOSTICS = "diagnostics";
6364

olp-cpp-sdk-authentication/src/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Constants {
6464
static const char* USER_ID;
6565
static const char* DECISION;
6666
static const char* ACTION;
67+
static const char* RESOURCE;
6768
static const char* PERMISSIONS;
6869
static const char* DIAGNOSTICS;
6970
};

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

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@
2222
#include <olp/authentication/AuthorizeResult.h>
2323

2424
namespace {
25-
using namespace olp::authentication;
25+
namespace auth = olp::authentication;
2626

2727
TEST(DecisionApiClientTest, AuthorizeRequestTest) {
28-
EXPECT_EQ(AuthorizeRequest().WithServiceId("ServiceId").GetServiceId(),
28+
EXPECT_EQ(auth::AuthorizeRequest().WithServiceId("ServiceId").GetServiceId(),
2929
"ServiceId");
30-
EXPECT_EQ(
31-
AuthorizeRequest().WithContractId("ContractId").GetContractId().get(),
32-
"ContractId");
33-
auto request = AuthorizeRequest().WithAction("action1").WithAction(
30+
EXPECT_EQ(auth::AuthorizeRequest()
31+
.WithContractId("ContractId")
32+
.GetContractId()
33+
.get(),
34+
"ContractId");
35+
auto request = auth::AuthorizeRequest().WithAction("action1").WithAction(
3436
"action2", std::string("hrn::test"));
35-
EXPECT_EQ(AuthorizeRequest().GetDiagnostics(), false);
36-
EXPECT_EQ(AuthorizeRequest().WithDiagnostics(true).GetDiagnostics(), true);
37+
EXPECT_EQ(auth::AuthorizeRequest().GetDiagnostics(), false);
38+
EXPECT_EQ(auth::AuthorizeRequest().WithDiagnostics(true).GetDiagnostics(),
39+
true);
3740
EXPECT_EQ(request.GetActions().size(), 2);
3841
auto actions_it = request.GetActions().begin();
3942
EXPECT_EQ(actions_it->first, "action1");
@@ -42,29 +45,44 @@ TEST(DecisionApiClientTest, AuthorizeRequestTest) {
4245
EXPECT_EQ(actions_it->first, "action2");
4346
EXPECT_EQ(actions_it->second, "hrn::test");
4447
EXPECT_EQ(request.GetOperatorType(),
45-
AuthorizeRequest::DecisionOperatorType::kAnd);
46-
request.WithOperatorType(AuthorizeRequest::DecisionOperatorType::kOr);
48+
auth::AuthorizeRequest::DecisionOperatorType::kAnd);
49+
request.WithOperatorType(auth::AuthorizeRequest::DecisionOperatorType::kOr);
4750
EXPECT_EQ(request.GetOperatorType(),
48-
AuthorizeRequest::DecisionOperatorType::kOr);
51+
auth::AuthorizeRequest::DecisionOperatorType::kOr);
4952
request.WithServiceId("service");
5053
EXPECT_EQ(request.CreateKey(), "service");
5154
request.WithContractId("contract");
5255
EXPECT_EQ(request.CreateKey(), "service[contract]");
5356
}
5457

5558
TEST(DecisionApiClientTest, AuthorizeResponceTest) {
56-
EXPECT_EQ(AuthorizeResult().GetDecision(), DecisionType::kDeny);
57-
EXPECT_EQ(ActionResult().GetDecision(), DecisionType::kDeny);
58-
EXPECT_EQ(AuthorizeResult().GetClientId(), "");
59-
ActionResult action;
60-
action.SetDecision(DecisionType::kAllow);
61-
action.SetPermissions({{"read", DecisionType::kAllow}});
62-
AuthorizeResult decision;
59+
EXPECT_EQ(auth::AuthorizeResult().GetDecision(), auth::DecisionType::kDeny);
60+
EXPECT_EQ(auth::ActionResult().GetDecision(), auth::DecisionType::kDeny);
61+
EXPECT_EQ(auth::AuthorizeResult().GetClientId(), "");
62+
auth::ActionResult action;
63+
action.SetDecision(auth::DecisionType::kAllow);
64+
auth::Permission permission;
65+
permission.SetAction("read");
66+
permission.SetResource("hrn:test");
67+
permission.SetDecision(auth::DecisionType::kAllow);
68+
action.SetPermissions({permission});
69+
auth::AuthorizeResult decision;
6370
decision.SetActionResults({action});
64-
EXPECT_EQ(decision.GetActionResults().front().GetPermissions().front().first,
65-
"read");
66-
EXPECT_EQ(decision.GetActionResults().front().GetPermissions().front().second,
67-
DecisionType::kAllow);
71+
EXPECT_EQ(
72+
decision.GetActionResults().front().GetPermissions().front().GetAction(),
73+
"read");
74+
EXPECT_EQ(decision.GetActionResults()
75+
.front()
76+
.GetPermissions()
77+
.front()
78+
.GetDecision(),
79+
auth::DecisionType::kAllow);
80+
EXPECT_EQ(decision.GetActionResults()
81+
.front()
82+
.GetPermissions()
83+
.front()
84+
.GetResource(),
85+
"hrn:test");
6886
}
6987

7088
} // namespace

0 commit comments

Comments
 (0)