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 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation() |
71 | : data(new QOpcUaAuthenticationInformationData) |
72 | { |
73 | setAnonymousAuthentication(); |
74 | } |
75 | |
76 | /*! |
77 | Constructs an authentication information from \a rhs. |
78 | */ |
79 | QOpcUaAuthenticationInformation::QOpcUaAuthenticationInformation(const QOpcUaAuthenticationInformation &rhs) |
80 | : data(rhs.data) |
81 | { |
82 | } |
83 | |
84 | /*! |
85 | Sets the values from \a rhs in this authentication information. |
86 | */ |
87 | QOpcUaAuthenticationInformation &QOpcUaAuthenticationInformation::operator=(const QOpcUaAuthenticationInformation &rhs) |
88 | { |
89 | if (this != &rhs) |
90 | data.operator=(o: rhs.data); |
91 | return *this; |
92 | } |
93 | |
94 | /*! |
95 | Returns \c true if this authentication information has the same value as \a rhs. |
96 | */ |
97 | bool QOpcUaAuthenticationInformation::operator==(const QOpcUaAuthenticationInformation &rhs) const |
98 | { |
99 | return data->authenticationType == rhs.data->authenticationType && |
100 | data->authenticationData == rhs.data->authenticationData; |
101 | } |
102 | |
103 | QOpcUaAuthenticationInformation::~QOpcUaAuthenticationInformation() |
104 | { |
105 | } |
106 | |
107 | /*! |
108 | \qmlmethod AuthenticationInformation::setAnonymousAuthentication() |
109 | |
110 | Sets the authentication method to anonymous. |
111 | */ |
112 | |
113 | /*! |
114 | Sets the authentication method to anonymous. |
115 | */ |
116 | void QOpcUaAuthenticationInformation::setAnonymousAuthentication() |
117 | { |
118 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Anonymous; |
119 | data->authenticationData = QVariant(); |
120 | } |
121 | |
122 | /*! |
123 | \qmlmethod AuthenticationInformation::setUsernameAuthentication(string username, string password) |
124 | |
125 | Sets the authentication method to username, using the given \a username and \a password. |
126 | */ |
127 | |
128 | /*! |
129 | Sets the authentication method to username, using the given \a username and \a password. |
130 | */ |
131 | void QOpcUaAuthenticationInformation::setUsernameAuthentication(const QString &username, const QString &password) |
132 | { |
133 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Username; |
134 | data->authenticationData = QVariant::fromValue(value: qMakePair(value1: username, value2: password)); |
135 | } |
136 | |
137 | /*! |
138 | Sets the authentication method to use certificates. |
139 | |
140 | When using this authentication type a proper configured \l QOpcUaPkiConfiguration has to be set to |
141 | the \l QOpcUaClient. |
142 | |
143 | \sa QOpcUaPkiConfiguration QOpcUaClient::setPkiConfiguration() |
144 | */ |
145 | void QOpcUaAuthenticationInformation::setCertificateAuthentication() |
146 | { |
147 | data->authenticationData = QVariant(); |
148 | data->authenticationType = QOpcUaUserTokenPolicy::TokenType::Certificate; |
149 | } |
150 | |
151 | /*! |
152 | The content of the \l QVariant returned by this method depends on the currently selected authentication method. |
153 | */ |
154 | const QVariant &QOpcUaAuthenticationInformation::authenticationData() const |
155 | { |
156 | return data->authenticationData; |
157 | } |
158 | |
159 | /*! |
160 | Returns the current authentication type. |
161 | |
162 | \sa QOpcUaUserTokenPolicy::TokenType |
163 | */ |
164 | QOpcUaUserTokenPolicy::TokenType QOpcUaAuthenticationInformation::authenticationType() const |
165 | { |
166 | return data->authenticationType; |
167 | } |
168 | |
169 | QT_END_NAMESPACE |
170 |