eteos-client 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/eteos-client.rb +157 -48
  2. metadata +5 -5
@@ -1,48 +1,157 @@
1
- # eteos-client.rb
2
- # eteos client for rails
3
- # v0.2.0
4
- #
5
- # David Adams
6
- # March 10, 2005
7
- #
8
-
9
- require 'uri'
10
- require 'net/http'
11
-
12
- module EteosClient
13
- EteosLoginURL = "http://www.eteos.com/login?url="
14
- EteosServer = "www.eteos.com"
15
- EteosTicketURI = "/ticket/"
16
-
17
- # authenticate takes session hash and updates it
18
- def authenticate
19
- # check to see that EteosClientURL is set
20
- if EteosClientURL.nil? or EteosClientURL.empty?
21
- render_text "Error: you must specify EteosClientURL " +
22
- "in your class definition " +
23
- "before calling authenticate."
24
-
25
- # if eteosUsername is defined, do nothing
26
- unless @session["eteosUsername"]
27
- unless @params["eteosTicket"]
28
- # TODO: URI.escape doesn't sufficiently escape URLs
29
- redirect_to EteosLoginURL + URI.escape(EteosClientURL + @request.request_uri)
30
- else
31
- # we have a ticket, we must check it
32
- Net::HTTP.start(EteosServer, 80) do |http|
33
- response = http.get("#{EteosTicketURI + @params['eteosTicket']}")
34
- status,msg = response.body.split(":")
35
- if status == "OK"
36
- @session["eteosUsername"] = msg
37
- return true
38
- else
39
- render_text "Invalid Ticket. Please try again!"
40
- return false
41
- end
42
- end
43
- end
44
- else
45
- return true
46
- end
47
- end
48
- end
1
+ # eteos-client.rb
2
+ # eteos client for rails
3
+ # v0.3.0
4
+ #
5
+ # David Adams
6
+ # March 22, 2005
7
+ #
8
+
9
+ require 'uri'
10
+ require 'net/http'
11
+
12
+ # extend ActionController::Base to provide
13
+ # an authentication framework
14
+ class ActionController::Base
15
+
16
+ private
17
+ # this class variable holds the error handler for the controller
18
+ @@eteos_error_handler = nil
19
+
20
+ # this class variable holds the name of the user Model
21
+ @@eteos_user_model = :User
22
+
23
+ # this class variable holds the name of the field in the
24
+ # user Model where the eteos username is stored
25
+ @@eteos_username_field = "eteos_username"
26
+
27
+ # this class variable holds the name of the field in the
28
+ # user Model where the last visit timestampe is stored
29
+ @@eteos_last_visit_field = "last_visit"
30
+
31
+ protected
32
+ def self.eteos_error_handler(m)
33
+ @@eteos_error_handler = m
34
+ end
35
+
36
+ def self.eteos_user_model(m)
37
+ @@eteos_user_model = m
38
+ end
39
+
40
+ def self.eteos_username_field(f)
41
+ @@eteos_username_field = f
42
+ end
43
+
44
+ def self.eteos_last_visit_field(f)
45
+ @@eteos_last_visit_field = f
46
+ end
47
+
48
+ private
49
+ EteosLoginURL = "http://www.eteos.com/login?url="
50
+ EteosServer = "www.eteos.com"
51
+ EteosTicketURI = "/ticket/"
52
+
53
+ protected
54
+
55
+ # eteos_authenticate takes session hash and updates it
56
+ def eteos_authenticate
57
+ # build request URL
58
+ request_url = @request.ssl? ? "https://" : "http://" + @request.host_with_port
59
+
60
+ # if eteos_username is defined, do nothing
61
+ unless @session["eteos_username"]
62
+ unless @params["eteosTicket"]
63
+ redirect_to EteosLoginURL + URI.escape(request_url + @request.request_uri, '?&+=/[]\% :$')
64
+ else
65
+ # we have a ticket, we must check it
66
+ Net::HTTP.start(EteosServer, 80) do |http|
67
+ response = http.get("#{EteosTicketURI + @params['eteosTicket']}")
68
+ status,msg = response.body.split(":")
69
+ if status == "OK"
70
+ eteos_update_user_info(msg)
71
+ return true
72
+ else
73
+ eteos_error("Invalid ticket.")
74
+ return false
75
+ end
76
+ end
77
+ end
78
+ else
79
+ eteos_update_user_info(@session['eteos_username'])
80
+ return true
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ # handle an error
87
+ def eteos_error(err)
88
+ if @@eteos_error_handler.nil?
89
+ eteos_default_error_handler(err)
90
+ else
91
+ result = case @@eteos_error_handler
92
+ when Symbol
93
+ self.send(@@eteos_error_handler, err)
94
+ when Array
95
+ @session['eteos_error'] = err
96
+ redirect_to(:controller => @@eteos_error_handler[0],
97
+ :action => @@eteos_error_handler[1])
98
+ else
99
+ # use the default handler but return a different error
100
+ eteos_default_error_handler("An error occured, but " +
101
+ "eteos_error_handler was not defined properly!")
102
+ end
103
+ end
104
+ end
105
+
106
+ # default error handler
107
+ def eteos_default_error_handler(err)
108
+ render_text "ETEOS ERROR: <b>#{err}</b>"
109
+ end
110
+
111
+ # check to see if user model and field exist
112
+ def eteos_valid_model?
113
+ # check to see if model exists
114
+ model = @@eteos_user_model.to_s
115
+ begin
116
+ eval "#{model}.new"
117
+ rescue NameError
118
+ return false
119
+ end
120
+
121
+ # check to see if field exists
122
+ field = @@eteos_username_field.to_s
123
+ begin
124
+ field_exists = eval "#{model}.new.attributes.include?('#{field}')"
125
+ return field_exists ? true : false
126
+
127
+ rescue NameError
128
+ return false
129
+ end
130
+ end
131
+
132
+ def eteos_update_user_info(username)
133
+ @session["eteos_username"] = username
134
+
135
+ if eteos_valid_model?
136
+ model = @@eteos_user_model.to_s
137
+ username_field = @@eteos_username_field.to_s
138
+ last_visit_field = @@eteos_last_visit_field.to_s
139
+ has_changed = false
140
+
141
+ eval <<-END_OF_EVAL
142
+ user_object = #{model}.find_by_#{username_field}("#{username}")
143
+ if user_object.nil?
144
+ user_object = #{model}.new(:#{username_field} => "#{username}")
145
+ has_changed = true
146
+ end
147
+ if user_object.attributes.include? "#{last_visit_field}"
148
+ user_object.#{last_visit_field} = Time.now
149
+ has_changed = true
150
+ end
151
+
152
+ user_object.save if has_changed
153
+ END_OF_EVAL
154
+ end
155
+ end
156
+
157
+ end
metadata CHANGED
@@ -3,16 +3,16 @@ rubygems_version: 0.8.4
3
3
  specification_version: 1
4
4
  name: eteos-client
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2005-03-10
8
- summary: Eteos Client for Ruby on Rails
6
+ version: 0.3.0
7
+ date: 2005-03-23
8
+ summary: Eteos Client for Rails allows cross-website authentication via the Eteos authentication service (www.eteos.com) in two lines of code for Rails applications. Even integrates with ActiveRecord user models simply and easily.
9
9
  require_paths:
10
10
  - lib
11
11
  email: daveadams@gmail.com
12
- homepage: http://eteos-client.rubyforge.org
12
+ homepage: http://rubyforge.org/projects/eteos-client
13
13
  rubyforge_project: eteos-client
14
14
  description:
15
- autorequire:
15
+ autorequire: eteos-client
16
16
  default_executable:
17
17
  bindir: bin
18
18
  has_rdoc: false