Module: Devise::ImapAdapter

Extended by:
ImapAdapter
Included in:
ImapAdapter
Defined in:
lib/devise_imap_authenticatable/adapter.rb

Overview

Simple adapter for imap credential checking

Instance Method Summary collapse

Instance Method Details

#bin_encode(string) ⇒ Object



35
36
37
38
39
# File 'lib/devise_imap_authenticatable/adapter.rb', line 35

def bin_encode(string)
  encoded = string.dup
  encoded.force_encoding(Encoding::BINARY) if encoded.respond_to?(:force_encoding)
  encoded
end

#new_connection ⇒ Object



23
24
25
26
27
28
29
# File 'lib/devise_imap_authenticatable/adapter.rb', line 23

def new_connection
  if previous_version?
    Net::IMAP.new ::Devise.imap_host, ::Devise.imap_port, ::Devise.imap_ssl
  else
    Net::IMAP.new ::Devise.imap_host, :port => ::Devise.imap_port, :ssl => ::Devise.imap_ssl
  end
end

#previous_version? ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/devise_imap_authenticatable/adapter.rb', line 31

def previous_version?
  Net::IMAP::VERSION < "1.1.0"
end

#valid_credentials?(username, password) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/devise_imap_authenticatable/adapter.rb', line 10

def valid_credentials?(username, password)
  imap = new_connection
  Timeout.timeout(::Devise.imap_timeout) do
    imap. bin_encode(username), bin_encode(password)
  end
  true
rescue Net::IMAP::ResponseError, Timeout::Error, Errno::ETIMEDOUT,
       Errno::ECONNRESET, Errno::EHOSTUNREACH, EOFError, SocketError
  false
ensure
  imap.try(:disconnect)
end