1 | /* |
2 | * Copyright (C) by Olivier Goffart <ogoffart@woboq.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
12 | * for more details. |
13 | */ |
14 | |
15 | #pragma once |
16 | #include <QPointer> |
17 | #include <QTcpServer> |
18 | #include <QUrl> |
19 | #include "accountfwd.h" |
20 | |
21 | namespace OCC { |
22 | |
23 | /** |
24 | * Job that do the authorization grant and fetch the access token |
25 | * |
26 | * Normal workflow: |
27 | * |
28 | * --> start() |
29 | * | |
30 | * +----> openBrowser() open the browser to the login page, redirects to http://localhost:xxx |
31 | * | |
32 | * +----> _server starts listening on a TCP port waiting for an HTTP request with a 'code' |
33 | * | |
34 | * v |
35 | * request the access_token and the refresh_token via 'apps/oauth2/api/v1/token' |
36 | * | |
37 | * v |
38 | * emit result(...) |
39 | * |
40 | */ |
41 | class OAuth : public QObject |
42 | { |
43 | Q_OBJECT |
44 | public: |
45 | OAuth(Account *account, QObject *parent) |
46 | : QObject(parent) |
47 | , _account(account) |
48 | { |
49 | } |
50 | ~OAuth(); |
51 | |
52 | enum Result { NotSupported, |
53 | LoggedIn, |
54 | Error }; |
55 | Q_ENUM(Result); |
56 | void start(); |
57 | bool openBrowser(); |
58 | QUrl authorisationLink() const; |
59 | |
60 | signals: |
61 | /** |
62 | * The state has changed. |
63 | * when logged in, token has the value of the token. |
64 | */ |
65 | void result(OAuth::Result result, const QString &user = QString(), const QString &token = QString(), const QString &refreshToken = QString()); |
66 | |
67 | private: |
68 | Account *_account; |
69 | QTcpServer _server; |
70 | |
71 | public: |
72 | QString _expectedUser; |
73 | }; |
74 | |
75 | |
76 | } // namespace OCC |
77 | |