1 /*
2  * Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
3  * Copyright (C) by Michael Schuster <michael@schuster.ms>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  */
15 
16 #pragma once
17 #include <QPointer>
18 #include <QUrl>
19 #include <QTimer>
20 #include "accountfwd.h"
21 
22 namespace OCC {
23 
24 /**
25  * Job that does the authorization, grants and fetches the access token via Login Flow v2
26  *
27  * See: https://docs.nextcloud.com/server/latest/developer_manual/client_apis/LoginFlow/index.html#login-flow-v2
28  */
29 class Flow2Auth : public QObject
30 {
31     Q_OBJECT
32 public:
33     enum TokenAction {
34         actionOpenBrowser = 1,
35         actionCopyLinkToClipboard
36     };
37     enum PollStatus {
38         statusPollCountdown = 1,
39         statusPollNow,
40         statusFetchToken,
41         statusCopyLinkToClipboard
42     };
43 
44     Flow2Auth(Account *account, QObject *parent);
45     ~Flow2Auth() override;
46 
47     enum Result { NotSupported,
48         LoggedIn,
49         Error };
50     Q_ENUM(Result);
51     void start();
52     void openBrowser();
53     void copyLinkToClipboard();
54     QUrl authorisationLink() const;
55 
56 signals:
57     /**
58      * The state has changed.
59      * when logged in, appPassword has the value of the app password.
60      */
61     void result(Flow2Auth::Result result, const QString &errorString = QString(),
62                 const QString &user = QString(), const QString &appPassword = QString());
63 
64     void statusChanged(const PollStatus status, int secondsLeft);
65 
66 public slots:
67     void slotPollNow();
68 
69 private slots:
70     void slotPollTimerTimeout();
71 
72 private:
73     void fetchNewToken(const TokenAction action);
74 
75     Account *_account;
76     QUrl _loginUrl;
77     QString _pollToken;
78     QString _pollEndpoint;
79     QTimer _pollTimer;
80     qint64 _secondsLeft;
81     qint64 _secondsInterval;
82     bool _isBusy;
83     bool _hasToken;
84     bool _enforceHttps = false;
85 };
86 
87 } // namespace OCC
88