Module: Rodauth::InternalRequestClassMethods

Defined in:
lib/rodauth/features/internal_request.rb

Instance Method Summary collapse

Instance Method Details

#internal_request(route, opts = {}, &block) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/rodauth/features/internal_request.rb', line 307

def internal_request(route, opts={}, &block)
  opts = opts.dup
  
  env = {
     'REQUEST_METHOD'=>'POST',
     'PATH_INFO'=>'/'.dup,
     "SCRIPT_NAME" => "",
     "HTTP_HOST" => INVALID_DOMAIN,
     "SERVER_NAME" => INVALID_DOMAIN,
     "SERVER_PORT" => 443,
     "CONTENT_TYPE" => "application/x-www-form-urlencoded",
     "rack.input"=>StringIO.new(''),
     "rack.url_scheme"=>"https"
  }
  env.merge!(opts.delete(:env)) if opts[:env]

  session = {}
  session.merge!(opts.delete(:session)) if opts[:session]

  params = {}
  params.merge!(opts.delete(:params)) if opts[:params]

  scope = roda_class.new(env)
  rodauth = new(scope)
  rodauth.session = session
  rodauth.params = params
  rodauth.internal_request_block = block

  unless  = opts.delete(:account_id)
    if ( = opts.delete(:account_login))
      if ( = rodauth.send(:_account_from_login, ))
         = [rodauth.]
      else
        raise InternalRequestError, "no account for login: #{account_login.inspect}"
      end
    end
  end

  if 
    session[rodauth.session_key] = 
    unless authenticated_by = opts.delete(:authenticated_by)
      authenticated_by = case route
      when :otp_auth, :sms_request, :sms_auth, :recovery_auth, :webauthn_auth, :webauthn_auth_params, :valid_otp_auth?, :valid_sms_auth?, :valid_recovery_auth?
        ['internal1']
      else
        ['internal1', 'internal2']
      end
    end
    session[rodauth.authenticated_by_session_key] = authenticated_by
  end

  opts.keys.each do |k|
    meth = :"#{k}_param"
    params[rodauth.public_send(meth).to_s] = opts.delete(k) if rodauth.respond_to?(meth)
  end

  unless opts.empty?
    warn "unhandled options passed to #{route}: #{opts.inspect}"
  end

  rodauth.handle_internal_request(:"_handle_#{route}")
end