-
Notifications
You must be signed in to change notification settings - Fork 688
Expand file tree
/
Copy pathrequestauthenticationcontroller.cpp
More file actions
127 lines (112 loc) · 4.2 KB
/
requestauthenticationcontroller.cpp
File metadata and controls
127 lines (112 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (C) Lisa Reese <lisa.reese@kiteworks.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "requestauthenticationcontroller.h"
#include "accountsgui/accountview.h"
#include "application.h"
#include "requestauthenticationwidget.h"
#include "accountmodalwidget.h"
#include "settingsdialog.h"
#include <config/appconfig.h>
namespace OCC {
/**
* @brief RequestAuthenticationController::RequestAuthenticationController
* @param widget - may be null, for example when using the "log in" function after user explicitly logged out
* @param parent - the usual qt parenting scheme
*/
RequestAuthenticationController::RequestAuthenticationController(RequestAuthenticationWidget *widget, QObject *parent)
: QObject{parent}
, _widget(widget)
{
}
void RequestAuthenticationController::handleSignIn()
{
_oauth->openBrowser();
}
void RequestAuthenticationController::handleLogOut()
{
if (_modalWidget) {
// this calls finished which deletes the modal widget.
// our widget should also be deleted as it has been added to the layout of the modal
// widget, which effectively reparents it to the modal widget.
_modalWidget->reject();
}
// this ultimately needs to be chained to creds::requestLogout signal
Q_EMIT requestLogout();
}
void RequestAuthenticationController::startAuthentication(Account *account)
{
Q_ASSERT(account);
if (_oauth) {
delete _oauth;
_oauth = nullptr;
}
AppConfig systemConfig;
_account = account;
_oauth = new AccountBasedOAuth(_account, systemConfig.openIdConfig(), this);
connect(_oauth, &OAuth::authorisationLinkChanged, this, &RequestAuthenticationController::authUrlReady);
connect(_oauth, &OAuth::result, this, &RequestAuthenticationController::handleOAuthResult);
if (_widget && _modalWidget == nullptr) { // first show of the gui
connect(_widget, &RequestAuthenticationWidget::connectClicked, this, &RequestAuthenticationController::handleSignIn);
connect(_widget, &RequestAuthenticationWidget::stayLoggedOutClicked, this, &RequestAuthenticationController::handleLogOut);
AccountView *accountView = ocApp()->gui()->settingsDialog()->accountView(_account);
_modalWidget = new AccountModalWidget(QString(), _widget, accountView);
accountView->addModalAccountWidget(_modalWidget);
}
_oauth->startAuthentication();
}
void RequestAuthenticationController::authUrlReady()
{
if (_widget) {
_widget->setAuthUrl(_oauth->authorisationLink().toString(QUrl::FullyEncoded));
} else {
// just run with it
_oauth->openBrowser();
}
}
void RequestAuthenticationController::handleOAuthResult(OAuth::Result result, const QString &accessToken, const QString &refreshToken)
{
QString errString;
switch (result) {
case OAuth::ErrorIdPUnreachable:
errString = tr("IdP is unreachable. Contact your system administrator or try again later.");
break;
case OAuth::NotSupported:
// also should not happen after initial setup?
Q_ASSERT(false);
[[fallthrough]];
case OAuth::ErrorInsecureUrl:
// should not happen after the initial setup
Q_ASSERT(false);
[[fallthrough]];
case OAuth::Error:
errString = tr("Authentication failed.");
break;
case OAuth::LoggedIn:
errString.clear();
break;
}
if (!errString.isEmpty() && _account) {
if (_widget) {
_widget->setErrorMessage(errString);
startAuthentication(_account);
} else
Q_EMIT authenticationFailed(errString);
} else {
if (_modalWidget)
_modalWidget->accept();
Q_EMIT authenticationSucceeded(accessToken, refreshToken);
}
ownCloudGui::raise();
}
}