blob: 65863032f7f68ac5ea24452cdf6c9de4c340e6fe (
plain)
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
|
# Generates and decodes JSON Web Tokens for Authenticating users.
class UserToken
Habitat.create { setting stubbed_token : String? }
ALGORITHM = JWT::Algorithm::HS256
def self.generate(user : User) : String
payload = {"user_id" => user.id}
settings.stubbed_token || create_token(payload)
end
def self.create_token(payload)
JWT.encode(payload, Lucky::Server.settings.secret_key_base, ALGORITHM)
end
def self.decode_user_id(token : String) : Int64?
payload, _header = JWT.decode(token, Lucky::Server.settings.secret_key_base, ALGORITHM)
payload["user_id"].to_s.to_i64
rescue e : JWT::Error
Lucky::Log.dexter.error { {jwt_decode_error: e.message} }
nil
end
# Used in tests to return a fake token to test against.
def self.stub_token(token : String, &)
temp_config(stubbed_token: token) do
yield
end
end
end
|