easyssl 0.50.142aaef
EasySSL is base back end library for your c++ Qt projects.
asynckeysauth.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2025 QuasarApp.
3 * Distributed under the GPLv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#ifndef ASYNCKEYSAUTH_H
9#define ASYNCKEYSAUTH_H
10
11#include <QCryptographicHash>
12#include <time.h>
13#include <QString>
14#include <easyssl/icrypto.h>
15
16
17namespace EasySSL {
18
68template<class CryptoImplementation>
69class AsyncKeysAuth: public CryptoImplementation
70{
71public:
73 //The CryptoImplementation class must be support ICrypto::Features::Signing feature
74 assert(this->supportedFeatures() & ICrypto::Features::Signing);
75 }
76
78
79 }
80
86 bool auth(int allowedTimeRangeSec, QString* retLoginedUserId) const {
87
88 int diff = time(0) - _unixTime;
89
90 if (diff < 0) {
91 return false;
92 }
93
94 if (diff >= allowedTimeRangeSec) {
95 return false;
96 }
97
98 QByteArray data = _publicKey;
99 data.insert(0, reinterpret_cast<const char*>(&_unixTime),
100 sizeof(_unixTime));
101
102 bool result = checkSign(data, _signature, _publicKey);
103
104 if (result && retLoginedUserId) {
105 *retLoginedUserId = getUserId();
106 }
107
108 return result;
109 }
110
115 bool prepare() {
116 _unixTime = time(0);
117
118 QByteArray data = _publicKey;
119 data.insert(0, reinterpret_cast<const char*>(&_unixTime),
120 sizeof(_unixTime));
121
123
124 return isValid();
125 }
126
132 unsigned int unixTime() const {
133 return _unixTime;
134 }
135
140 void setUnixTime(unsigned int newUnixTime) {
141 _unixTime = newUnixTime;
142 }
143
149 const QByteArray &signature() const {
150 return _signature;
151 }
152
158 bool isValid() const {
159 return _publicKey.size() && _signature.size() && _unixTime;
160 }
161
167 QString getUserId() const {
168 return QCryptographicHash::hash(_publicKey,
169 QCryptographicHash::Sha256).
170 toBase64(QByteArray::Base64UrlEncoding);
171 }
172
173
180 const QByteArray &publicKey() const {
181 return _publicKey;
182 }
183
189 void setPublicKey(const QByteArray &newPublicKey) {
190 _publicKey = newPublicKey;
191 }
192
193protected:
194
195 QByteArray decrypt(const QByteArray &message, const QByteArray &key) override {
196 return CryptoImplementation::decrypt(message, key);
197 };
198
199 QByteArray encrypt(const QByteArray &message, const QByteArray &key) override {
200 return CryptoImplementation::encrypt(message, key);
201 };
202
203 QByteArray signMessage(const QByteArray &message, const QByteArray &key) const override {
204 return CryptoImplementation::signMessage(message, key);
205 };
206
207 bool checkSign(const QByteArray &message, const QByteArray &signature, const QByteArray &key) const override {
208 return CryptoImplementation::checkSign(message, signature, key);
209 };
210
215 virtual QByteArray getPrivateKey() const = 0;
216
222 void setSignature(const QByteArray &newSignature) {
223 _signature = newSignature;
224 }
225
226 unsigned int _unixTime = 0;
227 QByteArray _signature;
228 QByteArray _publicKey;
229};
230
231}
232
233#endif // ASYNCKEYSAUTH_H
The AsyncKeysAuth class is temaplate class for works with authorization of a pair of asynchronous key...
virtual QByteArray getPrivateKey() const =0
getPrivateKey This method should be return private key for the public key that saved in this object.
void setSignature(const QByteArray &newSignature)
setSignature Tihis is internal method for sets new signature value.
QByteArray encrypt(const QByteArray &message, const QByteArray &key) override
bool auth(int allowedTimeRangeSec, QString *retLoginedUserId) const
auth This method make authentication and return true if the authentication finished successful else f...
QByteArray decrypt(const QByteArray &message, const QByteArray &key) override
bool checkSign(const QByteArray &message, const QByteArray &signature, const QByteArray &key) const override
void setPublicKey(const QByteArray &newPublicKey)
setPublicKey This method sets new public key for authentication.
void setUnixTime(unsigned int newUnixTime)
setUnixTime This method sets new value of the unixTime propertye.
QByteArray signMessage(const QByteArray &message, const QByteArray &key) const override
unsigned int unixTime() const
unixTime This method return unix time that client added for authentication.
bool isValid() const
isValid this method check this ibject to valid.
const QByteArray & signature() const
signature This method return signature array.
const QByteArray & publicKey() const
publicKey This method return public key that client added for authentication.
bool prepare()
prepare This method will generate signature for autentication of client. Please inboke this method be...
QString getUserId() const
getUserId This method return user id that generated from the public key.
@ Signing
Signin and check sign of the data.
Definition icrypto.h:31