Can store login tokens.

This commit is contained in:
Pierre HUBERT 2018-11-29 15:53:54 +01:00
parent 99724f845c
commit 1a3169bc16
13 changed files with 269 additions and 6 deletions

View File

@ -10,7 +10,10 @@ SOURCES += \
data/apirequestparameter.cpp \ data/apirequestparameter.cpp \
helpers/apihelper.cpp \ helpers/apihelper.cpp \
utils/jsonutils.cpp \ utils/jsonutils.cpp \
data/apirequestslist.cpp data/apirequestslist.cpp \
helpers/configurationhelper.cpp \
data/accountlogintokens.cpp \
widgets/mainwindow.cpp
HEADERS += \ HEADERS += \
helpers/accounthelper.h \ helpers/accounthelper.h \
@ -22,7 +25,11 @@ HEADERS += \
data/apirequestparameter.h \ data/apirequestparameter.h \
helpers/apihelper.h \ helpers/apihelper.h \
utils/jsonutils.h \ utils/jsonutils.h \
data/apirequestslist.h data/apirequestslist.h \
helpers/configurationhelper.h \
data/accountlogintokens.h \
widgets/mainwindow.h
FORMS += \ FORMS += \
widgets/loginwidget.ui widgets/loginwidget.ui \
widgets/mainwindow.ui

View File

@ -6,6 +6,13 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
/**
* Application information
*/
#define ORGANIZATION_NAME "Communiquons"
#define ORGANIZATION_DOMAIN "communiquons.org"
#define APPLICATION_NAME "ComunicMessages"
/** /**
* API credentials * API credentials
*/ */
@ -14,5 +21,10 @@
#define API_SERVICE_TOKEN "cWHlmMS5A1" #define API_SERVICE_TOKEN "cWHlmMS5A1"
/**
* Settings information
*/
#define SETTINGS_ACCOUNT_LOGIN_TOKEN_1 "account_login_token_1"
#define SETTINGS_ACCOUNT_LOGIN_TOKEN_2 "account_login_token_2"
#endif // CONFIG_H #endif // CONFIG_H

View File

@ -17,6 +17,7 @@ enum LoginResult {
INVALID_CREDENTIALS, INVALID_CREDENTIALS,
TOO_MANY_REQUEST, TOO_MANY_REQUEST,
NETWORK_ERROR, NETWORK_ERROR,
INVALID_SERVER_RESPONSE,
OTHER_ERROR OTHER_ERROR
}; };

View File

@ -0,0 +1,31 @@
#include "accountlogintokens.h"
AccountLoginTokens::AccountLoginTokens()
{
}
bool AccountLoginTokens::isValid()
{
return token1().length() > 0 && token2().length() > 0;
}
QString AccountLoginTokens::token1() const
{
return mToken1;
}
void AccountLoginTokens::setToken1(const QString &token1)
{
mToken1 = token1;
}
QString AccountLoginTokens::token2() const
{
return mToken2;
}
void AccountLoginTokens::setToken2(const QString &token2)
{
mToken2 = token2;
}

36
data/accountlogintokens.h Normal file
View File

@ -0,0 +1,36 @@
/**
* Account Login tokens
*
* @author Pierre HUBERT
*/
#ifndef ACCOUNTLOGINTOKENS_H
#define ACCOUNTLOGINTOKENS_H
#include <QString>
class AccountLoginTokens
{
public:
AccountLoginTokens();
/**
* Check if the tokens present in this object
* are valid or not
*
* @return TRUE if the tokens are valid / FALSE else
*/
bool isValid();
QString token1() const;
void setToken1(const QString &token1);
QString token2() const;
void setToken2(const QString &token2);
private:
QString mToken1;
QString mToken2;
};
#endif // ACCOUNTLOGINTOKENS_H

View File

@ -1,6 +1,9 @@
#include <QJsonObject>
#include "accounthelper.h" #include "accounthelper.h"
#include "configurationhelper.h"
#include "../data/apirequest.h" #include "../data/apirequest.h"
#include "../data/accountlogintokens.h"
#include "../helpers/apihelper.h" #include "../helpers/apihelper.h"
AccountHelper::AccountHelper(QObject *parent) : QObject(parent) AccountHelper::AccountHelper(QObject *parent) : QObject(parent)
@ -10,7 +13,7 @@ AccountHelper::AccountHelper(QObject *parent) : QObject(parent)
bool AccountHelper::signedIn() bool AccountHelper::signedIn()
{ {
return false; //TODO : implement return ConfigurationHelper().getAccountTokens().isValid();
} }
void AccountHelper::login(const AccountLoginRequest &info) void AccountHelper::login(const AccountLoginRequest &info)
@ -53,7 +56,20 @@ void AccountHelper::requestLoginResult(const QJsonDocument &document)
//Delete API request //Delete API request
qobject_cast<APIRequest *>(sender())->deleteLater(); qobject_cast<APIRequest *>(sender())->deleteLater();
//Intend to parse server response
QJsonObject object = document.object().value("tokens").toObject();
if(object.isEmpty()){
qWarning("objects 'tokens' of server response is empty!");
emit loginResult(LoginResult::INVALID_SERVER_RESPONSE);
return;
}
//Intend to parse get login responses
AccountLoginTokens tokens;
tokens.setToken1(object.value("token1").toString());
tokens.setToken2(object.value("token2").toString());
ConfigurationHelper().setAccountTokens(tokens);
//Success //Success
emit loginResult(LoginResult::LOGIN_SUCCESS); emit loginResult(LoginResult::LOGIN_SUCCESS);

View File

@ -0,0 +1,34 @@
#include <QSettings>
#include "configurationhelper.h"
#include "../config.h"
ConfigurationHelper::ConfigurationHelper()
{
mSettings = new QSettings();
}
ConfigurationHelper::~ConfigurationHelper()
{
mSettings->deleteLater();
}
AccountLoginTokens ConfigurationHelper::getAccountTokens()
{
//Check if we have both of the tokens
if(!mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid() ||
!mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid())
return AccountLoginTokens(); //Return invalid object : not all settings available
//Parse and return account login tokens
AccountLoginTokens tokens;
tokens.setToken1(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).toString());
tokens.setToken2(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_2).toString());
return tokens;
}
void ConfigurationHelper::setAccountTokens(const AccountLoginTokens &tokens)
{
mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_1, tokens.token1());
mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_2, tokens.token2());
}

View File

@ -0,0 +1,41 @@
/**
* Configuration helper
*
* @author Pierre HUBERT
*/
#ifndef CONFIGURATIONHELPER_H
#define CONFIGURATIONHELPER_H
#include <QObject>
#include "../data/accountlogintokens.h"
class QSettings;
class ConfigurationHelper
{
public:
ConfigurationHelper();
~ConfigurationHelper();
/**
* Get the login tokens associated with this account
*
* @return The login tokens associated with this account /
* invalid object if none found
*/
AccountLoginTokens getAccountTokens();
/**
* Set the login tokens associated with the current account
*
* @param tokens The tokens to set
*/
void setAccountTokens(const AccountLoginTokens &tokens);
private:
QSettings *mSettings;
};
#endif // CONFIGURATIONHELPER_H

View File

@ -6,13 +6,22 @@
#include "helpers/accounthelper.h" #include "helpers/accounthelper.h"
#include "widgets/loginwidget.h" #include "widgets/loginwidget.h"
#include "widgets/mainwindow.h"
#include "config.h"
int main(int argc, char** argv){ int main(int argc, char** argv){
QApplication app(argc, argv); QApplication app(argc, argv);
//Define some basic values
QCoreApplication::setOrganizationName(ORGANIZATION_NAME);
QCoreApplication::setOrganizationDomain(ORGANIZATION_DOMAIN);
QCoreApplication::setApplicationName(APPLICATION_NAME);
//Determine whether user is signed in or not //Determine whether user is signed in or not
if(AccountHelper().signedIn()) if(AccountHelper().signedIn()){
qFatal("Can not handle signed in users yet!"); MainWindow *mainWindow = new MainWindow;
mainWindow->show();
}
else { else {
LoginWidget *widget = new LoginWidget(); LoginWidget *widget = new LoginWidget();
widget->show(); widget->show();

View File

@ -2,6 +2,7 @@
#include "loginwidget.h" #include "loginwidget.h"
#include "ui_loginwidget.h" #include "ui_loginwidget.h"
#include "mainwindow.h"
#include "../utils/accountutils.h" #include "../utils/accountutils.h"
#include "../helpers/accounthelper.h" #include "../helpers/accounthelper.h"
@ -25,6 +26,10 @@ void LoginWidget::loginResult(LoginResult result)
if(result == LoginResult::LOGIN_SUCCESS){ if(result == LoginResult::LOGIN_SUCCESS){
qDebug("User successfully signed in."); qDebug("User successfully signed in.");
//Open the window
MainWindow *mainWindow = new MainWindow();
mainWindow->show();
close(); close();
return; return;
} }
@ -46,6 +51,10 @@ void LoginWidget::loginResult(LoginResult result)
showError(tr("Too many login attempts from your computer. Please try again later...")); showError(tr("Too many login attempts from your computer. Please try again later..."));
break; break;
case LoginResult::INVALID_SERVER_RESPONSE:
showError(tr("Could not understand server response!"));
break;
case LoginResult::OTHER_ERROR: case LoginResult::OTHER_ERROR:
default: default:
showError(tr("Login attempt did not succeed.")); showError(tr("Login attempt did not succeed."));

14
widgets/mainwindow.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}

22
widgets/mainwindow.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

31
widgets/mainwindow.ui Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>ComunicMessages</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>