1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  * Copyright (C) 2012-2016 Canonical Ltd.
6  *
7  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 #include "exampleplugin.h"
25 #include "exampledata.h"
26 #include "SignOn/signonplugincommon.h"
27 
28 using namespace SignOn;
29 
30 namespace ExamplePluginNS {
31 
ExamplePlugin(QObject * parent)32 ExamplePlugin::ExamplePlugin(QObject *parent):
33     AuthPluginInterface(parent)
34 {
35     TRACE();
36     m_showTos = false;
37 }
38 
~ExamplePlugin()39 ExamplePlugin::~ExamplePlugin()
40 {
41     TRACE();
42 }
43 
type() const44 QString ExamplePlugin::type() const
45 {
46     return QLatin1String("example");
47 }
48 
mechanisms() const49 QStringList ExamplePlugin::mechanisms() const
50 {
51     QStringList res = QStringList(QLatin1String("default"));
52     res << QLatin1String("example");
53 
54     return res;
55 }
56 
cancel()57 void ExamplePlugin::cancel()
58 {
59 }
60 /*
61  * example plugin is used for testing purposes only
62  * */
process(const SignOn::SessionData & inData,const QString & mechanism)63 void ExamplePlugin::process(const SignOn::SessionData &inData,
64                             const QString &mechanism )
65 {
66     ExampleData response;
67     ExampleData input = inData.data<ExampleData>();
68 
69     if (!mechanisms().contains(mechanism) ) {
70         TRACE() << "invalid mechanism: " << mechanism;
71         // next is commented to allow invalid machanism to be used
72         /*
73         emit error(PLUGIN_ERROR_MECHANISM_NOT_SUPPORTED);
74         return;
75         */
76     }
77     TRACE() << "User: " << inData.UserName() ;
78     TRACE() << "Example" << input.Example();
79 
80     if (input.Params() == QLatin1String("Example")) {
81         qDebug() << inData.UserName();
82         response.setExample(QLatin1String("authenticated"));
83         emit result(response);
84         return;
85     }
86 
87     if (input.Params() == QLatin1String("error")) {
88         emit error(Error::NotAuthorized);
89         return;
90     }
91 
92     if (input.Params() == QLatin1String("toserror")) {
93         emit error(Error::TOSNotAccepted);
94         return;
95     }
96 
97     if (input.Params() == QLatin1String("store")) {
98         ExampleData storeData;
99         storeData.setExample(QLatin1String("store:") + input.Example());
100         emit store(storeData);
101     }
102 
103     if (input.Params() == QLatin1String("url")) {
104         SignOn::UiSessionData data;
105         data.setOpenUrl(input.Example());
106         data.setNetworkProxy(inData.NetworkProxy());
107         emit userActionRequired(data);
108 
109         return;
110     }
111 
112     if (input.Params() == QLatin1String("ui")) {
113         SignOn::UiSessionData data;
114         data.setQueryPassword(true);
115         data.setQueryUserName(true);
116         emit userActionRequired(data);
117 
118         return;
119     }
120 
121     if (input.Params() == QLatin1String("captcha")) {
122         SignOn::UiSessionData data;
123         data.setCaptchaUrl(input.Example());
124         data.setNetworkProxy(inData.NetworkProxy());
125         emit userActionRequired(data);
126 
127         return;
128     }
129 
130     if (!input.Tos().isEmpty()) {
131         SignOn::UiSessionData data;
132         //% "Click here to see TOS update"
133         /*
134         QString tos("Terms of service has changed. Click <a href=\"%1\">"
135                     "here " "! </a> to see changes.");
136         */
137         QString tos = input.Tos();
138         data.setQueryMessage(tos.arg(input.Example()));
139         data.setOpenUrl(input.Example());
140         m_showTos = true;
141         emit userActionRequired(data);
142 
143         return;
144     }
145 
146     response.setExample(QLatin1String("authenticated"));
147     TRACE() << "Emitting results";
148 
149     emit store(response);
150     emit result(response);
151     return;
152 }
153 
154 
userActionFinished(const SignOn::UiSessionData & data)155 void ExamplePlugin::userActionFinished(const SignOn::UiSessionData &data)
156 {
157     Q_UNUSED(data);
158     ExampleData response;
159     TRACE();
160     if (m_showTos) {
161         m_showTos = false;
162         if (data.QueryErrorCode() != QUERY_ERROR_NONE) {
163             emit error(Error::TOSNotAccepted);
164             return;
165         }
166     }
167 
168     response.setExample(QLatin1String("signon-ui shown"));
169     emit result(response);
170 
171 }
172 
173 SIGNON_DECL_AUTH_PLUGIN(ExamplePlugin)
174 
175 } //namespace ExamplePluginNS
176