Files
terminal-web-server/src/auth/jwt.h

42 lines
1.1 KiB
C++

#ifndef JWT_H
#define JWT_H
#include <string>
#include "resources.h"
namespace http::auth::jwt {
extern std::string secretKey;
constexpr const char* EMPTY_AUTH_COOKIE = "auth=;Path=/; Max-Age=86400; HttpOnly; SameSite=Lax";
constexpr int64_t SESSION_LIVE_MS = 24 * 60 * 60 * 1000; // 24 часа
constexpr int64_t SESSION_UPDATE_THRESHOLD = 10 * 60 * 1000; // 10 минут
void generateSecretKey();
/**
* Упрощенная реализация JWT (Json Web Token). Токен имеет вид: `{ username | base64 }.{ signature | base64 }`.
* Сигнатура вычисляется следующим образом: `SHA256(username + secret)`.
* Имя cookie: `auth`.
*/
class Jwt {
std::string payload;
int64_t lastUpdate = 0;
std::string signature;
public:
static Jwt fromCookies(const std::string& cookie);
static Jwt fromUser(const std::string& User);
bool isValid();
std::string getUsername();
std::string asCookie(bool isSecure = false);
bool needUpdate() const;
~Jwt();
};
}
#endif //JWT_H