annotate Plugin/CachedAuthorizationService.cpp @ 191:55435a4dd2c6

cppcheck
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Jun 2024 15:23:55 +0200
parents c4b908970ae4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Advanced authorization plugin for Orthanc
68
1a13c4fbc9a1 copyrights
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
3 * Copyright (C) 2017-2023 Osimis S.A., Belgium
150
Alain Mazy <am@osimis.io>
parents: 115
diff changeset
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
188
c4b908970ae4 updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 150
diff changeset
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 #include "CachedAuthorizationService.h"
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
22 #include "AuthorizationWebService.h"
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 31
diff changeset
24 #include <OrthancException.h>
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
25 #include <Toolbox.h>
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <boost/lexical_cast.hpp>
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancPlugins
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 std::string CachedAuthorizationService::ComputeKey(OrthancPluginHttpMethod method,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 const AccessedResource& access,
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
33 const Token* token,
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 const std::string& tokenValue) const
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
36 if (token != NULL)
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
37 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
38 return (boost::lexical_cast<std::string>(method) + "|" +
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
39 boost::lexical_cast<std::string>(access.GetLevel()) + "|" +
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
40 access.GetOrthancId() + "|" + token->GetKey() + "|" + tokenValue);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
41 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
42 else
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
43 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
44 return (boost::lexical_cast<std::string>(method) + "|" +
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
45 boost::lexical_cast<std::string>(access.GetLevel()) + "|" +
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
46 access.GetOrthancId() + "|anonymous");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
47 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
51 std::string CachedAuthorizationService::ComputeKey(const std::string& permission,
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
52 const Token* token,
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
53 const std::string& tokenValue) const
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
54 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
55 if (token != NULL)
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
56 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
57 return (permission + "|" + token->GetKey() + "|" + tokenValue);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
58 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
59 else
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
60 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
61 return (permission + "|anonymous");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
62 }
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
63 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
64
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
65
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
66 CachedAuthorizationService::CachedAuthorizationService(BaseAuthorizationService* decorated /* takes ownership */,
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 ICacheFactory& factory) :
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 decorated_(decorated),
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 cache_(factory.Create())
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (decorated_.get() == NULL)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
78 bool CachedAuthorizationService::IsGrantedInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
79 OrthancPluginHttpMethod method,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
80 const AccessedResource& access,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
81 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
82 const std::string& tokenValue)
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 assert(decorated_.get() != NULL);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
86 std::string key = ComputeKey(method, access, token, tokenValue);
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 std::string value;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (cache_->Retrieve(value, key))
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 // Return the previously cached value
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 return (value == "1");
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
95 bool granted = decorated_->IsGrantedInternal(validity, method, access, token, tokenValue);
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (granted)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if (validity > 0)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 cache_->Store(key, "1", validity);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 return true;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 else
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if (validity > 0)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 cache_->Store(key, "0", validity);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 return false;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
118 bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validityNotUsed,
109
7381a7674b36 wip: adding labels
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
119 UserProfile& profile /* out */,
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
120 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
121 const std::string& tokenValue)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
122 {
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
123 assert(decorated_.get() != NULL);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
124
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
125 std::string key = ComputeKey("user-profile", token, tokenValue);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
126 std::string serializedProfile;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
127
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
128 if (cache_->Retrieve(serializedProfile, key))
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
129 {
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
130 // Return the previously cached profile
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
131 Json::Value jsonProfile;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
132
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
133 Orthanc::Toolbox::ReadJson(jsonProfile, serializedProfile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
134
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
135 AuthorizationWebService::FromJson(profile, jsonProfile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
136
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
137 profile.tokenKey = token->GetKey();
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
138 profile.tokenType = token->GetType();
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
139 profile.tokenValue = tokenValue;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
140
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
141 return true;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
142 }
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
143 else
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
144 {
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
145 unsigned int validity;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
146
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
147 if (decorated_->GetUserProfileInternal(validity, profile, token, tokenValue))
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
148 {
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
149 Json::Value jsonProfile;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
150
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
151 AuthorizationWebService::ToJson(jsonProfile, profile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
152 Orthanc::Toolbox::WriteFastJson(serializedProfile, jsonProfile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
153
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
154 cache_->Store(key, serializedProfile, validity);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
155
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
156 return true;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
157 }
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
158 }
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
159
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
160 return false;
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
161 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
162
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
163 bool CachedAuthorizationService::HasUserPermissionInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
164 const std::string& permission,
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
165 const UserProfile& profile)
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 assert(decorated_.get() != NULL);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
169 Token token(profile.tokenType, profile.tokenKey);
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
170 std::string key = ComputeKey(permission, &token, profile.tokenValue);
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
171 std::string value;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
172
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
173 if (cache_->Retrieve(value, key))
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
174 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
175 // Return the previously cached value
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
176 return (value == "1");
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
177 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
178
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
179 bool granted = decorated_->HasUserPermissionInternal(validity, permission, profile);
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
180
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
181 if (granted)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
182 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
183 if (validity > 0)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
184 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
185 cache_->Store(key, "1", validity);
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
186 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
187
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
188 return true;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
189 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
190 else
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
191 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
192 if (validity > 0)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
193 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
194 cache_->Store(key, "0", validity);
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
195 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
196
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
197 return false;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
198 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 }
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
200
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
201
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
202
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }