| 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 | \deprecated [6.9] |
| 44 | |
| 45 | This class holds the information necessary to perform a login on a server. |
| 46 | |
| 47 | \code |
| 48 | var authInfo = connection.authenticationInformation; |
| 49 | authInfo.setUsernameAuthentication("user1", "password"); |
| 50 | connection.authenticationInformation = authInfo; |
| 51 | \endcode |
| 52 | |
| 53 | Current supported authentication methods are: |
| 54 | |
| 55 | \list |
| 56 | \li Anonymous |
| 57 | \li Usernane |
| 58 | \li Certificate |
| 59 | \endlist |
| 60 | |
| 61 | \sa setAnonymousAuthentication() setUsernameAuthentication() |
| 62 | */ |
| 63 | |
| 64 | class QOpcUaAuthenticationInformationData : public QSharedData |
| 65 | { |
| 66 | public: |
| 67 | QVariant authenticationData; |
| 68 | QOpcUaUserTokenPolicy::TokenType authenticationType; |
| 69 | }; |
| 70 | |
| 71 | /*! |
| 72 | Default constructs an authentication information with no parameters set. |
| 73 | */ |
| 74 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation() |
| 75 | : data(new QOpcUaAuthenticationInformationData) |
| 76 | { |
| 77 | setAnonymousAuthentication(); |
| 78 | } |
| 79 | |
| 80 | /*! |
| 81 | Constructs an authentication information from \a rhs. |
| 82 | */ |
| 83 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation(const QOpcUaAuthenticationInformation &rhs) |
| 84 | : data(rhs.data) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /*! |
| 89 | Sets the values from \a rhs in this authentication information. |
| 90 | */ |
| 91 | QOpcUaAuthenticationInformation &QOpcUaAuthenticationInformation::operator=(const QOpcUaAuthenticationInformation &rhs) |
| 92 | { |
| 93 | if (this != &rhs) |
| 94 | data.operator=(o: rhs.data); |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | Returns \c true if this authentication information has the same value as \a rhs. |
| 100 | */ |
| 101 | bool QOpcUaAuthenticationInformation::operator==(const QOpcUaAuthenticationInformation &rhs) const |
| 102 | { |
| 103 | return data->authenticationType == rhs.data->authenticationType && |
| 104 | data->authenticationData == rhs.data->authenticationData; |
| 105 | } |
| 106 | |
| 107 | QOpcUaAuthenticationInformation::~QOpcUaAuthenticationInformation() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | \qmlmethod AuthenticationInformation::setAnonymousAuthentication() |
| 113 | |
| 114 | Sets the authentication method to anonymous. |
| 115 | */ |
| 116 | |
| 117 | /*! |
| 118 | Sets the authentication method to anonymous. |
| 119 | */ |
| 120 | void QOpcUaAuthenticationInformation::setAnonymousAuthentication() |
| 121 | { |
| 122 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Anonymous; |
| 123 | data->authenticationData = QVariant(); |
| 124 | } |
| 125 | |
| 126 | /*! |
| 127 | \qmlmethod AuthenticationInformation::setUsernameAuthentication(string username, string password) |
| 128 | |
| 129 | Sets the authentication method to username, using the given \a username and \a password. |
| 130 | */ |
| 131 | |
| 132 | /*! |
| 133 | Sets the authentication method to username, using the given \a username and \a password. |
| 134 | */ |
| 135 | void QOpcUaAuthenticationInformation::setUsernameAuthentication(const QString &username, const QString &password) |
| 136 | { |
| 137 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Username; |
| 138 | data->authenticationData = QVariant::fromValue(value: qMakePair(value1: username, value2: password)); |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | Sets the authentication method to use certificates. |
| 143 | |
| 144 | When using this authentication type a proper configured \l QOpcUaPkiConfiguration has to be set to |
| 145 | the \l QOpcUaClient. |
| 146 | |
| 147 | \sa QOpcUaPkiConfiguration QOpcUaClient::setPkiConfiguration() |
| 148 | */ |
| 149 | void QOpcUaAuthenticationInformation::setCertificateAuthentication() |
| 150 | { |
| 151 | data->authenticationData = QVariant(); |
| 152 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Certificate; |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | \since 6.9 |
| 157 | |
| 158 | Sets the authentication method to certificate authentication with a |
| 159 | certificate different from the client certificate. |
| 160 | */ |
| 161 | void QOpcUaAuthenticationInformation::setCertificateAuthentication(const QString &certificatePath, |
| 162 | const QString &privateKeyPath) |
| 163 | { |
| 164 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Certificate; |
| 165 | data->authenticationData = QVariant::fromValue(value: QPair<QString, QString>(certificatePath, privateKeyPath)); |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | The content of the \l QVariant returned by this method depends on the currently selected authentication method. |
| 170 | */ |
| 171 | const QVariant &QOpcUaAuthenticationInformation::authenticationData() const |
| 172 | { |
| 173 | return data->authenticationData; |
| 174 | } |
| 175 | |
| 176 | /*! |
| 177 | Returns the current authentication type. |
| 178 | |
| 179 | \sa QOpcUaUserTokenPolicy::TokenType |
| 180 | */ |
| 181 | QOpcUaUserTokenPolicy::TokenType QOpcUaAuthenticationInformation::authenticationType() const |
| 182 | { |
| 183 | return data->authenticationType; |
| 184 | } |
| 185 | |
| 186 | QT_END_NAMESPACE |
| 187 | |