imap.rb/lib/imap_server/client_handler/authenticate.rb
2022-12-10 01:31:15 +01:00

27 lines
866 B
Ruby

class ImapServer
class ClientHandler
module Authenticate
def handle_authenticate(msg:, user:)
if msg.args.first == "PLAIN"
handle_authenticate_plain(msg: msg)
else
msg.respond ServerMessage.new("NO invalid #{msg.args.first} arg, allowed PLAIN only", tag: msg.tag)
end
end
private
def handle_authenticate_plain(msg:)
msg.respond ServerMessage.new("+", tag: nil)
payload = msg.next(parse: false)
_, username, password = payload.unpack("m").first.split("\x00")
if msg.handler.authenticate_user!(username:, password:)
msg.respond ServerMessage.new("OK connection succeed for #{username}", tag: msg.tag)
else
msg.respond ServerMessage.new("BAD connection failure for #{username}", tag: msg.tag)
end
end
end
end
end