DB.pm 2.94 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7

8
package Bugzilla::Auth::Verify::DB;
9
use strict;
10
use base qw(Bugzilla::Auth::Verify);
11 12

use Bugzilla::Constants;
13
use Bugzilla::Token;
14
use Bugzilla::Util;
15
use Bugzilla::User;
16

17 18 19
sub check_credentials {
    my ($self, $login_data) = @_;
    my $dbh = Bugzilla->dbh;
20

21
    my $username = $login_data->{username};
22
    my $user = new Bugzilla::User({ name => $username });
23

24
    return { failure => AUTH_NO_SUCH_USER } unless $user;
25

26 27 28 29 30 31
    $login_data->{user} = $user;
    $login_data->{bz_username} = $user->login;

    if ($user->account_is_locked_out) {
        return { failure => AUTH_LOCKOUT, user => $user };
    }
32

33 34
    my $password = $login_data->{password};
    my $real_password_crypted = $user->cryptpassword;
35

36 37
    # Using the internal crypted password as the salt,
    # crypt the password the user entered.
38
    my $entered_password_crypted = bz_crypt($password, $real_password_crypted);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    if ($entered_password_crypted ne $real_password_crypted) {
        # Record the login failure
        $user->note_login_failure();

        # Immediately check if we are locked out
        if ($user->account_is_locked_out) {
            return { failure => AUTH_LOCKOUT, user => $user,
                     just_locked_out => 1 };
        }

        return { failure => AUTH_LOGINFAILED,
                 failure_count => scalar(@{ $user->account_ip_login_failures }),
               };
    } 
54

55 56
    # Force the user to type a longer password if it's too short.
    if (length($password) < USER_PASSWORD_MIN_LENGTH) {
57 58
        return { failure => AUTH_ERROR, user_error => 'password_current_too_short',
                 details => { locked_user => $user } };
59 60
    }

61
    # The user's credentials are okay, so delete any outstanding
62 63 64
    # password tokens or login failures they may have generated.
    Bugzilla::Token::DeletePasswordTokens($user->id, "user_logged_in");
    $user->clear_login_failures();
65

66 67 68 69 70
    # If their old password was using crypt() or some different hash
    # than we're using now, convert the stored password to using
    # whatever hashing system we're using now.
    my $current_algorithm = PASSWORD_DIGEST_ALGORITHM;
    if ($real_password_crypted !~ /{\Q$current_algorithm\E}$/) {
71 72 73
        # We can't call $user->set_password because we don't want the password
        # complexity rules to apply here.
        $user->{cryptpassword} = bz_crypt($password);
74
        $user->update();
75 76
    }

77
    return $login_data;
78 79 80
}

sub change_password {
81
    my ($self, $user, $password) = @_;
82
    my $dbh = Bugzilla->dbh;
83
    my $cryptpassword = bz_crypt($password);
84 85
    $dbh->do("UPDATE profiles SET cryptpassword = ? WHERE userid = ?",
             undef, $cryptpassword, $user->id);
86 87
}

88
1;