authd/src/authd/token.cr

30 lines
441 B
Crystal

require "json"
class AuthD::Token
include JSON::Serializable
property login : String
property uid : UInt32
def initialize(@login, @uid)
end
def to_h
{
:login => login,
:uid => uid
}
end
def to_s(key)
JWT.encode to_h, key, JWT::Algorithm::HS256
end
def self.from_s(key, str)
payload, meta = JWT.decode str, key, JWT::Algorithm::HS256
self.new payload["login"].as_s, payload["uid"].as_i64.to_u32
end
end