Commit 587c42a7 authored by Gus Grubba's avatar Gus Grubba

Decode anonymous pilot ID off the JSON Web Token returned by the anonymous login.

parent 2de27a63
......@@ -1161,6 +1161,8 @@ contains (DEFINES, QGC_AIRMAP_ENABLED) {
DEFINES += QGC_AIRMAP_KEY_AVAILABLE
}
include(src/Airmap/QJsonWebToken/src/qjsonwebtoken.pri)
} else {
#-- Dummies
INCLUDEPATH += \
......
......@@ -48,7 +48,6 @@ private:
State _state = State::Idle;
AirMapSharedState& _shared;
QString _flightID;
QString _pilotID; ///< Pilot ID in the form "auth0|abc123"
QGCGeoBoundingCube _searchArea;
};
......@@ -270,7 +270,7 @@ AirMapFlightPlanManager::endFlight(QString flightID)
{
qCDebug(AirMapManagerLog) << "End flight";
_flightToEnd = flightID;
if (_pilotID == "") {
if (_shared.pilotID().isEmpty()) {
//-- Need to get the pilot id
qCDebug(AirMapManagerLog) << "Getting pilot ID";
_state = State::GetPilotID;
......@@ -283,8 +283,9 @@ AirMapFlightPlanManager::endFlight(QString flightID)
if (!isAlive.lock()) return;
if (_state != State::GetPilotID) return;
if (result) {
_pilotID = QString::fromStdString(result.value().id);
qCDebug(AirMapManagerLog) << "Got Pilot ID:"<<_pilotID;
QString pilotID = QString::fromStdString(result.value().id);
_shared.setPilotID(pilotID);
qCDebug(AirMapManagerLog) << "Got Pilot ID:" << pilotID;
_state = State::Idle;
_endFlight();
} else {
......@@ -380,7 +381,7 @@ AirMapFlightPlanManager::_createFlightPlan()
qCDebug(AirMapManagerLog) << "Flight Start:" << flightStartTime().toString();
qCDebug(AirMapManagerLog) << "Flight Duration: " << flightDuration();
if (_pilotID == "" && !_shared.settings().userName.isEmpty() && !_shared.settings().password.isEmpty()) {
if (_shared.pilotID().isEmpty() && !_shared.settings().userName.isEmpty() && !_shared.settings().password.isEmpty()) {
//-- Need to get the pilot id before uploading the flight plan
qCDebug(AirMapManagerLog) << "Getting pilot ID";
_state = State::GetPilotID;
......@@ -393,8 +394,9 @@ AirMapFlightPlanManager::_createFlightPlan()
if (!isAlive.lock()) return;
if (_state != State::GetPilotID) return;
if (result) {
_pilotID = QString::fromStdString(result.value().id);
qCDebug(AirMapManagerLog) << "Got Pilot ID:"<<_pilotID;
QString pilotID = QString::fromStdString(result.value().id);
_shared.setPilotID(pilotID);
qCDebug(AirMapManagerLog) << "Got Pilot ID:" << pilotID;
_state = State::Idle;
_uploadFlightPlan();
} else {
......@@ -513,7 +515,7 @@ AirMapFlightPlanManager::_uploadFlightPlan()
params.buffer = 10.f;
params.latitude = static_cast<float>(_flight.takeoffCoord.latitude());
params.longitude = static_cast<float>(_flight.takeoffCoord.longitude());
params.pilot.id = _pilotID.toStdString();
params.pilot.id = _shared.pilotID().toStdString();
//-- Handle flight start/end
_updateFlightStartEndTime(params.start_time, params.end_time);
//-- Rules & Features
......@@ -838,7 +840,7 @@ AirMapFlightPlanManager::loadFlightList(QDateTime startTime, QDateTime endTime)
_rangeStart = startTime;
_rangeEnd = endTime;
qCDebug(AirMapManagerLog) << "List flights from:" << _rangeStart.toString("yyyy MM dd - hh:mm:ss") << "to" << _rangeEnd.toString("yyyy MM dd - hh:mm:ss");
if (_pilotID == "") {
if (_shared.pilotID().isEmpty()) {
//-- Need to get the pilot id
qCDebug(AirMapManagerLog) << "Getting pilot ID";
_state = State::GetPilotID;
......@@ -851,8 +853,9 @@ AirMapFlightPlanManager::loadFlightList(QDateTime startTime, QDateTime endTime)
if (!isAlive.lock()) return;
if (_state != State::GetPilotID) return;
if (result) {
_pilotID = QString::fromStdString(result.value().id);
qCDebug(AirMapManagerLog) << "Got Pilot ID:"<<_pilotID;
QString pilotID = QString::fromStdString(result.value().id);
_shared.setPilotID(pilotID);
qCDebug(AirMapManagerLog) << "Got Pilot ID:" << pilotID;
_state = State::Idle;
_loadFlightList();
} else {
......@@ -893,7 +896,7 @@ AirMapFlightPlanManager::_loadFlightList()
params.start_after = airmap::from_milliseconds_since_epoch(airmap::milliseconds(static_cast<long long>(start)));
params.start_before = airmap::from_milliseconds_since_epoch(airmap::milliseconds(static_cast<long long>(end)));
params.limit = 250;
params.pilot_id = _pilotID.toStdString();
params.pilot_id = _shared.pilotID().toStdString();
_shared.client()->flights().search(params, [this, isAlive](const Flights::Search::Result& result) {
if (!isAlive.lock()) return;
if (_state != State::LoadFlightList) return;
......
......@@ -152,7 +152,6 @@ private:
AirMapSharedState& _shared;
QTimer _pollTimer; ///< timer to poll for approval check
QString _flightId; ///< Current flight ID, not necessarily accepted yet
QString _pilotID; ///< Pilot ID in the form "auth0|abc123"
QString _flightToEnd;
PlanMasterController* _planController = nullptr;
bool _valid = false;
......
......@@ -11,6 +11,7 @@
#include "AirMapManager.h"
#include "airmap/authenticator.h"
#include "qjsonwebtoken.h"
using namespace airmap;
......@@ -32,6 +33,11 @@ AirMapSharedState::doRequestWithLogin(const Callback& callback)
}
}
//-- TODO:
// For now, only anonymous login collects the (anonymous) pilot ID within login()
// For autheticated logins, we need to collect it here as opposed to spread all over
// the place as it is the case now.
void
AirMapSharedState::login()
{
......@@ -52,6 +58,11 @@ AirMapSharedState::login()
qCDebug(AirMapManagerLog) << "Successfully authenticated with AirMap: id="<< result.value().id.c_str();
emit authStatus(AirspaceManager::AuthStatus::Anonymous);
_loginToken = QString::fromStdString(result.value().id);
QJsonWebToken token = QJsonWebToken::fromTokenAndSecret(_loginToken, QString());
QJsonDocument doc = token.getPayloadJDoc();
QJsonObject json = doc.object();
_pilotID = json.value("sub").toString();
qCDebug(AirMapManagerLog) << "Anonymous pilot id:" << _pilotID;
_processPendingRequests();
} else {
_pendingRequests.clear();
......@@ -105,9 +116,9 @@ AirMapSharedState::logout()
if (!isLoggedIn()) {
return;
}
_loginToken = "";
_pilotID.clear();
_loginToken.clear();
_pendingRequests.clear();
}
......@@ -37,6 +37,9 @@ public:
const Settings& settings () const { return _settings; }
void setClient (airmap::qt::Client* client) { _client = client; }
QString pilotID () { return _pilotID; }
void setPilotID (const QString& pilotID) { _pilotID = pilotID; }
/**
* Get the current client instance. It can be NULL. If not NULL, it implies
* there's an API key set.
......@@ -66,6 +69,7 @@ private:
private:
bool _isLoginInProgress = false;
QString _loginToken; ///< login token: empty when not logged in
QString _pilotID;
airmap::qt::Client* _client = nullptr;
Settings _settings;
QQueue<Callback> _pendingRequests; ///< pending requests that are processed after a successful login
......
## Introduction
QJsonWebToken : JWT (JSON Web Token) Implementation in Qt C++
This class implements a subset of the [JSON Web Token](https://en.wikipedia.org/wiki/JSON_Web_Token)
open standard [RFC 7519](https://tools.ietf.org/html/rfc7519).
Currently this implementation **only supports** the following algorithms:
Alg | Parameter Value Algorithm
----- | ------------------------------------
HS256 | HMAC using SHA-256 hash algorithm
HS384 | HMAC using SHA-384 hash algorithm
HS512 | HMAC using SHA-512 hash algorithm
### Include
In order to include this class in your project, in the qt project **.pro** file add the lines:
```cmake
HEADERS += ./src/qjsonwebtoken.h
SOURCES += ./src/qjsonwebtoken.cpp
```
### Usage
The repository of this project includes examples that demonstrate the use of this class:
* ```./examples/jwtcreator/``` : Example that shows how to create a JWT with your custom *payload*.
* ```./examples/jwtverifier/``` : Example that shows how to validate a JWT with a given *secret*.
### Limitations
Currently, `QJsonWebToken` validator, can **only** validate tokens created by `QJsonWebToken` itself. This limitation is due to the usage of Qt's [QJsonDocument API](http://doc.qt.io/qt-5/qjsondocument.html), see [this issue for further explanation](https://github.com/juangburgos/QJsonWebToken/issues/3#issuecomment-333056575).
### License
MIT
```
The MIT License(MIT)
Copyright(c) <2016> <Juan Gonzalez Burgos>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
This diff is collapsed.
This diff is collapsed.
CONFIG -= flat
INCLUDEPATH += $$PWD/
SOURCES += $$PWD/qjsonwebtoken.cpp
HEADERS += $$PWD/qjsonwebtoken.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment