Commit 09666e97 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

Authen from SQL tables too

parent d10fc738
use warnings;
use strict;
package Ravada::Auth;
our $LDAP;
use Ravada::Auth::SQL;
eval { require Ravada::Auth::LDAP; $LDAP = 1 };
sub init {
my ($config, $db_con) = @_;
Ravada::Auth::LDAP::init($config) if $LDAP;
Ravada::Auth::SQL::init($config, $db_con);
}
sub login {
return Ravada::Auth::LDAP::login(@_) if $LDAP;
return Ravada::Auth::SQL::login(@_);
}
1;
......@@ -56,4 +56,7 @@ sub _init_ldap {
or die "I can't connect to LDAP server at $host / $port : $@";
}
sub init {
}
1;
package Ravada::Auth::SQL;
use warnings;
use strict;
use Digest::SHA qw(sha1_hex);
our $CON;
sub init {
my ($config, $con) = @_;
$CON = $con;
}
sub add_user {
my ($login,$password) = @_;
my $sth = $CON->dbh->prepare(
"INSERT INTO users (name,password) VALUES(?,?)");
$sth->execute(sha1_hex($password));
$sth->finish;
}
sub login {
my ($login,$password) = @_;
my $sth = $CON->dbh->prepare(
"SELECT name FROM users WHERE name=? AND password=?");
$sth->execute(sha1_hex($password));
my ($found) = $sth->fetchrow;
$sth->finish;
return if !$found;
return $found;
}
1;
......@@ -4,17 +4,34 @@ use strict;
use Data::Dumper;
use DBIx::Connector;
use Getopt::Long;
use Mojolicious::Lite;
use lib 'lib';
use Ravada::Auth::LDAP;
use Ravada::Auth;
my $FILE_CONFIG = "/etc/ravada.conf";
my $help;
GetOptions(
config => \$FILE_CONFIG
,help => \$help
) or exit;
if ($help) {
print "$0 [--help] [--config=$FILE_CONFIG]\n";
exit;
}
our $CON = DBIx::Connector->new("DBI:mysql:ravada"
,undef,undef,{RaiseError => 1
, PrintError=> 0 }) or die "I can't connect";
our $TIMEOUT = 120;
my $config = plugin Config => {file => $FILE_CONFIG} if -e $FILE_CONFIG;
init();
############################################################################3
any '/' => sub {
my $c = shift;
......@@ -51,7 +68,7 @@ sub quick_start {
}
if ( $login && $password ) {
if (Ravada::Auth::LDAP::login($login, $password)) {
if (Ravada::Auth::login($login, $password)) {
$c->session('login' => $login);
} else {
push @error,("Access denied");
......@@ -232,6 +249,10 @@ sub list_bases {
return \%base;
}
sub init {
Ravada::Auth::init($config);
}
app->start;
__DATA__
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment