File indexing completed on 2024-05-12 05:01:56

0001 /*
0002 
0003  * SPDX-FileCopyrightText: 2020 Alessandro Ambrosano <alessandro.ambrosano@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  *
0007  */
0008 
0009 #pragma once
0010 
0011 #include "ddpapi/ddpmanager.h"
0012 #include "libruqolacore_export.h"
0013 
0014 #include <QJsonObject>
0015 
0016 class LIBRUQOLACORE_EXPORT DDPAuthenticationManager : public DDPManager
0017 {
0018     Q_OBJECT
0019 
0020     enum class Method {
0021         Login,
0022         SendOtp,
0023         Logout,
0024         LogoutCleanUp,
0025     };
0026 
0027     static QString METHOD_LOGIN;
0028     static QString METHOD_SEND_OTP;
0029     static QString METHOD_LOGOUT;
0030     static QString METHOD_LOGOUT_CLEAN_UP;
0031 
0032 public:
0033     // state == LoginOngoing for all the time since the login request until a response
0034     //   comes back, then it may result in
0035     //   - LoggedIn
0036     //   - LoginOtpRequired
0037     //   - LoginFailedInvalidUserOrPassword
0038     //   - GenericError
0039     // state == LoginOtpAuthOngoing since when the otp code is sent to the server until
0040     //  a response comes back, then it may become:
0041     //   - LoggedIn
0042     //   - LoginFailedInvalidOtp
0043     //   - GenericError
0044     // state == LogoutOngoing since the logout request is sent until a response is received,
0045     //   next states could be
0046     //   - LoggedOut
0047     //   - GenericError
0048     // state == LogoutCleanUpOngoing since the clean up request is sent until a response is
0049     //   received, resulting in one of these states:
0050     //   - LoggedOutAndCleanedUp
0051     //   - GenericError
0052     // GenericError is used when the class doesn't know what else to do, and is irreversible
0053     enum LoginStatus {
0054         Connecting,
0055         LoginOngoing,
0056         LoggedIn,
0057         LoginFailedInvalidUserOrPassword,
0058         LoginOtpRequired,
0059         LoginOtpAuthOngoing,
0060         LoginFailedInvalidOtp,
0061         LogoutOngoing,
0062         LoggedOut,
0063         LogoutCleanUpOngoing,
0064         LoggedOutAndCleanedUp,
0065         FailedToLoginPluginProblem,
0066         LoginFailedUserNotActivated,
0067         LoginFailedLoginBlockForIp,
0068         LoginFailedLoginBlockedForUser,
0069         LoginFailedLoginAppNotAllowedToLogin,
0070         GenericError,
0071     };
0072     Q_ENUM(LoginStatus)
0073 
0074     explicit DDPAuthenticationManager(DDPClient *ddpClient, QObject *parent = nullptr);
0075     ~DDPAuthenticationManager() override;
0076 
0077     void login();
0078     void login(const QString &user, const QString &password);
0079     void loginLDAP(const QString &user, const QString &password); // TODO: LDAP options?
0080     void loginOAuth(const QString &credentialToken, const QString &credentialSecret);
0081     void sendOTP(const QString &otp);
0082     void logout();
0083 
0084     void setAuthToken(const QString &authToken);
0085 
0086     [[nodiscard]] QString userId() const;
0087     [[nodiscard]] QString authToken() const;
0088     [[nodiscard]] bool isLoggedIn() const;
0089     [[nodiscard]] bool isLoggedOut() const;
0090     [[nodiscard]] LoginStatus loginStatus() const;
0091     void setLoginStatus(LoginStatus newStatus);
0092 
0093     [[nodiscard]] qint64 tokenExpires() const;
0094 
0095 Q_SIGNALS:
0096     void loginStatusChanged();
0097 
0098 private:
0099     QString mUserId;
0100     QString mAuthToken;
0101     qint64 mTokenExpires;
0102     LoginStatus mLoginStatus = LoggedOut;
0103     // Used when sending OTP
0104     QJsonObject mLastLoginPayload;
0105 
0106     LIBRUQOLACORE_NO_EXPORT void processMethodResponseImpl(int operationId, const QJsonObject &response) override;
0107 
0108     // Authentication doesn't involve any subscriptions
0109     LIBRUQOLACORE_NO_EXPORT void processSubscriptionResultImpl(int subscriptionId, const QJsonObject &result) override
0110     {
0111         Q_UNUSED(subscriptionId)
0112         Q_UNUSED(result)
0113     }
0114 
0115     LIBRUQOLACORE_NO_EXPORT void loginImpl(const QJsonArray &params);
0116     LIBRUQOLACORE_NO_EXPORT void clientConnectedChangedSlot();
0117     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT bool checkGenericError() const;
0118 };