annotate Plugin/AuthorizationWebService.cpp @ 74:aa73b10c2db9

new API route to decode tokens
author Alain Mazy <am@osimis.io>
date Fri, 03 Mar 2023 18:03:22 +0100
parents 512247750f0a
children e2c3c497eb8d
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: 67
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 "AuthorizationWebService.h"
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
34
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 32
diff changeset
21 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 32
diff changeset
22
32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 31
diff changeset
23 #include <Logging.h>
59
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
24 #include <Toolbox.h>
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
25 #include <HttpClient.h>
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
26 #include <algorithm>
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 namespace OrthancPlugins
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
30 static const char* GRANTED = "granted";
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
31 static const char* VALIDITY = "validity";
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
32 static const char* PERMISSIONS = "permissions";
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
33
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
34
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 bool AuthorizationWebService::IsGrantedInternal(unsigned int& validity,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 OrthancPluginHttpMethod method,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 const AccessedResource& access,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 const Token* token,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 const std::string& tokenValue)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 Json::Value body = Json::objectValue;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 switch (method)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 case OrthancPluginHttpMethod_Get:
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 body["method"] ="get";
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 break;
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 case OrthancPluginHttpMethod_Post:
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 body["method"] ="post";
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 break;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 case OrthancPluginHttpMethod_Put:
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 body["method"] ="put";
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 break;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 case OrthancPluginHttpMethod_Delete:
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 body["method"] ="delete";
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 break;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 default:
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 body["level"] = EnumerationToString(access.GetLevel());
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if (access.GetLevel() == AccessLevel_System)
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 body["uri"] = access.GetOrthancId();
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 else
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 body["orthanc-id"] = access.GetOrthancId();
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 body["dicom-uid"] = access.GetDicomUid();
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 if (token != NULL)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 body["token-key"] = token->GetKey();
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 body["token-value"] = tokenValue;
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
54
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
83 if (!identifier_.empty())
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
84 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
85 body["server-id"] = identifier_;
54
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
86 }
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
87 else
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
88 {
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
89 body["server-id"] = Json::nullValue;
54
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
90 }
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
91
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
92 Orthanc::WebServiceParameters authWebservice;
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
93
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
94 if (!username_.empty())
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
95 {
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
96 authWebservice.SetCredentials(username_, password_);
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
97 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
99 std::string bodyAsString;
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
100 Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
101
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
102 Orthanc::HttpClient authClient(authWebservice, "");
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
103 authClient.SetUrl(tokenValidationUrl_);
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
104 authClient.AssignBody(bodyAsString);
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
105 authClient.SetMethod(Orthanc::HttpMethod_Post);
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
106 authClient.AddHeader("Content-Type", "application/json");
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
107 authClient.AddHeader("Expect", "");
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
108 authClient.SetTimeout(10);
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
109
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 if (token != NULL &&
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 token->GetType() == TokenType_HttpHeader)
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 // If the token source is a HTTP header, forward it also as a
59
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
114 // HTTP header except if it is the Authorization header that might conflict with username_ and password_
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
115 std::string lowerTokenKey;
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
116 Orthanc::Toolbox::ToLowerCase(lowerTokenKey, token->GetKey());
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
117
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
118 if (!(lowerTokenKey == "authorization" && !username_.empty()))
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
119 {
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
120 authClient.AddHeader(token->GetKey(), tokenValue);
59
a5f2976fe8a0 fix Authorization header conflicting with WebServiceUsername
Alain Mazy <am@osimis.io>
parents: 54
diff changeset
121 }
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 Json::Value answer;
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
125 authClient.ApplyAndThrowException(answer);
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 if (answer.type() != Json::objectValue ||
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 !answer.isMember(GRANTED) ||
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 answer[GRANTED].type() != Json::booleanValue ||
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 (answer.isMember(VALIDITY) &&
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 answer[VALIDITY].type() != Json::intValue))
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
29
bc0431cb6b8f fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
133 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
bc0431cb6b8f fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
134 "Syntax error in the result of the Web service");
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 validity = 0;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 if (answer.isMember(VALIDITY))
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 int tmp = answer[VALIDITY].asInt();
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 if (tmp < 0)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 {
29
bc0431cb6b8f fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
143 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
bc0431cb6b8f fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
144 "A validity duration cannot be negative");
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 validity = static_cast<unsigned int>(tmp);
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 return answer[GRANTED].asBool();
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 void AuthorizationWebService::SetCredentials(const std::string& username,
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 const std::string& password)
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 username_ = username;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 password_ = password;
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 }
54
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
160
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
161 void AuthorizationWebService::SetIdentifier(const std::string& webServiceIdentifier)
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
162 {
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
163 identifier_ = webServiceIdentifier;
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
164 }
317b31e99501 Added 3 new configurations: WebServiceUsername, WebServicePassword, WebServiceIdentifier. WebServiceIdentifier is now included in the payload as the 'identifier' field
Alain Mazy <am@osimis.io>
parents: 46
diff changeset
165
74
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
166
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
167 bool AuthorizationWebService::DecodeToken(DecodedToken& response,
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
168 const std::string& tokenKey,
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
169 const std::string& tokenValue)
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
170 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
171 if (tokenDecoderUrl_.empty())
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
172 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not create tokens if the 'WebServiceTokenValidationUrl' is not configured");
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
174 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
175 Orthanc::WebServiceParameters authWebservice;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
176
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
177 if (!username_.empty())
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
178 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
179 authWebservice.SetCredentials(username_, password_);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
180 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
181
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
182 Json::Value body;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
183
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
184 body["token-key"] = tokenKey;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
185 body["token-value"] = tokenValue;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
186
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
187 std::string bodyAsString;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
188 Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
189
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
190 Json::Value tokenResponse;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
191 try
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
192 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
193 Orthanc::HttpClient authClient(authWebservice, "");
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
194 authClient.SetUrl(tokenDecoderUrl_);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
195 authClient.AssignBody(bodyAsString);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
196 authClient.SetMethod(Orthanc::HttpMethod_Post);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
197 authClient.AddHeader("Content-Type", "application/json");
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
198 authClient.AddHeader("Expect", "");
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
199 authClient.SetTimeout(10);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
200
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
201 authClient.ApplyAndThrowException(tokenResponse);
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
202
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
203 if (tokenResponse.isMember("redirect-url"))
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
204 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
205 response.redirectUrl = tokenResponse["redirect-url"].asString();
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
206 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
207
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
208 if (tokenResponse.isMember("error-code"))
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
209 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
210 response.errorCode = tokenResponse["error-code"].asString();
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
211 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
212
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
213 if (tokenResponse.isMember("token-type"))
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
214 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
215 response.tokenType = tokenResponse["token-type"].asString();
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
216 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
217
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
218 return true;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
219 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
220 catch (Orthanc::OrthancException& ex)
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
221 {
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
222 return false;
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
223 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
224
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
225 }
aa73b10c2db9 new API route to decode tokens
Alain Mazy <am@osimis.io>
parents: 73
diff changeset
226
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
227 bool AuthorizationWebService::CreateToken(IAuthorizationService::CreatedToken& response,
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
228 const std::string& tokenType,
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
229 const std::string& id,
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
230 const std::vector<IAuthorizationService::OrthancResource>& resources,
73
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
231 const std::string& expirationDateString,
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
232 const uint64_t& validityDuration)
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
233 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
234 if (tokenCreationBaseUrl_.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
235 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
236 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not create tokens if the 'WebServiceTokenCreationBaseUrl' is not configured");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
237 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
238 std::string url = Orthanc::Toolbox::JoinUri(tokenCreationBaseUrl_, tokenType);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
239
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
240 Orthanc::WebServiceParameters authWebservice;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
241
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
242 if (!username_.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
243 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
244 authWebservice.SetCredentials(username_, password_);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
245 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
246
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
247 Json::Value body;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
248
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
249 if (!id.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
250 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
251 body["id"] = id;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
252 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
253
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
254 body["resources"] = Json::arrayValue;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
255 for (size_t i = 0; i < resources.size(); ++i)
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
256 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
257 Json::Value resource;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
258 if (!resources[i].dicomUid.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
259 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
260 resource["dicom-uid"] = resources[i].dicomUid;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
261 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
262 if (!resources[i].orthancId.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
263 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
264 resource["orthanc-id"] = resources[i].orthancId;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
265 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
266 if (!resources[i].url.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
267 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
268 resource["url"] = resources[i].url;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
269 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
270 if (!resources[i].level.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
271 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
272 resource["level"] = resources[i].level;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
273 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
274
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
275 body["resources"].append(resource);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
276 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
277
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
278 body["type"] = tokenType;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
279 if (!expirationDateString.empty())
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
280 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
281 body["expiration-date"] = expirationDateString;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
282 }
73
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
283 if (validityDuration > 0)
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
284 {
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
285 body["validity-duration"] = validityDuration;
512247750f0a new ValidityDuration arg in create token API
Alain Mazy <am@osimis.io>
parents: 72
diff changeset
286 }
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
287
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
288 std::string bodyAsString;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
289 Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
290
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
291 Json::Value tokenResponse;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
292 try
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
293 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
294 Orthanc::HttpClient authClient(authWebservice, "");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
295 authClient.SetUrl(url);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
296 authClient.AssignBody(bodyAsString);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
297 authClient.SetMethod(Orthanc::HttpMethod_Put);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
298 authClient.AddHeader("Content-Type", "application/json");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
299 authClient.AddHeader("Expect", "");
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
300 authClient.SetTimeout(10);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
301
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
302 authClient.ApplyAndThrowException(tokenResponse);
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
303
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
304 response.token = tokenResponse["token"].asString();
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
305 response.url = tokenResponse["url"].asString();
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
306
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
307 return true;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
308 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
309 catch (Orthanc::OrthancException& ex)
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
310 {
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
311 return false;
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
312 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
313
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
314 }
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
315
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
316
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
317 bool AuthorizationWebService::GetUserProfileInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
318 Json::Value& profile /* out */,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
319 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
320 const std::string& tokenValue)
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
321 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
322 if (userProfileUrl_.empty())
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
323 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
324 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadRequest, "Can not get user profile if the 'WebServiceUserProfileUrl' is not configured");
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
325 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
326
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
327 Orthanc::WebServiceParameters authWebservice;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
328
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
329 if (!username_.empty())
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
330 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
331 authWebservice.SetCredentials(username_, password_);
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
332 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
333
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
334 Json::Value body;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
335
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
336 if (token != NULL)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
337 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
338 body["token-key"] = token->GetKey();
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
339 body["token-value"] = tokenValue;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
340 }
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
341
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
342 if (!identifier_.empty())
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
343 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
344 body["identifier"] = identifier_;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
345 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
346 else
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
347 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
348 body["identifier"] = Json::nullValue;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
349 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
350
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
351 std::string bodyAsString;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
352 Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
353
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
354 try
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
355 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
356 Orthanc::HttpClient authClient(authWebservice, "");
72
e381ba725669 new PUT auth/tokens/{token-type} API route + updated interface with WebService
Alain Mazy <am@osimis.io>
parents: 71
diff changeset
357 authClient.SetUrl(userProfileUrl_);
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
358 authClient.AssignBody(bodyAsString);
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
359 authClient.SetMethod(Orthanc::HttpMethod_Post);
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
360 authClient.AddHeader("Content-Type", "application/json");
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
361 authClient.AddHeader("Expect", "");
70
786b202ef24e use more recent httpclient
Alain Mazy <am@osimis.io>
parents: 69
diff changeset
362 authClient.SetTimeout(10);
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
363
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
364 authClient.ApplyAndThrowException(profile);
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
365
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
366 if (profile.isMember("validity"))
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
367 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
368 validity = profile["validity"].asInt();
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
369 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
370 else
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
371 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
372 validity = 0;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
373 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
374
69
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
375 return true;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
376 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
377 catch (Orthanc::OrthancException& ex)
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
378 {
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
379 return false;
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
380 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
381 }
af44dce56328 new 'auth/user-profile' Rest API route
Alain Mazy <am@osimis.io>
parents: 68
diff changeset
382
71
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
383 bool AuthorizationWebService::HasUserPermissionInternal(unsigned int& validity,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
384 const std::string& permission,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
385 const Token* token,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
386 const std::string& tokenValue)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
387 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
388 Json::Value profile;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
389
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
390
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
391 if (GetUserProfileInternal(validity, profile, token, tokenValue))
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
392 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
393 if (profile.type() != Json::objectValue ||
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
394 !profile.isMember(PERMISSIONS) ||
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
395 !profile.isMember(VALIDITY) ||
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
396 profile[PERMISSIONS].type() != Json::arrayValue ||
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
397 profile[VALIDITY].type() != Json::intValue)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
398 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
399 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol,
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
400 "Syntax error in the result of the Web service");
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
401 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
402
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
403 validity = profile[VALIDITY].asUInt();
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
404
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
405 Json::Value& permissions = profile[PERMISSIONS];
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
406 for (Json::ArrayIndex i = 0; i < permissions.size(); ++i)
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
407 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
408 if (permission == permissions[i].asString())
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
409 {
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
410 return true;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
411 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
412 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
413 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
414
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
415 return false;
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
416 }
30fb3ce960d9 configurable user permissions
Alain Mazy <am@osimis.io>
parents: 70
diff changeset
417
1
d5d3cb00556a initial release
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 }