module Rodauth

Public Instance Methods

_json_response_body(hash) click to toggle source
# File lib/rodauth/features/json.rb, line 196
def _json_response_body(hash)
  request.send(:convert_to_json, hash)
end
before_login_attempt() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 115
def before_login_attempt
  if json_request?
    unless open_account?
      json_response[json_response_error_code_key] = "unverified_account"
    end
  end
  super
end
before_otp_setup_route() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 177
def before_otp_setup_route
  super if defined?(super)
  if json_request? && otp_keys_use_hmac? && !param_or_nil(otp_setup_raw_param)
    _otp_tmp_key(otp_new_secret)
    json_response[otp_setup_param] = otp_user_key
    json_response[otp_setup_raw_param] = otp_key
  end
end
before_rodauth() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 88
def before_rodauth
  if json_request?
    if json_check_accept? && (accept = request.env['HTTP_ACCEPT']) && accept !~ json_accept_regexp
      response.status = 406
      json_response[json_response_status_key] = json_response_error_status
      json_response[json_message_key] = json_not_accepted_error_message
      response['Content-Type'] ||= json_response_content_type
      response.write(_json_response_body(json_response))
      request.halt
    end

    unless request.post?
      response.status = 405
      response.headers['Allow'] = 'POST'
      json_response[json_response_status_key] = json_response_error_status
      json_response[json_message_key] = json_non_post_error_message
      return_json_response
    end
  elsif only_json?
    response.status = json_response_error_status
    response.write non_json_request_error_message
    request.halt
  end

  super
end
before_view_recovery_codes() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 132
def before_view_recovery_codes
  super if defined?(super)
  if json_request?
    json_response[:codes] = recovery_codes
    json_response[json_message_key] ||= "" if json_include_message?
  end
end
before_webauthn_auth_route() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 150
def before_webauthn_auth_route
  super if defined?(super)
  if json_request? && !param_or_nil(webauthn_auth_param)
    cred = webauth_credential_options_for_get
    json_response[webauthn_auth_param] = cred.as_json
    json_response[webauthn_auth_challenge_param] = cred.challenge
    json_response[webauthn_auth_challenge_hmac_param] = compute_hmac(cred.challenge)
  end
end
before_webauthn_login_route() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 160
def before_webauthn_login_route
  super if defined?(super)
  if json_request? && !param_or_nil(webauthn_auth_param) && account_from_login(param(login_param))
    cred = webauth_credential_options_for_get
    json_response[webauthn_auth_param] = cred.as_json
    json_response[webauthn_auth_challenge_param] = cred.challenge
    json_response[webauthn_auth_challenge_hmac_param] = compute_hmac(cred.challenge)
  end
end
before_webauthn_remove_route() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 170
def before_webauthn_remove_route
  super if defined?(super)
  if json_request? && !param_or_nil(webauthn_remove_param)
    json_response[webauthn_remove_param] = account_webauthn_usage
  end
end
before_webauthn_setup_route() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 140
def before_webauthn_setup_route
  super if defined?(super)
  if json_request? && !param_or_nil(webauthn_setup_param)
    cred = new_webauthn_credential
    json_response[webauthn_setup_param] = cred.as_json
    json_response[webauthn_setup_challenge_param] = cred.challenge
    json_response[webauthn_setup_challenge_hmac_param] = compute_hmac(cred.challenge)
  end
end
check_csrf?() click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 83
def check_csrf?
  return false if json_request?
  super
end
json_cors_allow?() click to toggle source
# File lib/rodauth/features/json_cors.rb, line 15
def json_cors_allow?
  return false unless origin = request.env['HTTP_ORIGIN']

  case allowed = json_cors_allow_origin
  when String
    timing_safe_eql?(origin, allowed)
  when Array
    allowed.any?{|s| timing_safe_eql?(origin, s)}
  when Regexp
    allowed =~ origin
  when true
    true
  else
    false
  end
end
json_request?() click to toggle source
# File lib/rodauth/features/json.rb, line 69
def json_request?
  return @json_request if defined?(@json_request)

  @json_request = request.content_type =~ json_request_content_type_regexp
end
json_response() click to toggle source
# File lib/rodauth/features/json.rb, line 192
def json_response
  @json_response ||= {json_response_status_key => json_response_success_status}
end
new_account(login) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 124
def new_account(login)
  if account_from_login(login) && allow_resending_verify_account_email?
    json_response[json_response_error_code_key] = "unverified_account" if json_request?
  end
  super
end
redirect(path) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 186
def redirect(path)
  return super unless json_request?
  # json_response["redirect"] = path
  return_json_response
end
return_json_response() click to toggle source
# File lib/rodauth/features/json.rb, line 200
def return_json_response
  response.status ||= json_response_error_status if json_response[json_response_status_key] == json_response_error_status
  response['Content-Type'] ||= json_response_content_type
  response.write(_json_response_body(json_response))
  request.halt
end
set_error_flash(message) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 41
def set_error_flash(message)
  return super unless json_request?

  json_response[json_response_status_key] = json_response_error_status
  json_response[json_message_key] = message
end
set_field_error(field, message) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 33
def set_field_error(field, message)
  return super unless json_request?

  json_response[json_response_error_code_key] = json_response_field_error_key
  json_response[json_response_status_key] = json_response_field_error_key
  json_response[json_response_field_error_key] = [field, message]
end
set_notice_flash(message) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 55
def set_notice_flash(message)
  return super unless json_request?

  json_response[json_response_status_key] = json_response_success_status
  json_response[json_message_key] = message if json_include_message?
end
set_notice_now_flash(message) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 62
def set_notice_now_flash(message)
  return super unless json_request?

  json_response[json_response_status_key] = json_response_success_status
  json_response[json_message_key] = message if json_include_message?
end
set_redirect_error_flash(message) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 48
def set_redirect_error_flash(message)
  return super unless json_request?

  json_response[json_response_status_key] = json_response_error_status
  json_response[json_message_key] = message
end
set_redirect_error_status(status) click to toggle source
# File lib/rodauth/features/json.rb, line 207
def set_redirect_error_status(status)
  if json_request? && json_response_custom_error_status?
    response.status = status
  end
end
set_response_error_status(status) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 213
def set_response_error_status(status)
  if json_request? && !json_response_custom_error_status?
    status = json_response_error_status
  end

  super
end
view(page, title) click to toggle source
Calls superclass method
# File lib/rodauth/features/json.rb, line 75
def view(page, title)
  return super unless json_request?
  return_json_response
end