| 58 | } |
| 59 | |
| 60 | void HyperAPI::handleMessage(const QString& messageString, const QString& httpAuthHeader) |
| 61 | { |
| 62 | try |
| 63 | { |
| 64 | if (HyperHdrInstance::isTerminated()) |
| 65 | return; |
| 66 | |
| 67 | const QString ident = "JsonRpc@" + _peerAddress; |
| 68 | QJsonObject message; |
| 69 | // parse the message |
| 70 | if (!JsonUtils::parse(ident, messageString, message, _log)) |
| 71 | { |
| 72 | sendErrorReply("Errors during message parsing, please consult the HyperHDR Log."); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | int tan = 0; |
| 77 | if (message.value("tan") != QJsonValue::Undefined) |
| 78 | tan = message["tan"].toInt(); |
| 79 | |
| 80 | // check basic message |
| 81 | if (!JsonUtils::validate(ident, message, ":schema", _log)) |
| 82 | { |
| 83 | sendErrorReply("Errors during message validation, please consult the HyperHDR Log.", "" /*command*/, tan); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // check specific message |
| 88 | const QString command = message["command"].toString(); |
| 89 | if (!JsonUtils::validate(ident, message, QString(":schema-%1").arg(command), _log)) |
| 90 | { |
| 91 | sendErrorReply("Errors during specific message validation, please consult the HyperHDR Log", command, tan); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // client auth before everything else but not for http |
| 96 | if (!_noListener && command == "authorize") |
| 97 | { |
| 98 | handleAuthorizeCommand(message, command, tan); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // check auth state |
| 103 | if (!BaseAPI::isAuthorized()) |
| 104 | { |
| 105 | bool authOk = false; |
| 106 | |
| 107 | if (_noListener) |
| 108 | { |
| 109 | QString cToken = httpAuthHeader.mid(5).trimmed(); |
| 110 | if (BaseAPI::isTokenAuthorized(cToken)) |
| 111 | authOk = true; |
| 112 | } |
| 113 | |
| 114 | if (!authOk) |
| 115 | { |
| 116 | sendErrorReply("No Authorization", command, tan); |
| 117 | return; |