open62541++ 0.13.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
AccessControl.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
8#include "open62541pp/Span.h"
9#include "open62541pp/detail/open62541/common.h" // UA_AccessControl
12#include "open62541pp/types/Composed.h" // UserTokenPolicy, PerformUpdateType
14
15namespace opcua {
16
17// forward declare
18class DataValue;
19class DateTime;
20class ExtensionObject;
21class Session;
22
23/// Login credentials.
24struct Login {
25 std::string username;
26 std::string password;
27};
28
29/**
30 * Access control base class.
31 *
32 * Used to authenticate sessions and grant access rights accordingly.
33 * Custom access control can be implemented by deriving from this class and overwriting the access
34 * control callbacks.
35 *
36 * If exceptions are thrown within the access control callbacks, they are caught in the C callbacks
37 * and will return the most restrictive access rights, e.g. `AccessLevel::None` in
38 * `getUserAccessLevel` or `false` in `allowAddNode`. The exception will be logged (warning level).
39 *
40 * The `sessionId` can originally be both `NULL` in open62541.
41 * This is the case when, for example, a MonitoredItem (the underlying Subscription) is detached
42 * from its Session but continues to run.
43 * This wrapper passes `session` by reference, so it can't be `NULL`.
44 * Instead, a `session` with an empty `sessionId` will be passed.
45 *
46 * @see UA_AccessControl
47 * @see https://www.open62541.org/doc/1.3/plugin_accesscontrol.html
48 */
49class AccessControlBase : public PluginAdapter<UA_AccessControl> {
50public:
51 /**
52 * Get available user token policies.
53 * If the `securityPolicyUri` is empty, the highest available security policy will be used to
54 * transfer user tokens.
55 * @note The returned span must be valid throughout the lifetime of the instance.
56 */
58
59 /**
60 * Authenticate a session.
61 * The new session is rejected if a status code other than `UA_STATUSCODE_GOOD` is returned.
62 */
64 Session& session,
65 const EndpointDescription& endpointDescription,
66 const ByteString& secureChannelRemoteCertificate,
67 const ExtensionObject& userIdentityToken
68 ) = 0;
69
70 /// Deauthenticate a session and cleanup session context.
71 virtual void closeSession(Session& session) = 0;
72
73 /// Access control for all nodes.
74 virtual Bitmask<WriteMask> getUserRightsMask(Session& session, const NodeId& nodeId) = 0;
75
76 /// Additional access control for variable nodes.
77 virtual Bitmask<AccessLevel> getUserAccessLevel(Session& session, const NodeId& nodeId) = 0;
78
79 /// Additional access control for method nodes.
80 virtual bool getUserExecutable(Session& session, const NodeId& methodId) = 0;
81
82 /// Additional access control for calling a method node in the context of a specific object.
84 Session& session, const NodeId& methodId, const NodeId& objectId
85 ) = 0;
86
87 /// Allow adding a node.
88 virtual bool allowAddNode(Session& session, const AddNodesItem& item) = 0;
89
90 /// Allow adding a reference.
91 virtual bool allowAddReference(Session& session, const AddReferencesItem& item) = 0;
92
93 /// Allow deleting a node.
94 virtual bool allowDeleteNode(Session& session, const DeleteNodesItem& item) = 0;
95
96 /// Allow deleting a reference.
97 virtual bool allowDeleteReference(Session& session, const DeleteReferencesItem& item) = 0;
98
99 /// Allow browsing a node.
100 virtual bool allowBrowseNode(Session& session, const NodeId& nodeId) = 0;
101
102 /// Allow transfer of a subscription to another session.
103 virtual bool allowTransferSubscription(Session& oldSession, Session& newSession) = 0;
104
105 /// Allow insert, replace, update of historical data.
106 virtual bool allowHistoryUpdate(
107 Session& session,
108 const NodeId& nodeId,
109 PerformUpdateType performInsertReplace,
110 const DataValue& value
111 ) = 0;
112
113 /// Allow delete of historical data.
114 virtual bool allowHistoryDelete(
115 Session& session,
116 const NodeId& nodeId,
117 DateTime startTimestamp,
118 DateTime endTimestamp,
119 bool isDeleteModified
120 ) = 0;
121
122 void clear(UA_AccessControl& ac) noexcept override;
124};
125
126/* ----------------------------------- Default access control ----------------------------------- */
127
128/**
129 * Default access control.
130 *
131 * This class implements the same logic as @ref UA_AccessControl_default().
132 * The log-in can be anonymous or username-password. A logged-in user has all access rights.
133 *
134 * @warning Use less permissive access control in production!
135 */
137public:
138 explicit AccessControlDefault(bool allowAnonymous = true, std::vector<Login> logins = {});
139
141
143 Session& session,
144 const EndpointDescription& endpointDescription,
145 const ByteString& secureChannelRemoteCertificate,
146 const ExtensionObject& userIdentityToken
147 ) override;
148
149 void closeSession(Session& session) override;
150
151 Bitmask<WriteMask> getUserRightsMask(Session& session, const NodeId& nodeId) override;
152
153 Bitmask<AccessLevel> getUserAccessLevel(Session& session, const NodeId& nodeId) override;
154
155 bool getUserExecutable(Session& session, const NodeId& methodId) override;
156
157 bool getUserExecutableOnObject(Session& session, const NodeId& methodId, const NodeId& objectId)
158 override;
159
160 bool allowAddNode(Session& session, const AddNodesItem& item) override;
161
162 bool allowAddReference(Session& session, const AddReferencesItem& item) override;
163
164 bool allowDeleteNode(Session& session, const DeleteNodesItem& item) override;
165
166 bool allowDeleteReference(Session& session, const DeleteReferencesItem& item) override;
167
168 bool allowBrowseNode(Session& session, const NodeId& nodeId) override;
169
170 bool allowTransferSubscription(Session& oldSession, Session& newSession) override;
171
173 Session& session,
174 const NodeId& nodeId,
175 PerformUpdateType performInsertReplace,
176 const DataValue& value
177 ) override;
178
180 Session& session,
181 const NodeId& nodeId,
182 DateTime startTimestamp,
183 DateTime endTimestamp,
184 bool isDeleteModified
185 ) override;
186
187private:
188 bool allowAnonymous_;
189 std::vector<Login> logins_;
190 std::vector<UserTokenPolicy> userTokenPolicies_;
191};
192
193} // namespace opcua
Access control base class.
virtual Span< UserTokenPolicy > getUserTokenPolicies()=0
Get available user token policies.
void clear(UA_AccessControl &ac) noexcept override
virtual bool allowDeleteReference(Session &session, const DeleteReferencesItem &item)=0
Allow deleting a reference.
virtual bool allowHistoryDelete(Session &session, const NodeId &nodeId, DateTime startTimestamp, DateTime endTimestamp, bool isDeleteModified)=0
Allow delete of historical data.
virtual bool getUserExecutableOnObject(Session &session, const NodeId &methodId, const NodeId &objectId)=0
Additional access control for calling a method node in the context of a specific object.
virtual bool allowAddNode(Session &session, const AddNodesItem &item)=0
Allow adding a node.
virtual bool getUserExecutable(Session &session, const NodeId &methodId)=0
Additional access control for method nodes.
UA_AccessControl create() override
virtual bool allowBrowseNode(Session &session, const NodeId &nodeId)=0
Allow browsing a node.
virtual bool allowDeleteNode(Session &session, const DeleteNodesItem &item)=0
Allow deleting a node.
virtual Bitmask< WriteMask > getUserRightsMask(Session &session, const NodeId &nodeId)=0
Access control for all nodes.
virtual Bitmask< AccessLevel > getUserAccessLevel(Session &session, const NodeId &nodeId)=0
Additional access control for variable nodes.
virtual StatusCode activateSession(Session &session, const EndpointDescription &endpointDescription, const ByteString &secureChannelRemoteCertificate, const ExtensionObject &userIdentityToken)=0
Authenticate a session.
virtual bool allowTransferSubscription(Session &oldSession, Session &newSession)=0
Allow transfer of a subscription to another session.
virtual bool allowHistoryUpdate(Session &session, const NodeId &nodeId, PerformUpdateType performInsertReplace, const DataValue &value)=0
Allow insert, replace, update of historical data.
virtual void closeSession(Session &session)=0
Deauthenticate a session and cleanup session context.
virtual bool allowAddReference(Session &session, const AddReferencesItem &item)=0
Allow adding a reference.
Default access control.
bool allowAddNode(Session &session, const AddNodesItem &item) override
Allow adding a node.
bool getUserExecutable(Session &session, const NodeId &methodId) override
Additional access control for method nodes.
Bitmask< WriteMask > getUserRightsMask(Session &session, const NodeId &nodeId) override
Access control for all nodes.
bool allowAddReference(Session &session, const AddReferencesItem &item) override
Allow adding a reference.
void closeSession(Session &session) override
Deauthenticate a session and cleanup session context.
Span< UserTokenPolicy > getUserTokenPolicies() override
Get available user token policies.
bool allowDeleteNode(Session &session, const DeleteNodesItem &item) override
Allow deleting a node.
bool allowTransferSubscription(Session &oldSession, Session &newSession) override
Allow transfer of a subscription to another session.
bool getUserExecutableOnObject(Session &session, const NodeId &methodId, const NodeId &objectId) override
Additional access control for calling a method node in the context of a specific object.
StatusCode activateSession(Session &session, const EndpointDescription &endpointDescription, const ByteString &secureChannelRemoteCertificate, const ExtensionObject &userIdentityToken) override
Authenticate a session.
bool allowBrowseNode(Session &session, const NodeId &nodeId) override
Allow browsing a node.
bool allowHistoryDelete(Session &session, const NodeId &nodeId, DateTime startTimestamp, DateTime endTimestamp, bool isDeleteModified) override
Allow delete of historical data.
bool allowHistoryUpdate(Session &session, const NodeId &nodeId, PerformUpdateType performInsertReplace, const DataValue &value) override
Allow insert, replace, update of historical data.
Bitmask< AccessLevel > getUserAccessLevel(Session &session, const NodeId &nodeId) override
Additional access control for variable nodes.
bool allowDeleteReference(Session &session, const DeleteReferencesItem &item) override
Allow deleting a reference.
AccessControlDefault(bool allowAnonymous=true, std::vector< Login > logins={})
UA_AddNodesItem wrapper class.
Definition Composed.h:645
UA_AddReferencesItem wrapper class.
Definition Composed.h:725
Bitmask using (scoped) enums.
Definition Bitmask.h:108
UA_ByteString wrapper class.
Definition Builtin.h:172
UA_DataValue wrapper class.
Definition DataValue.h:20
UA_DateTime wrapper class.
Definition DateTime.h:23
UA_DeleteNodesItem wrapper class.
Definition Composed.h:796
UA_DeleteReferencesItem wrapper class.
Definition Composed.h:848
UA_EndpointDescription wrapper class.
Definition Composed.h:207
UA_ExtensionObject wrapper class.
UA_NodeId wrapper class.
Definition NodeId.h:36
Base class to implement plugin adapters.
High-level session class to manage client sessions.
Definition Session.h:23
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition Span.h:27
UA_StatusCode wrapper class.
Definition Builtin.h:41
PerformUpdateType
Perform update type for structured data history updates.
Definition Composed.h:1976
Login credentials.
std::string username
std::string password