DB.pm 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Terry Weissman <terry@mozilla.org>
#                 Dan Mosedale <dmose@mozilla.org>
#                 Joe Robins <jmrobins@tgix.com>
#                 Dave Miller <justdave@syndicomm.com>
#                 Christopher Aillon <christopher@aillon.com>
#                 Gervase Markham <gerv@gerv.net>
#                 Christian Reis <kiko@async.com.br>
#                 Bradley Baetz <bbaetz@acm.org>
28
#                 Erik Stambaugh <erik@dasbistro.com>
29

30
package Bugzilla::Auth::Verify::DB;
31
use strict;
32
use base qw(Bugzilla::Auth::Verify);
33 34

use Bugzilla::Constants;
35
use Bugzilla::Token;
36
use Bugzilla::Util;
37
use Bugzilla::User;
38

39 40 41
sub check_credentials {
    my ($self, $login_data) = @_;
    my $dbh = Bugzilla->dbh;
42

43
    my $username = $login_data->{username};
44
    my $user = new Bugzilla::User({ name => $username });
45

46
    return { failure => AUTH_NO_SUCH_USER } unless $user;
47

48 49 50 51 52 53
    $login_data->{user} = $user;
    $login_data->{bz_username} = $user->login;

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

55 56
    my $password = $login_data->{password};
    my $real_password_crypted = $user->cryptpassword;
57

58 59
    # Using the internal crypted password as the salt,
    # crypt the password the user entered.
60
    my $entered_password_crypted = bz_crypt($password, $real_password_crypted);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    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 }),
               };
    } 
76

77 78
    # Force the user to type a longer password if it's too short.
    if (length($password) < USER_PASSWORD_MIN_LENGTH) {
79 80
        return { failure => AUTH_ERROR, user_error => 'password_current_too_short',
                 details => { locked_user => $user } };
81 82
    }

83
    # The user's credentials are okay, so delete any outstanding
84 85 86
    # password tokens or login failures they may have generated.
    Bugzilla::Token::DeletePasswordTokens($user->id, "user_logged_in");
    $user->clear_login_failures();
87

88 89 90 91 92
    # 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}$/) {
93 94
        $user->set_password($password);
        $user->update();
95 96
    }

97
    return $login_data;
98 99 100
}

sub change_password {
101
    my ($self, $user, $password) = @_;
102
    my $dbh = Bugzilla->dbh;
103
    my $cryptpassword = bz_crypt($password);
104 105
    $dbh->do("UPDATE profiles SET cryptpassword = ? WHERE userid = ?",
             undef, $cryptpassword, $user->id);
106 107
}

108
1;