comparison Plugin/CachedAuthorizationService.cpp @ 115:0eed78c1e177

cache the UserProfile + updated http filter logic
author Alain Mazy <am@osimis.io>
date Fri, 08 Sep 2023 09:52:21 +0200
parents 43154740ea2e
children 9be1ee2b8fe1
comparison
equal deleted inserted replaced
114:546aea509427 115:0eed78c1e177
15 * You should have received a copy of the GNU Affero General Public License 15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 **/ 17 **/
18 18
19 #include "CachedAuthorizationService.h" 19 #include "CachedAuthorizationService.h"
20 #include "AuthorizationWebService.h"
20 21
21 #include <OrthancException.h> 22 #include <OrthancException.h>
22 #include <Toolbox.h> 23 #include <Toolbox.h>
23 24
24 #include <boost/lexical_cast.hpp> 25 #include <boost/lexical_cast.hpp>
110 return false; 111 return false;
111 } 112 }
112 } 113 }
113 114
114 115
115 bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validity, 116 bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validityNotUsed,
116 UserProfile& profile /* out */, 117 UserProfile& profile /* out */,
117 const Token* token, 118 const Token* token,
118 const std::string& tokenValue) 119 const std::string& tokenValue)
119 { 120 {
120 // no cache used when retrieving the full user profile 121 assert(decorated_.get() != NULL);
121 return decorated_->GetUserProfileInternal(validity, profile, token, tokenValue); 122
123 std::string key = ComputeKey("user-profile", token, tokenValue);
124 std::string serializedProfile;
125
126 if (cache_->Retrieve(serializedProfile, key))
127 {
128 // Return the previously cached profile
129 Json::Value jsonProfile;
130
131 Orthanc::Toolbox::ReadJson(jsonProfile, serializedProfile);
132
133 AuthorizationWebService::FromJson(profile, jsonProfile);
134
135 profile.tokenKey = token->GetKey();
136 profile.tokenType = token->GetType();
137 profile.tokenValue = tokenValue;
138
139 return true;
140 }
141 else
142 {
143 unsigned int validity;
144
145 if (decorated_->GetUserProfileInternal(validity, profile, token, tokenValue))
146 {
147 Json::Value jsonProfile;
148
149 AuthorizationWebService::ToJson(jsonProfile, profile);
150 Orthanc::Toolbox::WriteFastJson(serializedProfile, jsonProfile);
151
152 cache_->Store(key, serializedProfile, validity);
153
154 return true;
155 }
156 }
157
158 return false;
122 } 159 }
123 160
124 bool CachedAuthorizationService::HasUserPermissionInternal(unsigned int& validity, 161 bool CachedAuthorizationService::HasUserPermissionInternal(unsigned int& validity,
125 const std::string& permission, 162 const std::string& permission,
126 const UserProfile& profile) 163 const UserProfile& profile)