module Rodauth

def self.configure(app, opts={}, &block)

def self.configure(app, opts={}, &block)
  ((app.opts[:rodauths] ||= {})[opts[:name]] ||= Class.new(Auth)).configure(&block)
end

def self.create_database_authentication_functions(db, opts={})

def self.create_database_authentication_functions(db, opts={})
  table_name = opts[:table_name] || :account_password_hashes
  get_salt_name = opts[:get_salt_name] || :rodauth_get_salt
  valid_hash_name = opts[:valid_hash_name] || :rodauth_valid_password_hash 
  case db.database_type
  when :postgres
    db.run <<END
EATE OR REPLACE FUNCTION #{get_salt_name}(acct_id int8) RETURNS text AS $$
CLARE salt text;
GIN
LECT substr(password_hash, 0, 30) INTO salt 
OM #{table_name}
ERE acct_id = id;
TURN salt;
D;
 LANGUAGE plpgsql
CURITY DEFINER
T search_path = public, pg_temp;
D
    db.run <<END
EATE OR REPLACE FUNCTION #{valid_hash_name}(acct_id int8, hash text) RETURNS boolean AS $$
CLARE valid boolean;
GIN
LECT password_hash = hash INTO valid 
OM #{table_name}
ERE acct_id = id;
TURN valid;
D;
 LANGUAGE plpgsql
CURITY DEFINER
T search_path = public, pg_temp;
D
  when :mysql
    db.run <<END
EATE FUNCTION #{get_salt_name}(acct_id int8) RETURNS varchar(255)
L SECURITY DEFINER
ADS SQL DATA
GIN
CLARE salt varchar(255);
CLARE csr CURSOR FOR
LECT substr(password_hash, 1, 30)
OM #{table_name}
ERE acct_id = id;
EN csr;
TCH csr INTO salt;
OSE csr;
TURN salt;
D;
D
    db.run <<END
EATE FUNCTION #{valid_hash_name}(acct_id int8, hash varchar(255)) RETURNS tinyint(1)
L SECURITY DEFINER
ADS SQL DATA
GIN
CLARE valid tinyint(1);
CLARE csr CURSOR FOR 
LECT password_hash = hash
OM #{table_name}
ERE acct_id = id;
EN csr;
TCH csr INTO valid;
OSE csr;
TURN valid;
D;
D
  when :mssql
    db.run <<END
EATE FUNCTION #{get_salt_name}(@account_id bigint) RETURNS nvarchar(255)
TH EXECUTE AS OWNER

GIN
CLARE @salt nvarchar(255);
LECT @salt = substring(password_hash, 0, 30)
OM #{table_name}
ERE id = @account_id;
TURN @salt;
D;
D
    db.run <<END
EATE FUNCTION #{valid_hash_name}(@account_id bigint, @hash nvarchar(255)) RETURNS bit
TH EXECUTE AS OWNER

GIN
CLARE @valid bit;
CLARE @ph nvarchar(255);
LECT @ph = password_hash
OM #{table_name}
ERE id = @account_id;
(@hash = @ph)
SET @valid = 1;
SE
SET @valid = 0
TURN @valid;
D;
D
  end
end

def self.create_database_previous_password_check_functions(db)

def self.create_database_previous_password_check_functions(db)
  create_database_authentication_functions(db, :table_name=>:account_previous_password_hashes, :get_salt_name=>:rodauth_get_previous_salt, :valid_hash_name=>:rodauth_previous_password_hash_match)
end

def self.drop_database_authentication_functions(db)

def self.drop_database_authentication_functions(db)
  case db.database_type
  when :postgres
    db.run "DROP FUNCTION rodauth_get_salt(int8)"
    db.run "DROP FUNCTION rodauth_valid_password_hash(int8, text)"
  when :mysql, :mssql
    db.run "DROP FUNCTION rodauth_get_salt"
    db.run "DROP FUNCTION rodauth_valid_password_hash"
  end
end

def self.drop_database_previous_password_check_functions(db)

def self.drop_database_previous_password_check_functions(db)
  case db.database_type
  when :postgres
    db.run "DROP FUNCTION rodauth_get_previous_salt(int8)"
    db.run "DROP FUNCTION rodauth_previous_password_hash_match(int8, text)"
  when :mysql, :mssql
    db.run "DROP FUNCTION rodauth_get_previous_salt"
    db.run "DROP FUNCTION rodauth_previous_password_hash_match"
  end
end

def self.load_dependencies(app, opts={})

def self.load_dependencies(app, opts={})
  if opts[:json]
    app.plugin :json
    app.plugin :json_parser
  end
  unless opts[:json] == :only
    require 'tilt/string'
    app.plugin :render
    app.plugin :csrf unless opts[:csrf] == false
    app.plugin :flash unless opts[:flash] == false
    app.plugin :h
  end
end

def self.version

def self.version
  VERSION
end