annotate Plugin/CachedAuthorizationService.cpp @ 124:d5232d374fd8 0.6.0

Add support for ForbiddenAccess error for Orthanc < 1.12.2
author Alain Mazy <am@osimis.io>
date Mon, 18 Sep 2023 21:15:33 +0200
parents 0eed78c1e177
children 9be1ee2b8fe1
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
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * modify it under the terms of the GNU Affero General Public License
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * as published by the Free Software Foundation, either version 3 of
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * the License, or (at your option) any later version.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * 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
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * Affero General Public License for more details.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 *
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * 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
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 **/
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 #include "CachedAuthorizationService.h"
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
20 #include "AuthorizationWebService.h"
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 31
diff changeset
22 #include <OrthancException.h>
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
23 #include <Toolbox.h>
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <boost/lexical_cast.hpp>
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 namespace OrthancPlugins
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 std::string CachedAuthorizationService::ComputeKey(OrthancPluginHttpMethod method,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 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
31 const Token* token,
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 const std::string& tokenValue) const
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
34 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
35 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
36 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
37 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
38 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
39 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
40 else
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 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
43 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
44 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
45 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
49 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
50 const Token* token,
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
51 const std::string& tokenValue) const
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
52 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
53 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
54 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
55 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
56 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
57 else
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 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
60 }
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
61 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
62
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 CachedAuthorizationService::CachedAuthorizationService(BaseAuthorizationService* decorated /* takes ownership */,
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 ICacheFactory& factory) :
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 decorated_(decorated),
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 cache_(factory.Create())
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if (decorated_.get() == NULL)
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 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
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 }
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
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
76 bool CachedAuthorizationService::IsGrantedInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
77 OrthancPluginHttpMethod method,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
78 const AccessedResource& access,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
79 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
80 const std::string& tokenValue)
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 assert(decorated_.get() != NULL);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
84 std::string key = ComputeKey(method, access, token, tokenValue);
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 std::string value;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 if (cache_->Retrieve(value, key))
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 // Return the previously cached value
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 return (value == "1");
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
93 bool granted = decorated_->IsGrantedInternal(validity, method, access, token, tokenValue);
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 if (granted)
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 (validity > 0)
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 cache_->Store(key, "1", validity);
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
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 return true;
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 else
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 if (validity > 0)
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 cache_->Store(key, "0", validity);
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
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 return false;
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 }
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
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
116 bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validityNotUsed,
109
7381a7674b36 wip: adding labels
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
117 UserProfile& profile /* out */,
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
118 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
119 const std::string& tokenValue)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
120 {
115
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
121 assert(decorated_.get() != NULL);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
122
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
123 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
124 std::string serializedProfile;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
125
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
126 if (cache_->Retrieve(serializedProfile, key))
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 // Return the previously cached profile
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
129 Json::Value jsonProfile;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
130
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
131 Orthanc::Toolbox::ReadJson(jsonProfile, serializedProfile);
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 AuthorizationWebService::FromJson(profile, jsonProfile);
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 profile.tokenKey = token->GetKey();
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
136 profile.tokenType = token->GetType();
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
137 profile.tokenValue = tokenValue;
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
138
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
139 return true;
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 else
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 unsigned int validity;
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 if (decorated_->GetUserProfileInternal(validity, profile, token, tokenValue))
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 Json::Value jsonProfile;
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 AuthorizationWebService::ToJson(jsonProfile, profile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
150 Orthanc::Toolbox::WriteFastJson(serializedProfile, jsonProfile);
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
151
0eed78c1e177 cache the UserProfile + updated http filter logic
Alain Mazy <am@osimis.io>
parents: 113
diff changeset
152 cache_->Store(key, serializedProfile, validity);
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 return true;
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 }
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 return false;
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
159 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
160
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
161 bool CachedAuthorizationService::HasUserPermissionInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
162 const std::string& permission,
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
163 const UserProfile& profile)
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 assert(decorated_.get() != NULL);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
167 Token token(profile.tokenType, profile.tokenKey);
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
168 std::string key = ComputeKey(permission, &token, profile.tokenValue);
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
169 std::string value;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
170
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
171 if (cache_->Retrieve(value, key))
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 // Return the previously cached value
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
174 return (value == "1");
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
175 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
176
113
43154740ea2e wip: checking labels
Alain Mazy <am@osimis.io>
parents: 109
diff changeset
177 bool granted = decorated_->HasUserPermissionInternal(validity, permission, profile);
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
178
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
179 if (granted)
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 (validity > 0)
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 cache_->Store(key, "1", validity);
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
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
186 return true;
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 else
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 if (validity > 0)
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 cache_->Store(key, "0", validity);
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
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
195 return false;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
196 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
198
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
199
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
200
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }