Torc  0.1
torcwebsockettoken.cpp
Go to the documentation of this file.
1 /* Class TorcWebSocketToken
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2018
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QUuid>
25 #include <QMutex>
26 
27 // Torc
28 #include "torclogging.h"
29 #include "torccoreutils.h"
30 #include "torcwebsockettoken.h"
31 
34 {
35  public:
37  : m_timeStamp(0),
38  m_host()
39  {
40  }
41  WebSocketAuthentication(quint64 Timestamp, const QString &Host)
42  : m_timeStamp(Timestamp),
43  m_host(Host)
44  {
45  }
46 
47  quint64 m_timeStamp;
48  QString m_host;
49 };
50 
53 QString TorcWebSocketToken::GetWebSocketToken(const QString &Host, const QString &Current)
54 {
55  static QMutex lock(QMutex::Recursive);
56  static QMap<QString,WebSocketAuthentication> tokens;
57 
58  QMutexLocker locker(&lock);
59 
60  // always expire old tokens
61  quint64 tooold = TorcCoreUtils::GetMicrosecondCount() - 10000000;
62  QStringList old;
63  QMap<QString,WebSocketAuthentication>::iterator it = tokens.begin();
64  for ( ; it != tokens.end(); ++it)
65  if (it.value().m_timeStamp < tooold)
66  old.append(it.key());
67 
68  foreach (const QString &expire, old)
69  tokens.remove(expire);
70 
71  // if a current token is supplied, validate it
72  if (!Current.isEmpty())
73  {
74  if (tokens.contains(Current))
75  {
76  // validate the host
77  if (tokens.value(Current).m_host == Host)
78  {
79  tokens.remove(Current);
80  return Current;
81  }
82  tokens.remove(Current);
83  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Host mismatch for websocket authentication"));
84  }
85  return QStringLiteral("ErroR");
86  }
87 
88  // create new
89  QString uuid = QUuid::createUuid().toString().mid(1, 36);
90  tokens.insert(uuid, WebSocketAuthentication(TorcCoreUtils::GetMicrosecondCount(), Host));
91  return uuid;
92 }
quint64 GetMicrosecondCount(void)
Get the current system clock time in microseconds.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
A simple container for authenticaion tokens.
WebSocketAuthentication(quint64 Timestamp, const QString &Host)
static QString GetWebSocketToken(const QString &Host, const QString &Current=QString())
Retrieve an authentication token for the given request or validate a current token.