authlogic_wind 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{authlogic_wind}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["James Stuart"]
@@ -24,7 +24,13 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "authlogic_wind.gemspec",
27
+ "init.rb",
27
28
  "lib/authlogic_wind.rb",
29
+ "lib/authlogic_wind/acts_as_authentic.rb",
30
+ "lib/authlogic_wind/helper.rb",
31
+ "lib/authlogic_wind/session.rb",
32
+ "lib/wind_callback_filter.rb",
33
+ "rails/init.rb",
28
34
  "test/authlogic_wind_test.rb",
29
35
  "test/test_helper.rb"
30
36
  ]
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,84 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+
4
+ # This module is responsible for adding wind functionality to Authlogic. Checkout the README for more info and please
5
+ # see the sub modules for detailed documentation.
6
+ module AuthlogicWind
7
+ # This module is responsible for adding in the wind functionality to your models. It hooks itself into the
8
+ # acts_as_authentic method provided by Authlogic.
9
+ module ActsAsAuthentic
10
+ # Adds in the neccesary modules for acts_as_authentic to include and also disabled password validation if
11
+ # wind is being used.
12
+ def self.included(klass)
13
+ klass.class_eval do
14
+ extend Config
15
+ add_acts_as_authentic_module(Methods, :prepend)
16
+ end
17
+ end
18
+
19
+ module Config
20
+ # The name of the wind login field in the database.
21
+ #
22
+ # * <tt>Default:</tt> :wind_login, :login, or :username, if they exist
23
+ # * <tt>Accepts:</tt> Symbol
24
+ def wind_login_field(value = nil)
25
+ rw_config(:wind_login_field, value, first_column_to_exist(nil, :wind_login, :login, :username))
26
+ end
27
+ alias_method :wind_login_field=, :wind_login_field
28
+
29
+ # Whether or not to validate the wind_login field. If set to false ALL wind validation will need to be
30
+ # handled by you.
31
+ #
32
+ # * <tt>Default:</tt> true
33
+ # * <tt>Accepts:</tt> Boolean
34
+ def validate_wind_login(value = nil)
35
+ rw_config(:validate_wind_login, value, true)
36
+ end
37
+ alias_method :validate_wind_login=, :validate_wind_login
38
+
39
+
40
+ def find_by_wind_login_field(login)
41
+ find(wind_login_field, login)
42
+ end
43
+ end
44
+
45
+ module Methods
46
+ # Set up some simple validations
47
+ def self.included(klass)
48
+ klass.class_eval do
49
+ validate :validate_by_wind, :if => :authenticating_with_wind?
50
+
51
+ validates_uniqueness_of :wind_login, :scope => validations_scope, :if => :using_wind?
52
+ validates_length_of_password_field_options validates_length_of_password_field_options.merge(:if => :validate_password_with_wind?)
53
+ validates_confirmation_of_password_field_options validates_confirmation_of_password_field_options.merge(:if => :validate_password_with_wind?)
54
+ validates_length_of_password_confirmation_field_options validates_length_of_password_confirmation_field_options.merge(:if => :validate_password_with_wind?)
55
+
56
+
57
+ end
58
+ end
59
+
60
+
61
+
62
+ private
63
+
64
+ def authenticating_with_wind?
65
+
66
+ # Controller isn't available in all contexts (e.g. irb)
67
+ return false unless session_class.controller
68
+
69
+ # Initial request when user presses one of the button helpers
70
+ (session_class.controller.params && !session_class.controller.params[:login_with_wind].blank?) ||
71
+ # When the oauth provider responds and we made the initial request
72
+ (wind_response && session_class.controller.session && session_class.controller.session[:wind_request_class] == self.class.name)
73
+ end
74
+
75
+ def validate_password_with_wind?
76
+ !using_wind? && require_password?
77
+ end
78
+
79
+ def using_wind?
80
+ !wind_login.blank?
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,16 @@
1
+ module AuthlogicWind
2
+ module Helper
3
+ def oauth_register_button(options = {})
4
+ oauth_button('register_with_oauth', options)
5
+ end
6
+
7
+ def oauth_login_button(options = {})
8
+ oauth_button('login_with_oauth', options)
9
+ end
10
+
11
+ private
12
+ def oauth_button(name, options = {})
13
+ "<input type='submit' value='#{options[:value]}' name='#{name}' id='user_submit' class='#{options[:class]}'/>"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,185 @@
1
+ module AuthlogicWind
2
+ module Session
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend Config
6
+ include Methods
7
+ end
8
+ end
9
+
10
+ module Config
11
+ # The host of your WIND server.
12
+ #
13
+ # * <tt>Default:</tt> nil
14
+ # * <tt>Accepts:</tt> String
15
+ def wind_host(value = nil)
16
+ rw_config(:wind_host, value)
17
+ end
18
+ alias_method :wind_host=, :wind_host
19
+
20
+ # The service name of your WIND server.
21
+ #
22
+ # * <tt>Default:</tt> nil
23
+ # * <tt>Accepts:</tt> String
24
+ def wind_service(value = nil)
25
+ rw_config(:wind_service, value)
26
+ end
27
+ alias_method :wind_service=, :wind_service
28
+
29
+
30
+ def find_by_wind_method(value = nil)
31
+ rw_config(:find_by_wind_method, value, :find_by_wind_login)
32
+ end
33
+ alias_method :find_by_wind_method=, :find_by_wind_method
34
+
35
+
36
+ # Add this in your Session object to Auto Register a new user using openid via sreg
37
+ def auto_register(value=nil)
38
+ rw_config(:auto_register,value,false)
39
+ end
40
+
41
+ alias_method :auto_register=,:auto_register
42
+
43
+ end
44
+
45
+
46
+ module Methods
47
+ def self.included(klass)
48
+ klass.class_eval do
49
+ validate :validate_by_wind, :if => :authenticating_with_wind?
50
+ end
51
+ end
52
+
53
+ def credentials=(value)
54
+ super
55
+ values = value.is_a?(Array) ? value : [value]
56
+ hash = values.first.is_a?(Hash) ? values.first.with_indifferent_access : nil
57
+ self.record = hash[:priority_record] if !hash.nil? && hash.key?(:priority_record)
58
+ end
59
+
60
+ def save(&block)
61
+ block = nil if redirecting_to_wind_server?
62
+ super(&block)
63
+ end
64
+
65
+
66
+ private
67
+ def authenticating_with_wind?
68
+ # Initial request when user presses one of the button helpers
69
+ (controller.params && !controller.params[:login_with_wind].blank?) ||
70
+ # When the oauth provider responds and we made the initial request
71
+ (wind_response && controller.session && controller.session[:wind_request_class] == self.class.name)
72
+ end
73
+
74
+ def using_wind?
75
+ respond_to(:wind_login) && !wind_login.blank?
76
+ end
77
+
78
+ def authenticate_with_wind
79
+
80
+ if @record
81
+ self.attempted_record = record
82
+
83
+ if !attempted_record
84
+ errors.add_to_base("Could not find user in our database.")
85
+ end
86
+
87
+ else
88
+ uni = generate_verified_login
89
+ if uni
90
+ self.attempted_record = search_for_record(find_by_wind_method, uni)
91
+ if !attempted_record
92
+ if auto_register?
93
+ self.attempted_record = klass.new(:login => uni, :wind_login => uni)
94
+ self.attempted_record.reset_persistence_token
95
+ else
96
+ errors.add_to_base("Could not find UNI #{uni} in our database")
97
+ end
98
+ end
99
+ else
100
+ errors.add_to_base("WIND Ticket did not verify properly.")
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ def wind_host
107
+ self.class.wind_host
108
+ end
109
+
110
+ def wind_service
111
+ self.class.wind_service
112
+ end
113
+
114
+ def find_by_wind_method
115
+ self.class.find_by_wind_method
116
+ end
117
+
118
+ def auto_register?
119
+ self.class.auto_register == true
120
+ end
121
+
122
+ def validate_by_wind
123
+ validate_email_field = false
124
+ if wind_response.blank?
125
+ redirect_to_wind
126
+ else
127
+ authenticate_with_wind
128
+ end
129
+ end
130
+
131
+
132
+ def redirecting_to_wind_server?
133
+ authenticating_with_wind? && wind_response.blank?
134
+ end
135
+
136
+ def redirect_to_wind
137
+ # Store the class which is redirecting, so we can ensure other classes
138
+ # don't get confused and attempt to use the response
139
+ wind_controller.session[:wind_request_class] = self.class.name
140
+
141
+ # Tell our rack callback filter what method the current request is using
142
+ wind_controller.session[:wind_callback_method] = wind_controller.request.method
143
+
144
+ wind_controller.redirect_to wind_controller.url_for(:host => wind_host, :controller => "login", :protocol => "https", :service => wind_service, :destination => build_callback_url)
145
+ end
146
+
147
+ def build_callback_url
148
+ wind_controller.url_for :controller => wind_controller.controller_name, :action => wind_controller.action_name
149
+ end
150
+
151
+ def generate_verified_login
152
+ if (ticketid = wind_controller.params[:ticketid])
153
+ url = "/validate?ticketid=#{ticketid}"
154
+ h = Net::HTTP.new("wind.columbia.edu", 443)
155
+ h.use_ssl = true
156
+ resp, data = h.get(url, nil)
157
+ uni = data.split[1] unless data[0,2] == "no"
158
+ return uni
159
+ else
160
+ nil
161
+ end
162
+ end
163
+
164
+ def wind_response
165
+ wind_controller.params && wind_controller.params[:ticketid]
166
+ end
167
+
168
+ def wind_controller
169
+ is_auth_session? ? controller : session_class.controller
170
+ end
171
+
172
+ def wind
173
+ is_auth_session? ? self.class.wind_consumer : session_class.wind_consumer
174
+ end
175
+
176
+ def is_auth_session?
177
+ self.is_a?(Authlogic::Session::Base)
178
+ end
179
+
180
+
181
+ end
182
+
183
+ end
184
+
185
+ end
@@ -1,6 +1,6 @@
1
- require File.dirname(__FILE__) + "/authlogic_wind/acts_as_authentic"
2
- require File.dirname(__FILE__) + "/authlogic_wind/session"
3
- require File.dirname(__FILE__) + "/authlogic_wind/helper"
1
+ require "authlogic_wind/acts_as_authentic"
2
+ require "authlogic_wind/session"
3
+ require "authlogic_wind/helper"
4
4
 
5
5
  ActiveRecord::Base.send(:include, AuthlogicWind::ActsAsAuthentic)
6
6
  Authlogic::Session::Base.send(:include, AuthlogicWind::Session)
@@ -0,0 +1,13 @@
1
+ class WindCallbackFilter
2
+ def initialize(app)
3
+ @app = app
4
+ end
5
+
6
+ def call(env)
7
+ unless env["rack.session"][:wind_callback_method].blank?
8
+ # env["QUERY_STRING"].gsub!(/ticketid\=/,"user_session[ticketid]=")
9
+ env["REQUEST_METHOD"] = env["rack.session"].delete(:wind_callback_method).to_s.upcase
10
+ end
11
+ @app.call(env)
12
+ end
13
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "authlogic_wind"
2
+ require "wind_callback_filter"
3
+
4
+ # Throw callback rack app into the middleware stack
5
+ ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
6
+ ActionController::Dispatcher.middleware.each do |klass|
7
+ m.use klass
8
+ end
9
+ m.use WindCallbackFilter
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authlogic_wind
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stuart
@@ -49,7 +49,13 @@ files:
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - authlogic_wind.gemspec
52
+ - init.rb
52
53
  - lib/authlogic_wind.rb
54
+ - lib/authlogic_wind/acts_as_authentic.rb
55
+ - lib/authlogic_wind/helper.rb
56
+ - lib/authlogic_wind/session.rb
57
+ - lib/wind_callback_filter.rb
58
+ - rails/init.rb
53
59
  - test/authlogic_wind_test.rb
54
60
  - test/test_helper.rb
55
61
  has_rdoc: true