Class: EasyRackOpenIDProcessing

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_rack_open_id_processing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ EasyRackOpenIDProcessing

Returns a new instance of EasyRackOpenIDProcessing.



5
6
7
8
# File 'lib/easy_rack_open_id_processing.rb', line 5

def initialize(app, options ={})
  @app = app
  @options = options
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



3
4
5
# File 'lib/easy_rack_open_id_processing.rb', line 3

def env
  @env
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/easy_rack_open_id_processing.rb', line 3

def options
  @options
end

Instance Method Details

#after_logout_pathObject



128
129
130
# File 'lib/easy_rack_open_id_processing.rb', line 128

def after_logout_path
  options[:after_logout_path]
end

#allowed?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/easy_rack_open_id_processing.rb', line 99

def allowed?
  if allowed_identifiers
    allowed_identifiers.include? verified_identifier
  elsif identity_match
    identity_match === verified_identifier
  else
    verified_identifier
  end
end

#allowed_identifiersObject



113
114
115
# File 'lib/easy_rack_open_id_processing.rb', line 113

def allowed_identifiers
  options[:allowed_identifiers]
end

#asset?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/easy_rack_open_id_processing.rb', line 28

def asset?
  0 == path.index(asset_prefix)
end

#asset_prefixObject



32
33
34
# File 'lib/easy_rack_open_id_processing.rb', line 32

def asset_prefix
  '/easy-rack-openid-assets'
end

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/easy_rack_open_id_processing.rb', line 10

def call(env)
  @env = env
  if logout_path == path
    logout_result = logout
    return logout_result if logout_result
  end
  if asset?
    content_type_lookup = {'css' => 'text/css','html'=> 'text/html','js'=>'text/javascript','gif'=>'image/gif','ico' => 'image/vnd.microsoft.icon', 'png'=> 'image/png'}
    ok(IO.read(gem_public_path + path), content_type_lookup[File.extname(path)[1..-1]])
  elsif allowed?
    # pass through
    @app.call(env)
  else
    # break chain, start open_id_login
    
  end
end

#default_return_toObject



185
186
187
# File 'lib/easy_rack_open_id_processing.rb', line 185

def default_return_to
  options[:default_return_to] || '/'
end

#forward_to(url) ⇒ Object



95
96
97
# File 'lib/easy_rack_open_id_processing.rb', line 95

def forward_to(url)
  [302, {'Location' => url,'Content-Type' => 'text/html'}, ["Forwarding to #{url}"]]
end

#gem_public_pathObject



36
37
38
# File 'lib/easy_rack_open_id_processing.rb', line 36

def gem_public_path
  File.dirname(__FILE__) + '/../public/'
end

#identitifier_to_verifyObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/easy_rack_open_id_processing.rb', line 136

def identitifier_to_verify
  @identitifier_to_verify ||=
  if env["rack.request.query_hash"] && env["rack.request.query_hash"]["openid_identifier"]
    env["rack.request.query_hash"]["openid_identifier"]
  elsif posted_data = CGI.parse(env['rack.input'].read)
    env['rack.input'].rewind
    identifier = posted_data['openid_identifier']
    if identifier.kind_of? Array
      identifier.last
    else
      identifier
    end
  end
end

#identity_matchObject



109
110
111
# File 'lib/easy_rack_open_id_processing.rb', line 109

def identity_match
  options[:identity_match]
end

#login_pathObject



132
133
134
# File 'lib/easy_rack_open_id_processing.rb', line 132

def 
  options[:login_path]
end

#logoutObject



121
122
123
124
125
126
# File 'lib/easy_rack_open_id_processing.rb', line 121

def logout
  self.verified_identity = nil
  if after_logout_path
    forward_to(after_logout_path)
  end
end

#logout_pathObject



117
118
119
# File 'lib/easy_rack_open_id_processing.rb', line 117

def logout_path
  options[:logout_path] || '/logout'
end

#ok(text, content_type = 'text/html') ⇒ Object



189
190
191
# File 'lib/easy_rack_open_id_processing.rb', line 189

def ok(text, content_type = 'text/html')
  [200,{"Content-Type" => content_type, 'Content-Length'=> text.length.to_s},[text]]
end

#open_id_loginObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/easy_rack_open_id_processing.rb', line 40

def 
  if resp = env["rack.openid.response"]
    case resp.status
    when :success
      # Load in any registration data gathered
      profile_data = {}
      # merge the SReg data and the AX data into a single hash of profile data
      [ OpenID::SReg::Response, OpenID::AX::FetchResponse ].each do |data_response|
        if data_response.from_success_response( resp )
          profile_data.merge! data_response.from_success_response( resp ).data
        end
      end

      profile_data['identifier'] = resp.identity_url
      #... save id and registration and forward to ...
      self.verified_identity = profile_data
      forward_to(protected_path)
    when :failure
      
    end
  else
    if identitifier_to_verify && valid_identifier?
      self.protected_path = path
      header_hash =  {:identifier => identitifier_to_verify}
        header_hash.merge!(:required => options[:required]) if options[:required]
        header_hash.merge!(:required => options[:optional]) if options[:optional]
        header_hash.merge!(:required => options[:policy_url]) if options[:policy_url]
      [401, {"WWW-Authenticate" => Rack::OpenID.build_header(header_hash)}, []]
    else
      
    end
  end
end

#pathObject



74
75
76
# File 'lib/easy_rack_open_id_processing.rb', line 74

def path
  env['REQUEST_PATH']
end

#present_login_optionsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/easy_rack_open_id_processing.rb', line 78

def 
  if 
    forward_to()
  else
    dir = File.dirname(__FILE__)
    form = case options[:form]
    when 'boring'
      IO.read(dir + '/generic_openid_form.html.erb')
    when 'selector'
      IO.read(dir + '/nice_openid_form.html.erb')
    else # use default, real-openid selector
      IO.read(dir + '/nicer_openid_form.html.erb')
    end
    ok(form)
  end
end

#protected_pathObject



181
182
183
# File 'lib/easy_rack_open_id_processing.rb', line 181

def protected_path
  session['return_to'] || default_return_to
end

#protected_path=(path) ⇒ Object



177
178
179
# File 'lib/easy_rack_open_id_processing.rb', line 177

def protected_path=(path)
  session['return_to'] = path
end

#sessionObject



173
174
175
# File 'lib/easy_rack_open_id_processing.rb', line 173

def session
  env['rack.session']
end

#valid_identifier?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
# File 'lib/easy_rack_open_id_processing.rb', line 151

def valid_identifier?
  uri = URI.parse(identitifier_to_verify.to_s.strip)
  uri = URI.parse("http://#{uri}") unless uri.scheme
  uri.scheme = uri.scheme.downcase  # URI should do this
  uri.normalize.to_s
rescue URI::InvalidURIError
  # raise InvalidOpenId.new("#{url} is not an OpenID URL")
  false # Quietly fail for now.
end

#verified_identifierObject



169
170
171
# File 'lib/easy_rack_open_id_processing.rb', line 169

def verified_identifier
  verified_identity  && verified_identity['identifier']
end

#verified_identityObject



165
166
167
# File 'lib/easy_rack_open_id_processing.rb', line 165

def verified_identity
  session['verified_identity']
end

#verified_identity=(hash) ⇒ Object



161
162
163
# File 'lib/easy_rack_open_id_processing.rb', line 161

def verified_identity=(hash)
  session['verified_identity'] = hash
end