mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 17:56:21 +00:00
57 lines
1.4 KiB
Ruby
57 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'net/imap'
|
|
require 'warden'
|
|
require 'email_address'
|
|
|
|
# XXX Separación de concerns?
|
|
require_relative '../sutty/models/usuaria'
|
|
|
|
module Warden
|
|
module IMAP
|
|
# Una estrategia de autenticación por IMAP
|
|
class Strategy < Warden::Strategies::Base
|
|
def valid?
|
|
return false unless params.include? 'username'
|
|
return false unless params.include? 'password'
|
|
|
|
@email = EmailAddress.new(params['username'])
|
|
|
|
return false unless @email.valid?
|
|
|
|
true
|
|
end
|
|
|
|
def authenticate!
|
|
imap_connect
|
|
imap_login
|
|
end
|
|
|
|
private
|
|
|
|
def imap_connect
|
|
# No vamos a enviar la contraseña en texto plano a ningún lado
|
|
@imap = Net::IMAP.new(@email.host_name, ssl: true)
|
|
# Errores más comunes según
|
|
# https://ruby-doc.org/stdlib-2.0.0/libdoc/net/imap/rdoc/Net/IMAP.html
|
|
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ENETUNREACH,
|
|
SocketError, Net::IMAP::ByeResponseError => e
|
|
|
|
@imap.disconnect
|
|
fail! e.to_s
|
|
end
|
|
|
|
def imap_login
|
|
@imap.login(@email.normal, params['password'])
|
|
@imap.disconnect
|
|
|
|
success! Sutty::Usuaria.find(@email.normal)
|
|
rescue EOFError => e
|
|
@imap.disconnect
|
|
fail! e.to_s
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
Warden::Strategies.add(:imap, Warden::IMAP::Strategy)
|