MailTransport

outlookoauthtokenrequester.h
1/*
2 SPDX-FileCopyrightText: 2024 Daniel Vrátil <dvratil@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "mailtransport_export.h"
10
11#include <QObject>
12#include <QString>
13#include <QUrl>
14
15#include <memory>
16#include <optional>
17
18class QTcpSocket;
19class QTcpServer;
20class QNetworkReply;
22
23namespace MailTransport
24{
25
26class MAILTRANSPORT_EXPORT TokenResult
27{
28public:
29 enum ErrorCode {
30 OK = 0, // success
32 InvalidAuthorizationResponse,
33 AuthorizationFailed,
34 };
35
36 TokenResult(ErrorCode errorCode, const QString &errorText);
37 TokenResult(const QString &accessToken, const QString &refreshToken);
38
39 [[nodiscard]] QString accessToken() const;
40 [[nodiscard]] QString refreshToken() const;
41
42 [[nodiscard]] bool hasError() const;
43 [[nodiscard]] ErrorCode errorCode() const;
44 [[nodiscard]] QString errorText() const;
45
46private:
47 ErrorCode mErrorCode = ErrorCode::OK;
48 QString mErrorText;
49 QString mAccessToken;
50 QString mRefreshToken;
51};
52
53class PKCE;
54class MAILTRANSPORT_EXPORT OutlookOAuthTokenRequester : public QObject
55{
56 Q_OBJECT
57public:
58 explicit OutlookOAuthTokenRequester(const QString &clientId, const QString &tenantId, const QStringList &scopes, QObject *parent = nullptr);
59 ~OutlookOAuthTokenRequester() override;
60
61 void requestToken(const QString &usernameHint = {});
62 void refreshToken(const QString &refreshToken);
63
64Q_SIGNALS:
65 void finished(const TokenResult &result);
66
67private:
68 [[nodiscard]] MAILTRANSPORT_NO_EXPORT std::optional<QUrl> startLocalHttpServer();
69
70 MAILTRANSPORT_NO_EXPORT void handleNewConnection();
71 MAILTRANSPORT_NO_EXPORT void handleSocketReadyRead();
72 MAILTRANSPORT_NO_EXPORT void requestIdToken(const QString &code);
73 MAILTRANSPORT_NO_EXPORT void handleTokenResponse(QNetworkReply *reply, bool isTokenRefresh = false);
74 MAILTRANSPORT_NO_EXPORT void sendResponseToBrowserAndCloseSocket();
75
76private:
77 QString mClientId;
78 QString mTenantId;
79 QStringList mScopes;
80 QUrl mRedirectUri;
81 std::unique_ptr<PKCE> mPkce;
82 std::unique_ptr<QTcpServer> mHttpServer;
83 std::unique_ptr<QTcpSocket> mSocket;
84 std::unique_ptr<QNetworkAccessManager> mNam;
85};
86
87} // namespace MailTransport
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.