remove the config option, #32456
[freeside.git] / FS / FS / Password_Mixin.pm
1 package FS::Password_Mixin;
2
3 use FS::Record qw(qsearch);
4 use FS::Conf;
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
10 our $DEBUG = 0;
11
12 our $me = '[' . __PACKAGE__ . ']';
13
14 our $BLOWFISH_COST = 10;
15
16 =head1 NAME
17
18 FS::Password_Mixin - Object methods for accounts that have passwords governed
19 by the password policy.
20
21 =head1 METHODS
22
23 =over 4
24
25 =item is_password_allowed PASSWORD
26
27 Checks the password against the system password policy. Returns an error
28 message on failure, an empty string on success.
29
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.
34
35 =cut
36
37 sub is_password_allowed {
38   my $self = shift;
39   my $password = shift;
40
41   # check length and complexity here
42
43   my $no_reuse = 3;
44   # allow override here if we really must
45
46   if ( $no_reuse > 0 ) {
47
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",
55     });
56
57     # don't check the first one; reusing the current password is allowed.
58     shift @latest;
59
60     foreach my $history (@latest) {
61       warn "$me previous password created ".$history->created."\n" if $DEBUG;
62       if ( $history->password_equals($password) ) {
63         my $message;
64         if ( $no_reuse == 1 ) {
65           $message = "This password is the same as your previous password.";
66         } else {
67           $message = "This password was one of the last $no_reuse passwords on this account.";
68         }
69         return $message;
70       }
71     } #foreach $history
72
73   } # end of no_reuse checking
74
75   '';
76 }
77
78 =item password_history_key
79
80 Returns the name of the field in L<FS::password_history> that's the foreign
81 key to this table.
82
83 =cut
84
85 sub password_history_key {
86   my $self = shift;
87   $self->table . '__' . $self->primary_key;
88 }
89
90 =item insert_password_history
91
92 Creates a L<FS::password_history> record linked to this object, with its
93 current password.
94
95 =cut
96
97 sub insert_password_history {
98   my $self = shift;
99   my $encoding = $self->_password_encoding;
100   my $password = $self->_password;
101   my $auth;
102
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(
107       cost        => $cost,
108       salt_base64 => $salt,
109       hash_base64 => $hash,
110     );
111
112   } elsif ( $encoding eq 'crypt' ) {
113
114     # it's smart enough to figure this out
115     $auth = Authen::Passphrase->from_crypt($password);
116
117   } elsif ( $encoding eq 'ldap' ) {
118
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 );
124     }
125   
126   } else {
127     warn "unrecognized password encoding '$encoding'; treating as plain text"
128       unless $encoding eq 'plain';
129
130     $auth = $self->_blowfishcrypt( $password );
131
132   }
133
134   my $password_history = FS::password_history->new({
135       _password => $auth->as_rfc2307,
136       created   => time,
137       $self->password_history_key => $self->get($self->primary_key),
138   });
139
140   my $error = $password_history->insert;
141   return "recording password history: $error" if $error;
142   '';
143
144 }
145
146 =item _blowfishcrypt PASSWORD
147
148 For internal use: takes PASSWORD and returns a new
149 L<Authen::Passphrase::BlowfishCrypt> object representing it.
150
151 =cut
152
153 sub _blowfishcrypt {
154   my $class = shift;
155   my $passphrase = shift;
156   return Authen::Passphrase::BlowfishCrypt->new(
157     cost => $BLOWFISH_COST,
158     salt_random => 1,
159     passphrase => $passphrase,
160   );
161 }
162
163 =back
164
165 =head1 SEE ALSO
166
167 L<FS::password_history>
168
169 =cut
170
171 1;