Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
PolkitAgent.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorServicePolkit/phosphorservicepolkit_export.h>
7// Full AuthRequest definition, not a forward declaration: it sits on this
8// header's moc surface — the `activeRequest` Q_PROPERTY and the
9// `authenticationRequested` signal both carry AuthRequest*, and Qt6 moc
10// auto-registers property/signal-parameter metatypes (QMetaType
11// SFINAE-probes completeness). A fwd decl re-fires GCC's -Wsfinae-incomplete
12// the moment mocs_compilation aggregation order stops shielding this header
13// (same rationale as the phosphor-service-pipewire headers).
15
16#include <QObject>
17#include <QString>
18
19#include <memory>
20#include <functional>
21
22namespace PhosphorServicePolkit {
23
24class ListenerImpl;
25class PolkitAgentTest;
26namespace detail {
27class AuthenticationSession;
28}
29
52class PHOSPHORSERVICEPOLKIT_EXPORT PolkitAgent : public QObject
53{
54 Q_OBJECT
55 Q_PROPERTY(bool registered READ registered NOTIFY registeredChanged)
57 Q_PROPERTY(Phase phase READ phase NOTIFY stateChanged)
58 Q_PROPERTY(bool inputReady READ inputReady NOTIFY stateChanged)
59 Q_PROPERTY(bool busy READ busy NOTIFY stateChanged)
60 Q_PROPERTY(bool canRetry READ canRetry NOTIFY stateChanged)
61
62public:
63 enum class Phase {
64 Idle,
65 Starting,
66 Prompt,
67 Checking,
68 Unavailable,
69 Failed,
70 Success,
71 Cancelled
72 };
73 Q_ENUM(Phase)
75 explicit PolkitAgent(QObject* parent = nullptr);
80 PolkitAgent(QString sessionId, QString objectPath, QObject* parent = nullptr);
81 ~PolkitAgent() override;
82
86 [[nodiscard]] bool registered() const;
87
89 [[nodiscard]] static QString defaultObjectPath();
90
94 Q_INVOKABLE bool registerAgent();
95
98 [[nodiscard]] AuthRequest* activeRequest() const;
99 [[nodiscard]] Phase phase() const;
100 [[nodiscard]] bool inputReady() const;
101 [[nodiscard]] bool busy() const;
102 [[nodiscard]] bool canRetry() const;
103
108 Q_INVOKABLE void authenticate();
109
112 Q_INVOKABLE void respond(const QString& response);
115 Q_INVOKABLE void selectIdentity(int index);
117 Q_INVOKABLE void retry();
118
122 Q_INVOKABLE void cancel();
123
124Q_SIGNALS:
134 void promptRequested(const QString& prompt, bool echo);
137 void authenticationCompleted(bool gainedAuthorization);
139 void authenticationError(const QString& text);
141 void authenticationInfo(const QString& text);
145
146private:
147 Q_DISABLE_COPY_MOVE(PolkitAgent)
148 friend class ListenerImpl;
149 friend class PolkitAgentTest;
150
153 void onSessionCompleted(bool gainedAuthorization);
154 void beginRequest(const QString& actionId, const QString& message, const QString& iconName,
155 const QVariantMap& details, const QString& cookie, const QStringList& identities,
156 std::function<detail::AuthenticationSession*(int, QObject*)> sessionFactory,
157 std::function<void()> completeResult);
158 void settleActive(Phase outcome);
159 void stopSession(bool cancel);
160 void setPhase(Phase phase);
161 void restartIdentity();
162
163 class Private;
164 std::unique_ptr<Private> d;
165};
166
167} // namespace PhosphorServicePolkit
One active PolicyKit authentication request, decoded from polkit's initiateAuthentication callback.
Definition AuthRequest.h:33
friend class ListenerImpl
Definition PolkitAgent.h:148
void selectIdentity(int index)
Restart the same request as another offered identity.
AuthRequest * activeRequest() const
The authentication request polkit is currently waiting on, or null.
void respond(const QString &response)
Answer the active PAM prompt.
void authenticationCancelled()
The active request ended with none remaining (declined here, or withdrawn by polkit).
static QString defaultObjectPath()
The default D-Bus object path the agent exports at.
friend class PolkitAgentTest
Definition PolkitAgent.h:149
void promptRequested(const QString &prompt, bool echo)
PAM is asking for input: answer it with respond().
void authenticationInfo(const QString &text)
PAM reported an informational message to show the user.
void authenticationError(const QString &text)
PAM reported an error (e.g. "Authentication failure") to show the user.
Phase
Definition PolkitAgent.h:63
void retry()
Retry an unavailable conversation, within the request's attempt limit.
void cancel()
Decline the active request: it completes without authorization and clears (aborting any running PAM c...
void authenticationRequested(PhosphorServicePolkit::AuthRequest *request)
A new authentication request arrived (also reflected in activeRequest).
bool registered() const
True once this process is the registered authentication agent for its session.
PolkitAgent(QObject *parent=nullptr)
Agent for the current session, at the default object path.
void authenticate()
Begin authenticating the active request as its selectedIdentity: starts the PAM conversation,...
bool registerAgent()
Register with polkitd as the session's authentication agent.
void authenticationCompleted(bool gainedAuthorization)
The PAM conversation finished; gainedAuthorization is true when the user authenticated successfully.
Definition PolkitAgent.h:26
Definition AuthRequest.h:13