2018-09-22 21:25:03 +02:00
|
|
|
|
|
|
|
require "jwt"
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
require "ipc"
|
2018-09-22 21:42:21 +02:00
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
require "./user.cr"
|
2018-12-17 00:56:03 +01:00
|
|
|
require "./group.cr"
|
2018-09-22 21:25:03 +02:00
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
module AuthD
|
|
|
|
enum RequestTypes
|
|
|
|
GET_TOKEN
|
|
|
|
end
|
2018-09-22 21:25:03 +02:00
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
enum ResponseTypes
|
|
|
|
OK
|
|
|
|
MALFORMED_REQUEST
|
|
|
|
INVALID_CREDENTIALS
|
|
|
|
end
|
2018-09-22 21:25:03 +02:00
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
class GetTokenRequest
|
|
|
|
JSON.mapping({
|
2018-12-17 00:56:03 +01:00
|
|
|
# FIXME: Rename to "login" for consistency.
|
2018-11-12 18:51:21 +01:00
|
|
|
username: String,
|
|
|
|
password: String
|
|
|
|
})
|
2018-09-22 21:25:03 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
class Client < IPC::Client
|
|
|
|
property key : String
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@key = ""
|
|
|
|
|
|
|
|
initialize "auth"
|
2018-09-22 21:25:03 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
def get_token?(username : String, password : String)
|
|
|
|
send RequestTypes::GET_TOKEN.value.to_u8, {
|
|
|
|
:username => username,
|
|
|
|
:password => password
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
response = read
|
|
|
|
|
|
|
|
if response.type == ResponseTypes::OK.value.to_u8
|
|
|
|
response.payload
|
|
|
|
else
|
|
|
|
nil
|
2018-09-22 21:25:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:51:21 +01:00
|
|
|
def decode_token(token)
|
|
|
|
user, meta = JWT.decode token, @key, "HS256"
|
|
|
|
|
|
|
|
user = AuthD::User.from_json user.to_json
|
|
|
|
|
|
|
|
{user, meta}
|
|
|
|
end
|
2018-09-22 21:25:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|