| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::Auth::MT; |
|---|
| 8 | use strict; |
|---|
| 9 | |
|---|
| 10 | use base 'MT::ErrorHandler'; |
|---|
| 11 | use MT::Author qw( AUTHOR ); |
|---|
| 12 | |
|---|
| 13 | sub sanity_check { |
|---|
| 14 | my $auth = shift; |
|---|
| 15 | my ($app) = @_; |
|---|
| 16 | my $q = $app->param; |
|---|
| 17 | my $id = $q->param('id'); |
|---|
| 18 | |
|---|
| 19 | if ($q->param('pass') ne $q->param('pass_verify')) { |
|---|
| 20 | return $app->translate('Passwords do not match.'); |
|---|
| 21 | } else { |
|---|
| 22 | if ($q->param('pass') && $id) { |
|---|
| 23 | my $author = MT::Author->load($id) |
|---|
| 24 | or return $app->translate('Failed to verify current password.'); |
|---|
| 25 | if (!$auth->is_valid_password($author, $q->param('old_pass'))) { |
|---|
| 26 | return $app->translate('Failed to verify current password.'); |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | my $hint = $q->param('hint') || ''; |
|---|
| 31 | $hint =~ s!^\s+|\s+$!!gs; |
|---|
| 32 | unless ($hint) { |
|---|
| 33 | return $app->translate('Password hint is required.'); |
|---|
| 34 | } |
|---|
| 35 | return ''; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | sub is_valid_password { |
|---|
| 39 | my $auth = shift; |
|---|
| 40 | my($author, $pass, $crypted, $error_ref) = @_; |
|---|
| 41 | $pass ||= ''; |
|---|
| 42 | |
|---|
| 43 | my $real_pass = $author->column('password'); |
|---|
| 44 | if ((!$real_pass) || ($real_pass eq '(none)')) { |
|---|
| 45 | return 0; |
|---|
| 46 | } |
|---|
| 47 | return $crypted ? $real_pass eq $pass : |
|---|
| 48 | crypt($pass, $real_pass) eq $real_pass; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | sub can_recover_password { 1 } |
|---|
| 52 | sub is_profile_needed { 1 } |
|---|
| 53 | sub password_exists { 1 } |
|---|
| 54 | sub delegate_auth { 0 } |
|---|
| 55 | sub can_logout { 1 } |
|---|
| 56 | |
|---|
| 57 | # Standard MT-based login form / cookie auth. |
|---|
| 58 | sub login_credentials { |
|---|
| 59 | my $auth = shift; |
|---|
| 60 | my ($ctx) = @_; |
|---|
| 61 | |
|---|
| 62 | my $app = $ctx->{app} or return; |
|---|
| 63 | if ($app->param('username') && $app->param('password')) { |
|---|
| 64 | my ($user, $pass, $remember); |
|---|
| 65 | $user = $app->param('username'); |
|---|
| 66 | $pass = $app->param('password'); |
|---|
| 67 | $remember = $app->param('remember') ? 1 : 0; |
|---|
| 68 | return { %$ctx, username => $user, password => $pass, permanent => $remember, auth_type => 'MT' }; |
|---|
| 69 | } |
|---|
| 70 | return undef; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | sub session_credentials { |
|---|
| 74 | my $auth = shift; |
|---|
| 75 | my ($ctx) = @_; |
|---|
| 76 | |
|---|
| 77 | my $app = $ctx->{app} or return; |
|---|
| 78 | my $cookies = $app->cookies; |
|---|
| 79 | if ($cookies->{$app->user_cookie}) { |
|---|
| 80 | my ($user, $session_id, $remember) = split /::/, $cookies->{$app->user_cookie}->value; |
|---|
| 81 | return { %$ctx, username => $user, session_id => $session_id, permanent => $remember, auth_type => 'MT' }; |
|---|
| 82 | } |
|---|
| 83 | return undef; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub fetch_credentials { |
|---|
| 87 | my $auth = shift; |
|---|
| 88 | my ($ctx) = @_; |
|---|
| 89 | return $auth->login_credentials(@_) || $auth->session_credentials(@_); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | sub login_form { |
|---|
| 93 | my $auth = shift; |
|---|
| 94 | my ($app) = @_; |
|---|
| 95 | return $app->build_page('include/login_mt.tmpl'); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | sub validate_credentials { |
|---|
| 99 | my $auth = shift; |
|---|
| 100 | my ($ctx) = @_; |
|---|
| 101 | |
|---|
| 102 | my $app = $ctx->{app}; |
|---|
| 103 | my $username = $ctx->{username}; |
|---|
| 104 | my $password = $ctx->{password}; |
|---|
| 105 | my $result = MT::Auth::UNKNOWN(); |
|---|
| 106 | |
|---|
| 107 | if ((defined $username) && ($username ne '')) { |
|---|
| 108 | # load author from db |
|---|
| 109 | my $user_class = $app->user_class; |
|---|
| 110 | my ($author) = $user_class->search({ name => $username, type => AUTHOR, auth_type => 'MT' }); |
|---|
| 111 | |
|---|
| 112 | if ($author) { |
|---|
| 113 | # password validation |
|---|
| 114 | if ($ctx->{session_id}) { |
|---|
| 115 | $app->user($author); |
|---|
| 116 | $result = MT::Auth::SUCCESS(); |
|---|
| 117 | } else { |
|---|
| 118 | my $error; |
|---|
| 119 | if ($author->is_valid_password($password, 0, \$error)) { |
|---|
| 120 | $app->user($author); |
|---|
| 121 | $result = MT::Auth::NEW_LOGIN(); |
|---|
| 122 | } else { |
|---|
| 123 | $app->error($error); |
|---|
| 124 | $result = MT::Auth::INVALID_PASSWORD(); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | if ($author && !$author->is_active) { |
|---|
| 129 | if ( MT::Author::INACTIVE() == $author->status ) { |
|---|
| 130 | $result = MT::Auth::INACTIVE(); |
|---|
| 131 | $app->user(undef); |
|---|
| 132 | } |
|---|
| 133 | elsif ( MT::Author::PENDING() == $author->status ) { |
|---|
| 134 | $result = MT::Auth::PENDING(); |
|---|
| 135 | # leave user in $app - removed later in app |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | return $result; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | sub invalidate_credentials { |
|---|
| 143 | my $auth = shift; |
|---|
| 144 | my ($ctx) = @_; |
|---|
| 145 | |
|---|
| 146 | my $app = $ctx->{app}; |
|---|
| 147 | my $user = $app->user; |
|---|
| 148 | if ($user) { |
|---|
| 149 | $user->remove_sessions; |
|---|
| 150 | $app->user(undef); |
|---|
| 151 | } |
|---|
| 152 | $app->clear_login_cookie; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | 1; |
|---|
| 156 | |
|---|
| 157 | __END__ |
|---|
| 158 | |
|---|
| 159 | =head1 NAME |
|---|
| 160 | |
|---|
| 161 | MT::Auth::MT |
|---|
| 162 | |
|---|
| 163 | =head1 METHODS |
|---|
| 164 | |
|---|
| 165 | =head2 invalidate_credentials |
|---|
| 166 | |
|---|
| 167 | =head2 is_valid_password |
|---|
| 168 | |
|---|
| 169 | =head2 fetch_credentials |
|---|
| 170 | |
|---|
| 171 | =head2 delegate_auth |
|---|
| 172 | |
|---|
| 173 | =head2 session_credentials |
|---|
| 174 | |
|---|
| 175 | =head2 password_exists |
|---|
| 176 | |
|---|
| 177 | =head2 validate_credentials |
|---|
| 178 | |
|---|
| 179 | =head2 can_logout |
|---|
| 180 | |
|---|
| 181 | =head2 login_form |
|---|
| 182 | |
|---|
| 183 | =head2 sanity_check |
|---|
| 184 | |
|---|
| 185 | =head2 login_credentials |
|---|
| 186 | |
|---|
| 187 | =head2 is_profile_needed |
|---|
| 188 | |
|---|
| 189 | =head2 can_recover_password |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | =head1 AUTHOR & COPYRIGHT |
|---|
| 193 | |
|---|
| 194 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 195 | |
|---|
| 196 | =cut |
|---|