| 1 | // Copyright (C) 2019 The Qt Company Ltd. | 
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 | |
| 4 | #include "qopcuaauthenticationinformation.h" | 
| 5 | |
| 6 | QT_BEGIN_NAMESPACE | 
| 7 | |
| 8 | /*! | 
| 9 | \class QOpcUaAuthenticationInformation | 
| 10 | \inmodule QtOpcUa | 
| 11 | \brief The OPC UA authentication information. | 
| 12 | \since QtOpcUa 5.13 | 
| 13 | |
| 14 | This class holds the information necessary to perform a login on a server. | 
| 15 | Supported authentication mechanisms are | 
| 16 | |
| 17 | \list | 
| 18 | \li Anonymous | 
| 19 | \li Username | 
| 20 | \li Certificate | 
| 21 | \endlist | 
| 22 | |
| 23 | The anonymous method is used by default but also can be set manually. | 
| 24 | |
| 25 | This is an example authentication using username and password. | 
| 26 | |
| 27 | \code | 
| 28 | QOpcUaAuthenticationInformation authInfo; | 
| 29 | authInfo.setUsernameAuthentication("user", "password"); | 
| 30 | |
| 31 | m_client->setAuthenticationInformation(authInfo); | 
| 32 | m_client->connectToEndpoint(endpoint); | 
| 33 | \endcode | 
| 34 | |
| 35 | \sa setAnonymousAuthentication() setUsernameAuthentication() setCertificateAuthentication() | 
| 36 | */ | 
| 37 | |
| 38 | /*! | 
| 39 | \qmltype AuthenticationInformation | 
| 40 | \inqmlmodule QtOpcUa | 
| 41 | \brief The OPC UA authentication information. | 
| 42 | \since QtOpcUa 5.13 | 
| 43 | |
| 44 | This class holds the information necessary to perform a login on a server. | 
| 45 | |
| 46 | \code | 
| 47 | var authInfo = connection.authenticationInformation; | 
| 48 | authInfo.setUsernameAuthentication("user1", "password"); | 
| 49 | connection.authenticationInformation = authInfo; | 
| 50 | \endcode | 
| 51 | |
| 52 | Current supported authentication methods are: | 
| 53 | |
| 54 | \list | 
| 55 | \li Anonymous | 
| 56 | \li Usernane | 
| 57 | \li Certificate | 
| 58 | \endlist | 
| 59 | |
| 60 | \sa setAnonymousAuthentication() setUsernameAuthentication() | 
| 61 | */ | 
| 62 | |
| 63 | class QOpcUaAuthenticationInformationData : public QSharedData | 
| 64 | { | 
| 65 | public: | 
| 66 | QVariant authenticationData; | 
| 67 | QOpcUaUserTokenPolicy::TokenType authenticationType; | 
| 68 | }; | 
| 69 | |
| 70 | /*! | 
| 71 | Default constructs an authentication information with no parameters set. | 
| 72 | */ | 
| 73 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation() | 
| 74 | : data(new QOpcUaAuthenticationInformationData) | 
| 75 | { | 
| 76 | setAnonymousAuthentication(); | 
| 77 | } | 
| 78 | |
| 79 | /*! | 
| 80 | Constructs an authentication information from \a rhs. | 
| 81 | */ | 
| 82 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation(const QOpcUaAuthenticationInformation &rhs) | 
| 83 | : data(rhs.data) | 
| 84 | { | 
| 85 | } | 
| 86 | |
| 87 | /*! | 
| 88 | Sets the values from \a rhs in this authentication information. | 
| 89 | */ | 
| 90 | QOpcUaAuthenticationInformation &QOpcUaAuthenticationInformation::operator=(const QOpcUaAuthenticationInformation &rhs) | 
| 91 | { | 
| 92 | if (this != &rhs) | 
| 93 | data.operator=(o: rhs.data); | 
| 94 | return *this; | 
| 95 | } | 
| 96 | |
| 97 | /*! | 
| 98 | Returns \c true if this authentication information has the same value as \a rhs. | 
| 99 | */ | 
| 100 | bool QOpcUaAuthenticationInformation::operator==(const QOpcUaAuthenticationInformation &rhs) const | 
| 101 | { | 
| 102 | return data->authenticationType == rhs.data->authenticationType && | 
| 103 | data->authenticationData == rhs.data->authenticationData; | 
| 104 | } | 
| 105 | |
| 106 | QOpcUaAuthenticationInformation::~QOpcUaAuthenticationInformation() | 
| 107 | { | 
| 108 | } | 
| 109 | |
| 110 | /*! | 
| 111 | \qmlmethod AuthenticationInformation::setAnonymousAuthentication() | 
| 112 | |
| 113 | Sets the authentication method to anonymous. | 
| 114 | */ | 
| 115 | |
| 116 | /*! | 
| 117 | Sets the authentication method to anonymous. | 
| 118 | */ | 
| 119 | void QOpcUaAuthenticationInformation::setAnonymousAuthentication() | 
| 120 | { | 
| 121 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Anonymous; | 
| 122 | data->authenticationData = QVariant(); | 
| 123 | } | 
| 124 | |
| 125 | /*! | 
| 126 | \qmlmethod AuthenticationInformation::setUsernameAuthentication(string username, string password) | 
| 127 | |
| 128 | Sets the authentication method to username, using the given \a username and \a password. | 
| 129 | */ | 
| 130 | |
| 131 | /*! | 
| 132 | Sets the authentication method to username, using the given \a username and \a password. | 
| 133 | */ | 
| 134 | void QOpcUaAuthenticationInformation::setUsernameAuthentication(const QString &username, const QString &password) | 
| 135 | { | 
| 136 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Username; | 
| 137 | data->authenticationData = QVariant::fromValue(value: qMakePair(value1: username, value2: password)); | 
| 138 | } | 
| 139 | |
| 140 | /*! | 
| 141 | Sets the authentication method to use certificates. | 
| 142 | |
| 143 | When using this authentication type a proper configured \l QOpcUaPkiConfiguration has to be set to | 
| 144 | the \l QOpcUaClient. | 
| 145 | |
| 146 | \sa QOpcUaPkiConfiguration QOpcUaClient::setPkiConfiguration() | 
| 147 | */ | 
| 148 | void QOpcUaAuthenticationInformation::setCertificateAuthentication() | 
| 149 | { | 
| 150 | data->authenticationData = QVariant(); | 
| 151 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Certificate; | 
| 152 | } | 
| 153 | |
| 154 | /*! | 
| 155 | The content of the \l QVariant returned by this method depends on the currently selected authentication method. | 
| 156 | */ | 
| 157 | const QVariant &QOpcUaAuthenticationInformation::authenticationData() const | 
| 158 | { | 
| 159 | return data->authenticationData; | 
| 160 | } | 
| 161 | |
| 162 | /*! | 
| 163 | Returns the current authentication type. | 
| 164 | |
| 165 | \sa QOpcUaUserTokenPolicy::TokenType | 
| 166 | */ | 
| 167 | QOpcUaUserTokenPolicy::TokenType QOpcUaAuthenticationInformation::authenticationType() const | 
| 168 | { | 
| 169 | return data->authenticationType; | 
| 170 | } | 
| 171 | |
| 172 | QT_END_NAMESPACE | 
| 173 | 
