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
9 use Data::Password qw(:all);
13 FS::UID->install_callback( sub {
14 $conf = FS::Conf->new;
17 our $me = '[' . __PACKAGE__ . ']';
19 our $BLOWFISH_COST = 10;
23 FS::Password_Mixin - Object methods for accounts that have passwords governed
24 by the password policy.
30 =item is_password_allowed PASSWORD
32 Checks the password against the system password policy. Returns an error
33 message on failure, an empty string on success.
35 This MUST NOT be called from check(). It should be called by the office UI,
36 self-service ClientAPI, or other I<user-interactive> code that processes a
37 password change, and only if the user has taken some action with the intent
38 of changing the password.
42 sub is_password_allowed {
46 # basic checks using Data::Password;
47 # options for Data::Password
48 $DICTIONARY = 4; # minimum length of disallowed words
49 $MINLEN = $conf->config('passwordmin') || 6;
50 $MAXLEN = $conf->config('passwordmax') || 8;
51 $GROUPS = 4; # must have all 4 'character groups': numbers, symbols, uppercase, lowercase
52 # other options use the defaults listed below:
53 # $FOLLOWING = 3; # disallows more than 3 chars in a row, by alphabet or keyboard (ie abcd or asdf)
54 # $SKIPCHAR = undef; # set to true to skip checking for bad characters
55 # # lists of disallowed words
56 # @DICTIONARIES = qw( /usr/share/dict/web2 /usr/share/dict/words /usr/share/dict/linux.words );
58 my $error = IsBadPassword($password);
59 $error = 'must contain at least one each of numbers, symbols, and lowercase and uppercase letters'
60 if $error eq 'contains less than 4 character groups'; # avoid confusion
61 $error = 'Invalid password - ' . $error if $error;
62 return $error if $error;
64 #check against service fields
65 $error = $self->password_svc_check($password);
66 return $error if $error;
68 return '' unless $self->get($self->primary_key); # for validating new passwords pre-insert
71 # allow override here if we really must
73 if ( $no_reuse > 0 ) {
75 # "the last N" passwords includes the current password and the N-1
76 # passwords before that.
77 warn "$me checking password reuse limit of $no_reuse\n" if $DEBUG;
78 my @latest = qsearch({
79 'table' => 'password_history',
80 'hashref' => { $self->password_history_key => $self->get($self->primary_key) },
81 'order_by' => " ORDER BY created DESC LIMIT $no_reuse",
84 # don't check the first one; reusing the current password is allowed.
87 foreach my $history (@latest) {
88 warn "$me previous password created ".$history->created."\n" if $DEBUG;
89 if ( $history->password_equals($password) ) {
91 if ( $no_reuse == 1 ) {
92 $message = "This password is the same as your previous password.";
94 $message = "This password was one of the last $no_reuse passwords on this account.";
100 } # end of no_reuse checking
105 =item password_svc_check
107 Override to run additional service-specific password checks.
111 sub password_svc_check {
112 my ($self, $password) = @_;
116 =item password_history_key
118 Returns the name of the field in L<FS::password_history> that's the foreign
123 sub password_history_key {
125 $self->table . '__' . $self->primary_key;
128 =item insert_password_history
130 Creates a L<FS::password_history> record linked to this object, with its
135 sub insert_password_history {
137 my $encoding = $self->_password_encoding;
138 my $password = $self->_password;
141 if ( $encoding eq 'bcrypt' ) {
142 # our format, used for contact and access_user passwords
143 my ($cost, $salt, $hash) = split(',', $password);
144 $auth = Authen::Passphrase::BlowfishCrypt->new(
146 salt_base64 => $salt,
147 hash_base64 => $hash,
150 } elsif ( $encoding eq 'crypt' ) {
152 # it's smart enough to figure this out
153 $auth = Authen::Passphrase->from_crypt($password);
155 } elsif ( $encoding eq 'ldap' ) {
157 $password =~ s/^{PLAIN}/{CLEARTEXT}/i; # normalize
158 $auth = Authen::Passphrase->from_rfc2307($password);
159 if ( $auth->isa('Authen::Passphrase::Clear') ) {
160 # then we've been given the password in cleartext
161 $auth = $self->_blowfishcrypt( $auth->passphrase );
165 warn "unrecognized password encoding '$encoding'; treating as plain text"
166 unless $encoding eq 'plain';
168 $auth = $self->_blowfishcrypt( $password );
172 my $password_history = FS::password_history->new({
173 _password => $auth->as_rfc2307,
175 $self->password_history_key => $self->get($self->primary_key),
178 my $error = $password_history->insert;
179 return "recording password history: $error" if $error;
184 =item _blowfishcrypt PASSWORD
186 For internal use: takes PASSWORD and returns a new
187 L<Authen::Passphrase::BlowfishCrypt> object representing it.
193 my $passphrase = shift;
194 return Authen::Passphrase::BlowfishCrypt->new(
195 cost => $BLOWFISH_COST,
197 passphrase => $passphrase,
205 L<FS::password_history>