fixed up bluemonkey configuration
[profile/ivi/automotive-message-broker.git] / plugins / bluemonkey / authenticate.cpp
1 #include "authenticate.h"
2 #include <QSettings>
3 #include <QJSEngine>
4 #include <QFile>
5 #include <QtDebug>
6
7 Authenticate::Authenticate(std::map<std::string, std::string> config, QObject *parent) :
8         QObject(parent)
9 {
10         QSettings settings;
11         authorized = settings.value("authorized").toStringList();
12
13         QJSEngine engine;
14
15         QJSValue eventEngineValue = engine.newQObject(this);
16         engine.globalObject().setProperty("auth", eventEngineValue);
17
18         QString str = config["authSettings"].c_str();
19
20         QFile file(str);
21         if(!file.open(QIODevice::ReadOnly))
22         {
23                 qDebug()<<"failed to open config file: "<<str;
24                 return;
25         }
26
27         QString script = file.readAll();
28
29         file.close();
30
31         engine.evaluate(script);
32
33 }
34
35 bool Authenticate::isAuthorized(QString prefix)
36 {
37         return(authorized.contains(prefix));
38 }
39
40 bool Authenticate::authorize(QString prefix, QString password)
41 {
42         if(password == mPin && !authorized.contains(prefix))
43         {
44                 authorized.append(prefix);
45                 QSettings settings;
46                 settings.setValue("authorized", authorized);
47                 qDebug("authorized");
48
49                 return true;
50         }
51
52         qDebug("failed to authorize or already authorized");
53         return false;
54 }