devise_imap_authenticatable 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+
2
+ Copyright (C) 2011 Black Square Media Ltd
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,32 @@
1
+ $: << File.expand_path('../', __FILE__)
2
+
3
+ require 'devise'
4
+ require 'devise_imap_authenticatable/strategy'
5
+ require 'devise_imap_authenticatable/model'
6
+ require 'devise_imap_authenticatable/adapter'
7
+ require 'devise_imap_authenticatable/version'
8
+
9
+ module Devise
10
+
11
+ # IMAP Host address for authentication.
12
+ mattr_accessor :imap_host
13
+ @@imap_host = 'localhost'
14
+
15
+ # IMAP Host address for authentication.
16
+ mattr_accessor :imap_port
17
+ @@imap_port = 143
18
+
19
+ # Use SSL for IMAP for authentication.
20
+ mattr_accessor :imap_ssl
21
+ @@imap_ssl = false
22
+
23
+ mattr_accessor :imap_mechanism
24
+ @@imap_mechanism = "login"
25
+ end
26
+
27
+ # Add imap_authenticatable module
28
+ Devise.add_module :imap_authenticatable,
29
+ :route => :session,
30
+ :strategy => true,
31
+ :controller => :sessions,
32
+ :model => 'devise_imap_authenticatable/model'
@@ -0,0 +1,32 @@
1
+ require 'net/imap'
2
+
3
+ module Devise
4
+
5
+ # Simple adapter for imap credential checking
6
+ module ImapAdapter
7
+ extend self
8
+
9
+ def valid_credentials?(username, password)
10
+ imap = new_connection
11
+ imap.authenticate ::Devise.imap_mechanism, username, password
12
+ true
13
+ rescue Net::IMAP::ResponseError
14
+ false
15
+ ensure
16
+ imap.try(:disconnect)
17
+ end
18
+
19
+ def new_connection
20
+ if previous_version?
21
+ Net::IMAP.new ::Devise.imap_host, ::Devise.imap_port, ::Devise.imap_ssl
22
+ else
23
+ Net::IMAP.new ::Devise.imap_host, :port => ::Devise.imap_port, :ssl => ::Devise.imap_ssl
24
+ end
25
+ end
26
+
27
+ def previous_version?
28
+ Net::IMAP::VERSION < "1.1.0"
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,56 @@
1
+ require 'devise_imap_authenticatable/strategy'
2
+
3
+ module Devise
4
+ module Models
5
+
6
+ # IMAP Authenticatable Module
7
+ #
8
+ # == Examples
9
+ #
10
+ # User.find(1).valid_password?('password123') # returns true/false
11
+ #
12
+ module ImapAuthenticatable
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ attr_accessor :password
17
+ before_validation :downcase_keys
18
+ before_validation :strip_whitespace
19
+ end
20
+
21
+ # Verifies a given password
22
+ def valid_password?(password)
23
+ Devise::ImapAdapter.valid_credentials? send(::Devise.authentication_keys.first), password
24
+ end
25
+
26
+ # Callback, override in your model
27
+ def after_imap_authentication
28
+ end
29
+
30
+ protected
31
+
32
+ # Downcase case-insensitive keys
33
+ def downcase_keys
34
+ (self.class.case_insensitive_keys || []).each { |k| self[k].try(:downcase!) }
35
+ end
36
+
37
+ def strip_whitespace
38
+ (self.class.strip_whitespace_keys || []).each { |k| self[k].try(:strip!) }
39
+ end
40
+
41
+ module ClassMethods
42
+
43
+ # Override in your models if you want to auto-create users
44
+ def build_for_imap_authentication(conditions)
45
+ end
46
+
47
+ # We assume this method already gets a sanitized conditions hash
48
+ def find_for_imap_authentication(conditions)
49
+ find_for_authentication(conditions)
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ require 'devise/strategies/authenticatable'
2
+
3
+ module Devise
4
+ module Strategies
5
+
6
+ # Strategy for signing in a user based on his login and password using IMAP.
7
+ # Redirects to sign_in page if it's not authenticated
8
+ class ImapAuthenticatable < Authenticatable
9
+
10
+ def authenticate!
11
+ resource = valid_password? && (find_resource || build_resource)
12
+
13
+ if validate(resource){ resource.valid_password?(password) }
14
+ resource.after_imap_authentication
15
+ success!(resource)
16
+ elsif !halted?
17
+ fail(:invalid)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def find_resource
24
+ mapping.to.find_for_imap_authentication(authentication_hash)
25
+ end
26
+
27
+ def build_resource
28
+ mapping.to.build_for_imap_authentication(authentication_hash)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module Devise
2
+ module ImapAuthenticatable
3
+ VERSION = "0.6.0" unless const_defined?(:VERSION)
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_imap_authenticatable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
+ platform: ruby
12
+ authors:
13
+ - John Medforth
14
+ - Dimitrij Denissenko
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-07-06 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: devise
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 1
33
+ - 4
34
+ - 0
35
+ version: 1.4.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rails
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 7
47
+ segments:
48
+ - 3
49
+ - 0
50
+ - 0
51
+ version: 3.0.0
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ description: Authenticate users against an IMAP server
55
+ email: dimitrij@blacksquaremedia.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - LICENSE
64
+ - README
65
+ - lib/devise_imap_authenticatable/adapter.rb
66
+ - lib/devise_imap_authenticatable/version.rb
67
+ - lib/devise_imap_authenticatable/strategy.rb
68
+ - lib/devise_imap_authenticatable/model.rb
69
+ - lib/devise_imap_authenticatable.rb
70
+ has_rdoc: true
71
+ homepage: https://github.com/bsm/devise_imap_authenticatable
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 57
85
+ segments:
86
+ - 1
87
+ - 8
88
+ - 7
89
+ version: 1.8.7
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 23
96
+ segments:
97
+ - 1
98
+ - 3
99
+ - 6
100
+ version: 1.3.6
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.6.2
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: IMAP authentication for Devise
108
+ test_files: []
109
+