-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.cpp
More file actions
138 lines (114 loc) · 2.45 KB
/
Copy pathToken.cpp
File metadata and controls
138 lines (114 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
*
* @file Token.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira.
* All rights reserved.
* https://github.com/rixcpp/auth
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Rix
*
*/
#include <rix/auth/Token.hpp>
#include <utility>
namespace rixlib::auth
{
Token::Token(std::string value,
std::string user_id,
std::int64_t issued_at,
std::int64_t expires_at)
: value_(std::move(value)),
user_id_(std::move(user_id)),
issued_at_(issued_at),
expires_at_(expires_at)
{
}
const std::string &Token::value() const noexcept
{
return value_;
}
void Token::set_value(std::string value)
{
value_ = std::move(value);
}
const std::string &Token::user_id() const noexcept
{
return user_id_;
}
void Token::set_user_id(std::string value)
{
user_id_ = std::move(value);
}
const std::string &Token::issuer() const noexcept
{
return issuer_;
}
void Token::set_issuer(std::string value)
{
issuer_ = std::move(value);
}
std::int64_t Token::issued_at() const noexcept
{
return issued_at_;
}
void Token::set_issued_at(std::int64_t value) noexcept
{
issued_at_ = value;
}
std::int64_t Token::expires_at() const noexcept
{
return expires_at_;
}
void Token::set_expires_at(std::int64_t value) noexcept
{
expires_at_ = value;
}
bool Token::revoked() const noexcept
{
return revoked_;
}
void Token::set_revoked(bool value) noexcept
{
revoked_ = value;
}
bool Token::valid() const noexcept
{
return !value_.empty() && !user_id_.empty();
}
bool Token::has_value() const noexcept
{
return !value_.empty();
}
bool Token::has_user_id() const noexcept
{
return !user_id_.empty();
}
bool Token::belongs_to(std::string_view value) const noexcept
{
return user_id_ == value;
}
bool Token::matches(std::string_view value) const noexcept
{
return value_ == value;
}
bool Token::issued_by(std::string_view value) const noexcept
{
return issuer_ == value;
}
bool Token::expired(std::int64_t now) const noexcept
{
return expires_at_ > 0 && now >= expires_at_;
}
bool Token::usable(std::int64_t now) const noexcept
{
return valid() && !revoked_ && !expired(now);
}
void Token::revoke() noexcept
{
revoked_ = true;
}
} // namespace rixlib::auth