Class: EasyRackOpenId::Processing

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 = {}) ⇒ Processing

Returns a new instance of Processing.



6
7
8
9
# File 'lib/easy-rack-open-id/processing.rb', line 6

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

Instance Attribute Details

#envObject

Returns the value of attribute env.



4
5
6
# File 'lib/easy-rack-open-id/processing.rb', line 4

def env
  @env
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/easy-rack-open-id/processing.rb', line 4

def options
  @options
end

Instance Method Details

#after_logout_pathObject



129
130
131
# File 'lib/easy-rack-open-id/processing.rb', line 129

def after_logout_path
  options[:after_logout_path]
end

#allowed?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
# File 'lib/easy-rack-open-id/processing.rb', line 100

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

#allowed_identifiersObject



114
115
116
# File 'lib/easy-rack-open-id/processing.rb', line 114

def allowed_identifiers
  options[:allowed_identifiers]
end

#asset?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/easy-rack-open-id/processing.rb', line 29

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

#asset_prefixObject



33
34
35
# File 'lib/easy-rack-open-id/processing.rb', line 33

def asset_prefix
  '/easy-rack-open-id-assets'
end

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easy-rack-open-id/processing.rb', line 11

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



186
187
188
# File 'lib/easy-rack-open-id/processing.rb', line 186

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

#forward_to(url) ⇒ Object



96
97
98
# File 'lib/easy-rack-open-id/processing.rb', line 96

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

#gem_public_pathObject



37
38
39
# File 'lib/easy-rack-open-id/processing.rb', line 37

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

#identitifier_to_verifyObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/easy-rack-open-id/processing.rb', line 137

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



110
111
112
# File 'lib/easy-rack-open-id/processing.rb', line 110

def identity_match
  options[:identity_match]
end

#login_pathObject



133
134
135
# File 'lib/easy-rack-open-id/processing.rb', line 133

def 
  options[:login_path]
end

#logoutObject



122
123
124
125
126
127
# File 'lib/easy-rack-open-id/processing.rb', line 122

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

#logout_pathObject



118
119
120
# File 'lib/easy-rack-open-id/processing.rb', line 118

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

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



190
191
192
# File 'lib/easy-rack-open-id/processing.rb', line 190

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

#open_id_loginObject



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
73
# File 'lib/easy-rack-open-id/processing.rb', line 41

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



75
76
77
# File 'lib/easy-rack-open-id/processing.rb', line 75

def path
  env['REQUEST_PATH']
end

#present_login_optionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/easy-rack-open-id/processing.rb', line 79

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



182
183
184
# File 'lib/easy-rack-open-id/processing.rb', line 182

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

#protected_path=(path) ⇒ Object



178
179
180
# File 'lib/easy-rack-open-id/processing.rb', line 178

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

#sessionObject



174
175
176
# File 'lib/easy-rack-open-id/processing.rb', line 174

def session
  env['rack.session']
end

#valid_identifier?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
# File 'lib/easy-rack-open-id/processing.rb', line 152

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



170
171
172
# File 'lib/easy-rack-open-id/processing.rb', line 170

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

#verified_identityObject



166
167
168
# File 'lib/easy-rack-open-id/processing.rb', line 166

def verified_identity
  session['verified_identity']
end

#verified_identity=(hash) ⇒ Object



162
163
164
# File 'lib/easy-rack-open-id/processing.rb', line 162

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