nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
tokens.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_TOKENS_H_
2#define NANOMMO_SERVER_TOKENS_H_
3
4#include <chrono>
5#include <cstdint>
6#include <string>
7
8#include "absl/status/statusor.h"
9#include "server/redis.h"
10#include "server/tracing.h"
11
12namespace nanommo {
13
14/// 32 random bytes, in hex, which nobody can guess or make again: a token, a
15/// session, or a trade's id. Tokens initialises libsodium, which it needs.
16std::string RandomHex();
17
18class Tokens {
19 public:
20 static constexpr std::chrono::seconds kLifetime{30};
21 /// How long a login's session lasts after its last request.
22 static constexpr std::chrono::minutes kSessionLifetime{30};
23
24 explicit Tokens(Redis& redis);
25
26 /// A token to enter a world with, once, as the player with the id: a
27 /// character, from the login server, or one moving to another instance.
28 /// Issued for a traced request, its record carries on the trace.
29 absl::StatusOr<std::string> Issue(uint32_t player_id);
30
31 /// Returns a NotFound error for a token that's unknown, used or expired.
32 /// With an issuer, it's given the span that issued the token, which the
33 /// request redeeming it carries on from, or no span, when that wasn't
34 /// traced.
35 absl::StatusOr<uint32_t> Redeem(const std::string& token,
36 SpanContext* issuer = nullptr);
37
38 /// A session for the account, which its next requests to the login server
39 /// go with in place of the name and the password.
40 absl::StatusOr<std::string> Open(uint32_t account_id);
41
42 /// The account the session is for, which keeps it going for another
43 /// kSessionLifetime, or a NotFound error once it's run out.
44 absl::StatusOr<uint32_t> AccountOf(const std::string& session);
45
46 private:
47 Redis& redis_;
48};
49
50} // namespace nanommo
51
52#endif // NANOMMO_SERVER_TOKENS_H_
static constexpr std::chrono::minutes kSessionLifetime
How long a login's session lasts after its last request.
Definition tokens.h:22
absl::StatusOr< std::string > Issue(uint32_t player_id)
Definition tokens.cc:36
static constexpr std::chrono::seconds kLifetime
Definition tokens.h:20
Tokens(Redis &redis)
Definition tokens.cc:32
absl::StatusOr< uint32_t > AccountOf(const std::string &session)
Definition tokens.cc:87
absl::StatusOr< uint32_t > Redeem(const std::string &token, SpanContext *issuer=nullptr)
Definition tokens.cc:54
absl::StatusOr< std::string > Open(uint32_t account_id)
Definition tokens.cc:76
Definition chat.cc:9
std::string RandomHex()
Definition tokens.cc:24