1 package FS::Password_Mixin;
3 use FS::Record qw(qsearch);
5 use FS::password_history;
6 use Authen::Passphrase;
7 use Authen::Passphrase::BlowfishCrypt;
8 # https://rt.cpan.org/Ticket/Display.html?id=72743
12 our $me = '[' . __PACKAGE__ . ']';
14 our $BLOWFISH_COST = 10;
18 FS::Password_Mixin - Object methods for accounts that have passwords governed
19 by the password policy.
25 =item is_password_allowed PASSWORD
27 Checks the password against the system password policy. Returns an error
28 message on failure, an empty string on success.
30 This MUST NOT be called from check(). It should be called by the office UI,
31 self-service ClientAPI, or other I<user-interactive> code that processes a
32 password change, and only if the user has taken some action with the intent
33 of changing the password.
37 sub is_password_allowed {
41 # check length and complexity here
44 # allow override here if we really must
46 if ( $no_reuse > 0 ) {
48 # "the last N" passwords includes the current password and the N-1
49 # passwords before that.
50 warn "$me checking password reuse limit of $no_reuse\n" if $DEBUG;
51 my @latest = qsearch({
52 'table' => 'password_history',
53 'hashref' => { $self->password_history_key => $self->get($self->primary_key) },
54 'order_by' => " ORDER BY created DESC LIMIT $no_reuse",
57 # don't check the first one; reusing the current password is allowed.
60 foreach my $history (@latest) {
61 warn "$me previous password created ".$history->created."\n" if $DEBUG;
62 if ( $history->password_equals($password) ) {
64 if ( $no_reuse == 1 ) {
65 $message = "This password is the same as your previous password.";
67 $message = "This password was one of the last $no_reuse passwords on this account.";
73 } # end of no_reuse checking
78 =item password_history_key
80 Returns the name of the field in L<FS::password_history> that's the foreign
85 sub password_history_key {
87 $self->table . '__' . $self->primary_key;
90 =item insert_password_history
92 Creates a L<FS::password_history> record linked to this object, with its
97 sub insert_password_history {
99 my $encoding = $self->_password_encoding;
100 my $password = $self->_password;
103 if ( $encoding eq 'bcrypt' ) {
104 # our format, used for contact and access_user passwords
105 my ($cost, $salt, $hash) = split(',', $password);
106 $auth = Authen::Passphrase::BlowfishCrypt->new(
108 salt_base64 => $salt,
109 hash_base64 => $hash,
112 } elsif ( $encoding eq 'crypt' ) {
114 # it's smart enough to figure this out
115 $auth = Authen::Passphrase->from_crypt($password);
117 } elsif ( $encoding eq 'ldap' ) {
119 $password =~ s/^{PLAIN}/{CLEARTEXT}/i; # normalize
120 $auth = Authen::Passphrase->from_rfc2307($password);
121 if ( $auth->isa('Authen::Passphrase::Clear') ) {
122 # then we've been given the password in cleartext
123 $auth = $self->_blowfishcrypt( $auth->passphrase );
127 warn "unrecognized password encoding '$encoding'; treating as plain text"
128 unless $encoding eq 'plain';
130 $auth = $self->_blowfishcrypt( $password );
134 my $password_history = FS::password_history->new({
135 _password => $auth->as_rfc2307,
137 $self->password_history_key => $self->get($self->primary_key),
140 my $error = $password_history->insert;
141 return "recording password history: $error" if $error;
146 =item _blowfishcrypt PASSWORD
148 For internal use: takes PASSWORD and returns a new
149 L<Authen::Passphrase::BlowfishCrypt> object representing it.
155 my $passphrase = shift;
156 return Authen::Passphrase::BlowfishCrypt->new(
157 cost => $BLOWFISH_COST,
159 passphrase => $passphrase,
167 L<FS::password_history>