Lines 23-28
use Digest::MD5 qw(md5_base64);
Link Here
|
23 |
use JSON qw/encode_json decode_json/; |
23 |
use JSON qw/encode_json decode_json/; |
24 |
use URI::Escape; |
24 |
use URI::Escape; |
25 |
use CGI::Session; |
25 |
use CGI::Session; |
|
|
26 |
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); |
27 |
use Crypt::Random::Source; |
26 |
|
28 |
|
27 |
require Exporter; |
29 |
require Exporter; |
28 |
use C4::Context; |
30 |
use C4::Context; |
Lines 47-53
BEGIN {
Link Here
|
47 |
@ISA = qw(Exporter); |
49 |
@ISA = qw(Exporter); |
48 |
@EXPORT = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions); |
50 |
@EXPORT = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions); |
49 |
@EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &get_all_subpermissions &get_user_subpermissions |
51 |
@EXPORT_OK = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &get_all_subpermissions &get_user_subpermissions |
50 |
ParseSearchHistoryCookie |
52 |
ParseSearchHistoryCookie hash_password |
51 |
); |
53 |
); |
52 |
%EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] ); |
54 |
%EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] ); |
53 |
$ldap = C4::Context->config('useldapserver') || 0; |
55 |
$ldap = C4::Context->config('useldapserver') || 0; |
Lines 1464-1469
sub get_session {
Link Here
|
1464 |
return $session; |
1466 |
return $session; |
1465 |
} |
1467 |
} |
1466 |
|
1468 |
|
|
|
1469 |
# Using Bcrypt method for hashing. This can be changed to something else in future, if needed. |
1470 |
sub hash_password { |
1471 |
my $password = shift; |
1472 |
|
1473 |
# Generate a salt if one is not passed |
1474 |
my $settings = shift; |
1475 |
unless( defined $settings ){ # if there are no settings, we need to create a salt and append settings |
1476 |
# Set the cost to 8 and append a NULL |
1477 |
$settings = '$2a$08$'.en_base64(generate_salt('strong')); |
1478 |
} |
1479 |
# Encrypt it |
1480 |
return bcrypt($password, $settings); |
1481 |
} |
1482 |
|
1483 |
# Return a random salt |
1484 |
sub generate_salt { |
1485 |
my ($type) = @_; |
1486 |
|
1487 |
if ($type eq 'strong') { |
1488 |
return Crypt::Random::Source::get_strong(16); |
1489 |
} else { |
1490 |
return Crypt::Random::Source::get_weak(16); |
1491 |
} |
1492 |
} |
1493 |
|
1467 |
sub checkpw { |
1494 |
sub checkpw { |
1468 |
|
1495 |
|
1469 |
my ( $dbh, $userid, $password, $query ) = @_; |
1496 |
my ( $dbh, $userid, $password, $query ) = @_; |
Lines 1489-1498
sub checkpw {
Link Here
|
1489 |
); |
1516 |
); |
1490 |
$sth->execute($userid); |
1517 |
$sth->execute($userid); |
1491 |
if ( $sth->rows ) { |
1518 |
if ( $sth->rows ) { |
1492 |
my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname, |
1519 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1493 |
$surname, $branchcode, $flags ) |
1520 |
$surname, $branchcode, $flags ) |
1494 |
= $sth->fetchrow; |
1521 |
= $sth->fetchrow; |
1495 |
if ( md5_base64($password) eq $md5password and $md5password ne "!") { |
1522 |
|
|
|
1523 |
# check what encryption algorithm was implemented: Bcrypt - if the hash starts with '$2' it is Bcrypt else md5 |
1524 |
my $hash; |
1525 |
if ( substr($stored_hash,0,2) eq '$2') { |
1526 |
$hash = hash_password($password, $stored_hash); |
1527 |
} else { |
1528 |
$hash = md5_base64($password); |
1529 |
} |
1530 |
if ( $hash eq $stored_hash and $stored_hash ne "!") { |
1496 |
|
1531 |
|
1497 |
C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber, |
1532 |
C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber, |
1498 |
$firstname, $surname, $branchcode, $flags ); |
1533 |
$firstname, $surname, $branchcode, $flags ); |
Lines 1505-1514
sub checkpw {
Link Here
|
1505 |
); |
1540 |
); |
1506 |
$sth->execute($userid); |
1541 |
$sth->execute($userid); |
1507 |
if ( $sth->rows ) { |
1542 |
if ( $sth->rows ) { |
1508 |
my ( $md5password, $cardnumber, $borrowernumber, $userid, $firstname, |
1543 |
my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname, |
1509 |
$surname, $branchcode, $flags ) |
1544 |
$surname, $branchcode, $flags ) |
1510 |
= $sth->fetchrow; |
1545 |
= $sth->fetchrow; |
1511 |
if ( md5_base64($password) eq $md5password ) { |
1546 |
|
|
|
1547 |
# check what encryption algorithm was implemented: Bcrypt - if the hash starts with '$2' it is Bcrypt else md5 |
1548 |
my $hash; |
1549 |
if ( substr($stored_hash,0,2) eq '$2') { |
1550 |
$hash = hash_password($password, $stored_hash); |
1551 |
} else { |
1552 |
$hash = md5_base64($password); |
1553 |
} |
1554 |
|
1555 |
if ( $hash eq $stored_hash ) { |
1512 |
|
1556 |
|
1513 |
C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber, |
1557 |
C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber, |
1514 |
$firstname, $surname, $branchcode, $flags ); |
1558 |
$firstname, $surname, $branchcode, $flags ); |