open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_accesscontrol.cpp
#include <iostream>
using namespace opcua;
// Custom access control based on AccessControlDefault.
// If a user logs in with the username "admin", a session attribute "isAdmin" is stored. As an
// example, the user "admin" has write access level to the created variable node. So admins can
// change the value of the created variable node, anonymous users and the user "user" can't.
// Session attributes are available since open62541 v1.3, so this example requires at least v1.3.
class AccessControlCustom : public AccessControlDefault {
public:
using AccessControlDefault::AccessControlDefault; // inherit constructors
StatusCode activateSession(
Session& session,
const EndpointDescription& endpointDescription,
const ByteString& secureChannelRemoteCertificate,
const ExtensionObject& userIdentityToken
) override {
// Grant admin rights if user is logged in as "admin"
// Store attribute "isAdmin" as session attribute to use it in access callbacks
const auto* token = userIdentityToken.decodedData<UserNameIdentityToken>();
const bool isAdmin = (token != nullptr && token->userName() == "admin");
std::cout << "User has admin rights: " << isAdmin << std::endl;
session.setSessionAttribute({0, "isAdmin"}, Variant{isAdmin});
return AccessControlDefault::activateSession(
session, endpointDescription, secureChannelRemoteCertificate, userIdentityToken
);
}
Bitmask<AccessLevel> getUserAccessLevel(Session& session, const NodeId& nodeId) override {
const bool isAdmin = session.getSessionAttribute({0, "isAdmin"}).scalar<bool>();
std::cout << "Get user access level of node id " << opcua::toString(nodeId) << std::endl;
std::cout << "Admin rights granted: " << isAdmin << std::endl;
return isAdmin
? AccessLevel::CurrentRead | AccessLevel::CurrentWrite
: AccessLevel::CurrentRead;
}
};
int main() {
// Exchanging usernames/passwords without encryption as plain text is dangerous.
// We are doing this just for demonstration, don't use it in production!
AccessControlCustom accessControl{
true, // allow anonymous
{
Login{String{"admin"}, String{"admin"}},
Login{String{"user"}, String{"user"}},
}
};
ServerConfig config;
config.setAccessControl(accessControl);
#if UAPP_OPEN62541_VER_GE(1, 4)
config->allowNonePolicyPassword = true;
#endif
Server server{std::move(config)};
// Add variable node. Try to change its value as a client with different logins.
Node{server, ObjectId::ObjectsFolder}
{1, 1000},
"Variable",
.setAccessLevel(AccessLevel::CurrentRead | AccessLevel::CurrentWrite)
.setDataType(DataTypeId::Int32)
.setValueRank(ValueRank::Scalar)
.setValue(opcua::Variant{0})
);
server.run();
}
Bitmask using (scoped) enums.
Definition bitmask.hpp:127
UA_ByteString wrapper class.
Definition types.hpp:537
UA_ExtensionObject wrapper class.
Definition types.hpp:1742
T * decodedData() noexcept
Get pointer to the decoded data with given template type.
Definition types.hpp:1858
UA_NodeId wrapper class.
Definition types.hpp:641
High-level node class to access node attribute, browse and populate address space.
Definition node.hpp:45
Node addVariable(const NodeId &id, std::string_view browseName, const VariableAttributes &attributes={}, const NodeId &variableType=VariableTypeId::BaseDataVariableType, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add variable.
Definition node.hpp:156
Server configuration.
Definition server.hpp:43
void setAccessControl(AccessControlBase &accessControl)
Set custom access control.
High-level server class.
Definition server.hpp:142
High-level session class to manage client sessions.
Definition session.hpp:20
void setSessionAttribute(const QualifiedName &key, const Variant &value)
Attach a session attribute as a key-value pair.
Variant getSessionAttribute(const QualifiedName &key)
Get a session attribute by its key.
UA_StatusCode wrapper class.
Definition types.hpp:44
UA_String wrapper class.
Definition types.hpp:256
UA_Variant wrapper class.
Definition types.hpp:1048
UA_EndpointDescription wrapper class.
Definition types.hpp:271
UA_UserNameIdentityToken wrapper class.
Definition types.hpp:656
UA_VariableAttributes wrapper class.
Definition types.hpp:419
auto & setAccessLevel(Bitmask< AccessLevel > accessLevel) noexcept
Definition types.hpp:463
String toString(const NumericRange &range)
Login credentials.