Class: OpieController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, OpenID::Server
Defined in:
app/controllers/opie_controller.rb

Instance Method Summary (collapse)

Methods included from ApplicationHelper

#article_summary, #expanded_time_print, #format_text_for_display, #humane_date, #is_submitter, #micro_time_print, #selected_tab_if, #wrap_text

Methods inherited from ApplicationController

#content_date_sort, #do_404, #do_410, #do_invalid_page, #get_county_options, #get_location_options, #mobile_detection, #set_analytics_visitor, #set_app_location, #set_default_request_ip_address, #set_locale, #set_request_url_options, #unescape_params

Methods included from ControllerExtensions

#check_openidurl_foruser, #log_user_activity, #validate_datepicker

Methods included from AuthLib

#current_person, #set_current_person

Instance Method Details

- (Object) decision



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/controllers/opie_controller.rb', line 212

def decision
  opierequest = session[:last_opierequest]
  if(opierequest.nil?)
    # try to redirect back - because something really weird happened with the session
    if(!request.env["HTTP_REFERER"].nil?)
      return redirect_to(request.env["HTTP_REFERER"])
    else
      # intentionally crash it
      flash[:failure] = "An error occurred during your OpenID login.  Please return to the site you were using and try again."
      return redirect_to(people_welcome_url)
    end
  end

  if params[:Allow].nil?
    session[:last_opierequest] = nil
    return redirect_to(opierequest.cancel_url)
  else
    if(!self.approved(opierequest.trust_root))
      @currentuser.opie_approvals.create(:trust_root => opierequest.trust_root)
    end
    server_url = url_for(:action => 'index', :protocol => 'https://')
    if(opierequest.id_select)
      if(opierequest.message.is_openid1)
        response = opierequest.answer(true,server_url,@currentuser.openid_url(true))            
      else
        response = opierequest.answer(true,nil,@currentuser.openid_url,@currentuser.openid_url(true))
      end
    else
      response = opierequest.answer(true)          
    end
    self.add_sreg(opierequest, response)
    @currentuser.update_attribute(:last_login_at,Time.now.utc)
    UserEvent.log_event(:etype => UserEvent::LOGIN_OPENID_SUCCESS,:user => @currentuser,:description => 'openid login',:appname => opierequest.trust_root)                  
    log_user_activity(:user => @currentuser,:activitytype => Activity::LOGIN, :activitycode => Activity::LOGIN_OPENID,:trustroot => opierequest.trust_root)                                     
    session[:last_opierequest] = nil
    return self.render_response(response)
  end
end

- (Object) delegate



33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/opie_controller.rb', line 33

def delegate
  @openiduser = User.(params[:extensionid])
  @openidmeta = openidmeta(@openiduser)
  if(!@openiduser.nil?)
    @publicattributes = @openiduser.public_attributes
  end
  # force format to be html
  params[:format] = 'html'
  render(:layout => 'peopledelegate')
end

- (Object) idp_xrds



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/opie_controller.rb', line 161

def idp_xrds
  types = [OpenID::OPENID_IDP_2_0_TYPE]
  types_string = ''
  types.each do |type|
    types_string += "<Type>#{type}</Type>\n"
  end

  yadis = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS
  xmlns:xrds="xri://$xrds"
  xmlns="xri://$xrd*($v*2.0)">
<XRD>
  <Service priority="1">
    #{types_string}
    <URI>#{url_for(:controller => 'opie',:protocol => 'https://')}</URI>
  </Service>
</XRD>
</xrds:XRDS>
EOS

  response.headers['content-type'] = 'application/xrds+xml'
  render(:text => yadis)
end

- (Object) index



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/opie_controller.rb', line 44

def index
  # first thing, if we came back here from a login - get the request out of the session
  if(!params[:returnfrom].nil? and params[:returnfrom] == 'login')
    opierequest = session[:last_opierequest]
    if(opierequest.nil?)
      flash[:failure] = "An error occurred during your OpenID login.  Please return to the site you were using and try again."
      return redirect_to(people_welcome_url)
    else
      # clear it out of the session
      session[:last_opierequest] = nil
    end
  else    
    begin
      opierequest = server.decode_request(params)
    rescue ProtocolError => e
      # invalid openid request, so just display a page with an error message
      render(:text => e.to_s)
      return
    end
  end
    
  # no openid.mode was given
  unless opierequest
    render(:text => "This is an OpenID server endpoint.")
    return
  end
  # 
  
  server_url = url_for(:action => 'index', :protocol => 'https://')
  
  if opierequest.kind_of?(CheckIDRequest)
    
    if self.is_authorized(opierequest.id_select,opierequest.identity, opierequest.trust_root)
      if(opierequest.id_select)
        if(opierequest.message.is_openid1)
          response = opierequest.answer(true,server_url,@currentuser.openid_url(true))            
        else
          response = opierequest.answer(true,nil,@currentuser.openid_url,@currentuser.openid_url(true))
        end
      else
        response = opierequest.answer(true)          
      end
      # add the sreg response if requested
      self.add_sreg(opierequest, response)
      @currentuser.update_attribute(:last_login_at,Time.now.utc)
      UserEvent.log_event(:etype => UserEvent::LOGIN_OPENID_SUCCESS,:user => @currentuser,:description => 'openid login',:appname => opierequest.trust_root)                  
      log_user_activity(:user => @currentuser,:activitytype => Activity::LOGIN, :activitycode => Activity::LOGIN_OPENID,:trustroot => opierequest.trust_root)                                     
    elsif opierequest.immediate
      response = opierequest.answer(false, server_url)
    else
      if (self.checklogin(opierequest.id_select,opierequest.identity,opierequest.trust_root))
        if(!check_purgatory)
          return
        end
        session[:last_opierequest] = opierequest
        @opierequest = opierequest
        @desicionurl = url_for(:controller => 'opie', :action => 'decision', :protocol => 'https://')
        sregrequest = OpenID::SReg::Request.from_openid_request(opierequest)
        if(!sregrequest.nil?)
          askedfields = (sregrequest.required+sregrequest.optional).uniq
          @willprovide = []
          askedfields.each do |field|
            case field
              when 'nickname'
                @willprovide << "Nickname: #{@currentuser.first_name}"
              when 'email'
                @willprovide << "Email: #{@currentuser.email}"
              when 'fullname'
                @willprovide << "Fullname: #{@currentuser.first_name} #{@currentuser.last_name}"
              when 'extensionid'
                @willprovide << "eXtensionID: #{@currentuser.}"                
              else
                # nada
            end # case
          end # askedfields
        end
        render(:template => 'opie/decide', :layout => 'people')
      else
        @currentuser = nil
        session[:userid] = nil
        session[:last_opierequest] = opierequest
        session[:return_to] = url_for(:controller=>"opie", :action =>"index", :returnfrom => 'login')
        return(redirect_to )
      end
      return
    end

  else
    response = server.handle_request(opierequest)
  end

  self.render_response(response)
end

- (Boolean) ssl_allowed?

Returns:

  • (Boolean)


29
30
31
# File 'app/controllers/opie_controller.rb', line 29

def ssl_allowed?
  (!Rails.env.development?)
end

- (Object) user



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/opie_controller.rb', line 138

def user
  @openiduser = User.(params[:extensionid])
  # Yadis content-negotiation: we want to return the xrds if asked for.
  accept = request.env['HTTP_ACCEPT']
  
  # This is not technically correct, and should eventually be updated
  # to do real Accept header parsing and logic.  Though I expect it will work
  # 99% of the time.
  if (accept and accept.include?('application/xrds+xml') and !@openiduser.nil?)
    return user_xrds
  end

  # content negotiation failed, so just render the user page 
  if(@openiduser.nil?)
    flash.now[:failure] = 'No user by that name here.'
  else
    @openidmeta = openidmeta(@openiduser)
    @publicattributes = @openiduser.public_attributes
  end
  @right_column = false
  render(:layout => 'pubsite')
end

- (Object) user_xrds



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/opie_controller.rb', line 186

def user_xrds
  @openiduser = User.(params[:extensionid])
  if(@openiduser.nil?)
    redirect_to(:action => 'user')
  end
  
  types = [OpenID::OPENID_2_0_TYPE, OpenID::OPENID_1_0_TYPE,OpenID::SREG_URI]
  types_string = ''
  types.each do |type|
    types_string += "<Type>#{type}</Type>\n"
  end

  yadis = '<?xml version="1.0" encoding="UTF-8"?>'+"\n"
  yadis += '<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">'+"\n"
  yadis += "<XRD>\n"
  yadis += '<Service priority="0">' + "\n"
  yadis += "#{types_string}\n"
  yadis += "<URI>#{url_for(:controller => 'opie',:protocol => 'https://')}</URI>\n"
  yadis += "<LocalID>#{@openiduser.openid_url}</LocalID>\n"
  yadis += "</Service>\n"
  yadis += "</XRD>\n"
  yadis += "</xrds:XRDS>\n"
  response.headers['content-type'] = 'application/xrds+xml'
  render(:text => yadis)
end