module Rodauth::InternalRequestClassMethods

  1. lib/rodauth/features/internal_request.rb

Methods

Public Instance

  1. internal_request

Public Instance methods

internal_request(route, opts={}, &block)
[show source]
    # File lib/rodauth/features/internal_request.rb
309 def internal_request(route, opts={}, &block)
310   opts = opts.dup
311   
312   env = {
313      'REQUEST_METHOD'=>'POST',
314      'PATH_INFO'=>'/',
315      "SCRIPT_NAME" => "",
316      "HTTP_HOST" => INVALID_DOMAIN,
317      "SERVER_NAME" => INVALID_DOMAIN,
318      "SERVER_PORT" => 443,
319      "CONTENT_TYPE" => "application/x-www-form-urlencoded",
320      "rack.input"=>StringIO.new(''),
321      "rack.url_scheme"=>"https"
322   }
323   env.merge!(opts.delete(:env)) if opts[:env]
324 
325   session = {}
326   session.merge!(opts.delete(:session)) if opts[:session]
327 
328   params = {}
329   params.merge!(opts.delete(:params)) if opts[:params]
330 
331   scope = roda_class.new(env)
332   rodauth = new(scope)
333   rodauth.session = session
334   rodauth.params = params
335   rodauth.internal_request_block = block
336 
337   unless account_id = opts.delete(:account_id)
338     if (account_login = opts.delete(:account_login))
339       if (account = rodauth.send(:_account_from_login, account_login))
340         account_id = account[rodauth.account_id_column]
341       else
342         raise InternalRequestError, "no account for login: #{account_login.inspect}"
343       end
344     end
345   end
346 
347   if account_id
348     session[rodauth.session_key] = account_id
349     unless authenticated_by = opts.delete(:authenticated_by)
350       authenticated_by = case route
351       when :otp_auth, :sms_request, :sms_auth, :recovery_auth, :webauthn_auth, :webauthn_auth_params, :valid_otp_auth?, :valid_sms_auth?, :valid_recovery_auth?
352         ['internal1']
353       else
354         ['internal1', 'internal2']
355       end
356     end
357     session[rodauth.authenticated_by_session_key] = authenticated_by
358   end
359 
360   opts.keys.each do |k|
361     meth = :"#{k}_param"
362     params[rodauth.public_send(meth).to_s] = opts.delete(k) if rodauth.respond_to?(meth)
363   end
364 
365   unless opts.empty?
366     warn "unhandled options passed to #{route}: #{opts.inspect}"
367   end
368 
369   rodauth.handle_internal_request(:"_handle_#{route}")
370 end