Crystal bindings: authc kinda works.
parent
7a1e66423f
commit
f79938f441
|
@ -1,12 +1,25 @@
|
||||||
require "../src/json"
|
require "../src/json"
|
||||||
|
require "json"
|
||||||
|
|
||||||
module AuthD
|
module AuthD
|
||||||
class Client < IPC
|
class Client < IPC
|
||||||
property key : String
|
property key : String
|
||||||
|
property server_fd : Int32 = -1
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@key = ""
|
@key = ""
|
||||||
|
fd = self.connect "auth"
|
||||||
|
if fd.nil?
|
||||||
|
raise "couldn't connect to 'auth' IPC service"
|
||||||
|
end
|
||||||
|
@server_fd = fd
|
||||||
|
end
|
||||||
|
|
||||||
|
def read
|
||||||
|
slice = self.read @server_fd
|
||||||
|
m = IPCMessage::TypedMessage.deserialize slice
|
||||||
|
m.not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_token?(login : String, password : String) : String?
|
def get_token?(login : String, password : String) : String?
|
||||||
|
@ -45,8 +58,14 @@ module AuthD
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_now(msg : IPC::JSON)
|
||||||
|
m = IPCMessage::TypedMessage.new msg.type.to_u8, msg.to_json
|
||||||
|
write @server_fd, m
|
||||||
|
end
|
||||||
|
|
||||||
def send_now(type : Request::Type, payload)
|
def send_now(type : Request::Type, payload)
|
||||||
send_now @server_fd, type.value.to_u8, payload
|
m = IPCMessage::TypedMessage.new type.value.to_u8, payload
|
||||||
|
write @server_fd, m
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_token(token)
|
def decode_token(token)
|
||||||
|
@ -61,7 +80,7 @@ module AuthD
|
||||||
def add_user(login : String, password : String,
|
def add_user(login : String, password : String,
|
||||||
email : String?,
|
email : String?,
|
||||||
phone : String?,
|
phone : String?,
|
||||||
profile : Hash(String, JSON::Any)?) : ::AuthD::User::Public | Exception
|
profile : Hash(String, ::JSON::Any)?) : ::AuthD::User::Public | Exception
|
||||||
|
|
||||||
send_now Request::AddUser.new @key, login, password, email, phone, profile
|
send_now Request::AddUser.new @key, login, password, email, phone, profile
|
||||||
|
|
||||||
|
@ -126,7 +145,7 @@ module AuthD
|
||||||
password : String,
|
password : String,
|
||||||
email : String?,
|
email : String?,
|
||||||
phone : String?,
|
phone : String?,
|
||||||
profile : Hash(String, JSON::Any)?) : ::AuthD::User::Public?
|
profile : Hash(String, ::JSON::Any)?) : ::AuthD::User::Public?
|
||||||
|
|
||||||
send_now Request::Register.new login, password, email, phone, profile
|
send_now Request::Register.new login, password, email, phone, profile
|
||||||
response = AuthD.responses.parse_ipc_json read
|
response = AuthD.responses.parse_ipc_json read
|
||||||
|
|
|
@ -27,6 +27,12 @@ build-authd:
|
||||||
run-authd:
|
run-authd:
|
||||||
LD_LIBRARY_PATH=$(LDPATH) ./bin/authd
|
LD_LIBRARY_PATH=$(LDPATH) ./bin/authd
|
||||||
|
|
||||||
|
build-authc:
|
||||||
|
CRYSTAL_LIBRARY_PATH=$(LDPATH) shards build authc
|
||||||
|
|
||||||
|
run-authc:
|
||||||
|
LD_LIBRARY_PATH=$(LDPATH) ./bin/authc
|
||||||
|
|
||||||
|
|
||||||
run-test:
|
run-test:
|
||||||
crystal run src/libauth.cr
|
crystal run src/libauth.cr
|
||||||
|
|
|
@ -11,6 +11,8 @@ targets:
|
||||||
main: src/pongd.cr
|
main: src/pongd.cr
|
||||||
authd:
|
authd:
|
||||||
main: authd/main.cr
|
main: authd/main.cr
|
||||||
|
authc:
|
||||||
|
main: authd/authc.cr
|
||||||
|
|
||||||
crystal: 1.7.1
|
crystal: 1.7.1
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ class IPC
|
||||||
fd
|
fd
|
||||||
end
|
end
|
||||||
|
|
||||||
def connect(name : String) : Int
|
def connect(name : String) : Int32
|
||||||
fd = uninitialized Int32
|
fd = uninitialized Int32
|
||||||
if LibIPC.connect_service(@context, pointerof(fd), name, name.size) != 0
|
if LibIPC.connect_service(@context, pointerof(fd), name, name.size) != 0
|
||||||
raise "oh noes, 'connect_service' iz brkn"
|
raise "oh noes, 'connect_service' iz brkn"
|
||||||
|
@ -66,6 +66,13 @@ class IPC
|
||||||
self.write(fd, buffer.to_unsafe, buffer.size.to_u64)
|
self.write(fd, buffer.to_unsafe, buffer.size.to_u64)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read(fd : Int32) : Slice(UInt8)
|
||||||
|
buffer : Bytes = Bytes.new 2000000
|
||||||
|
size = buffer.size.to_u64
|
||||||
|
LibIPC.read(@context, fd, buffer.to_unsafe, pointerof(size))
|
||||||
|
buffer[0..size]
|
||||||
|
end
|
||||||
|
|
||||||
def schedule(fd : Int32, string : String)
|
def schedule(fd : Int32, string : String)
|
||||||
self.schedule(fd, string.to_unsafe, string.size.to_u64)
|
self.schedule(fd, string.to_unsafe, string.size.to_u64)
|
||||||
end
|
end
|
||||||
|
@ -96,7 +103,7 @@ class IPC
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def close_all()
|
def close
|
||||||
if LibIPC.close_all(@context) != 0
|
if LibIPC.close_all(@context) != 0
|
||||||
raise "Oh noes, 'close all' iz brkn"
|
raise "Oh noes, 'close all' iz brkn"
|
||||||
end
|
end
|
||||||
|
|
Reference in New Issue