Auth.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Netscape Communications
  16. # Corporation. Portions created by Netscape are
  17. # Copyright (C) 1998 Netscape Communications Corporation. All
  18. # Rights Reserved.
  19. #
  20. # Contributor(s): Bradley Baetz <bbaetz@acm.org>
  21. # Erik Stambaugh <erik@dasbistro.com>
  22. # Max Kanat-Alexander <mkanat@bugzilla.org>
  23. package Bugzilla::Auth;
  24. use strict;
  25. use fields qw(
  26. _info_getter
  27. _verifier
  28. _persister
  29. );
  30. use Bugzilla::Constants;
  31. use Bugzilla::Error;
  32. use Bugzilla::Auth::Login::Stack;
  33. use Bugzilla::Auth::Verify::Stack;
  34. use Bugzilla::Auth::Persist::Cookie;
  35. sub new {
  36. my ($class, $params) = @_;
  37. my $self = fields::new($class);
  38. $params ||= {};
  39. $params->{Login} ||= Bugzilla->params->{'user_info_class'} . ',Cookie';
  40. $params->{Verify} ||= Bugzilla->params->{'user_verify_class'};
  41. $self->{_info_getter} = new Bugzilla::Auth::Login::Stack($params->{Login});
  42. $self->{_verifier} = new Bugzilla::Auth::Verify::Stack($params->{Verify});
  43. # If we ever have any other login persistence methods besides cookies,
  44. # this could become more configurable.
  45. $self->{_persister} = new Bugzilla::Auth::Persist::Cookie();
  46. return $self;
  47. }
  48. sub login {
  49. my ($self, $type) = @_;
  50. my $dbh = Bugzilla->dbh;
  51. # Get login info from the cookie, form, environment variables, etc.
  52. my $login_info = $self->{_info_getter}->get_login_info();
  53. if ($login_info->{failure}) {
  54. return $self->_handle_login_result($login_info, $type);
  55. }
  56. # Now verify his username and password against the DB, LDAP, etc.
  57. if ($self->{_info_getter}->{successful}->requires_verification) {
  58. $login_info = $self->{_verifier}->check_credentials($login_info);
  59. if ($login_info->{failure}) {
  60. return $self->_handle_login_result($login_info, $type);
  61. }
  62. $login_info =
  63. $self->{_verifier}->{successful}->create_or_update_user($login_info);
  64. }
  65. else {
  66. $login_info = $self->{_verifier}->create_or_update_user($login_info);
  67. }
  68. if ($login_info->{failure}) {
  69. return $self->_handle_login_result($login_info, $type);
  70. }
  71. # Make sure the user isn't disabled.
  72. my $user = $login_info->{user};
  73. if ($user->disabledtext) {
  74. return $self->_handle_login_result({ failure => AUTH_DISABLED,
  75. user => $user }, $type);
  76. }
  77. $user->set_authorizer($self);
  78. return $self->_handle_login_result($login_info, $type);
  79. }
  80. sub can_change_password {
  81. my ($self) = @_;
  82. my $verifier = $self->{_verifier}->{successful};
  83. $verifier ||= $self->{_verifier};
  84. my $getter = $self->{_info_getter}->{successful};
  85. $getter = $self->{_info_getter}
  86. if (!$getter || $getter->isa('Bugzilla::Auth::Login::Cookie'));
  87. return $verifier->can_change_password &&
  88. $getter->user_can_create_account;
  89. }
  90. sub can_login {
  91. my ($self) = @_;
  92. my $getter = $self->{_info_getter}->{successful};
  93. $getter = $self->{_info_getter}
  94. if (!$getter || $getter->isa('Bugzilla::Auth::Login::Cookie'));
  95. return $getter->can_login;
  96. }
  97. sub can_logout {
  98. my ($self) = @_;
  99. my $getter = $self->{_info_getter}->{successful};
  100. # If there's no successful getter, we're not logged in, so of
  101. # course we can't log out!
  102. return 0 unless $getter;
  103. return $getter->can_logout;
  104. }
  105. sub user_can_create_account {
  106. my ($self) = @_;
  107. my $verifier = $self->{_verifier}->{successful};
  108. $verifier ||= $self->{_verifier};
  109. my $getter = $self->{_info_getter}->{successful};
  110. $getter = $self->{_info_getter}
  111. if (!$getter || $getter->isa('Bugzilla::Auth::Login::Cookie'));
  112. return $verifier->user_can_create_account
  113. && $getter->user_can_create_account;
  114. }
  115. sub can_change_email {
  116. return $_[0]->user_can_create_account;
  117. }
  118. sub _handle_login_result {
  119. my ($self, $result, $login_type) = @_;
  120. my $dbh = Bugzilla->dbh;
  121. my $user = $result->{user};
  122. my $fail_code = $result->{failure};
  123. if (!$fail_code) {
  124. if ($self->{_info_getter}->{successful}->requires_persistence) {
  125. $self->{_persister}->persist_login($user);
  126. }
  127. }
  128. elsif ($fail_code == AUTH_ERROR) {
  129. ThrowCodeError($result->{error}, $result->{details});
  130. }
  131. elsif ($fail_code == AUTH_NODATA) {
  132. $self->{_info_getter}->fail_nodata($self)
  133. if $login_type == LOGIN_REQUIRED;
  134. # If we're not LOGIN_REQUIRED, we just return the default user.
  135. $user = Bugzilla->user;
  136. }
  137. # The username/password may be wrong
  138. # Don't let the user know whether the username exists or whether
  139. # the password was just wrong. (This makes it harder for a cracker
  140. # to find account names by brute force)
  141. elsif ($fail_code == AUTH_LOGINFAILED or $fail_code == AUTH_NO_SUCH_USER) {
  142. ThrowUserError("invalid_username_or_password");
  143. }
  144. # The account may be disabled
  145. elsif ($fail_code == AUTH_DISABLED) {
  146. $self->{_persister}->logout();
  147. # XXX This is NOT a good way to do this, architecturally.
  148. $self->{_persister}->clear_browser_cookies();
  149. # and throw a user error
  150. ThrowUserError("account_disabled",
  151. {'disabled_reason' => $result->{user}->disabledtext});
  152. }
  153. # If we get here, then we've run out of options, which shouldn't happen.
  154. else {
  155. ThrowCodeError("authres_unhandled", { value => $fail_code });
  156. }
  157. return $user;
  158. }
  159. 1;
  160. __END__
  161. =head1 NAME
  162. Bugzilla::Auth - An object that authenticates the login credentials for
  163. a user.
  164. =head1 DESCRIPTION
  165. Handles authentication for Bugzilla users.
  166. Authentication from Bugzilla involves two sets of modules. One set is
  167. used to obtain the username/password (from CGI, email, etc), and the
  168. other set uses this data to authenticate against the datasource
  169. (the Bugzilla DB, LDAP, PAM, etc.).
  170. Modules for obtaining the username/password are subclasses of
  171. L<Bugzilla::Auth::Login>, and modules for authenticating are subclasses
  172. of L<Bugzilla::Auth::Verify>.
  173. =head1 AUTHENTICATION ERROR CODES
  174. Whenever a method in the C<Bugzilla::Auth> family fails in some way,
  175. it will return a hashref containing at least a single key called C<failure>.
  176. C<failure> will point to an integer error code, and depending on the error
  177. code the hashref may contain more data.
  178. The error codes are explained here below.
  179. =head2 C<AUTH_NODATA>
  180. Insufficient login data was provided by the user. This may happen in several
  181. cases, such as cookie authentication when the cookie is not present.
  182. =head2 C<AUTH_ERROR>
  183. An error occurred when trying to use the login mechanism.
  184. The hashref will also contain an C<error> element, which is the name
  185. of an error from C<template/en/default/global/code-error.html> --
  186. the same type of error that would be thrown by
  187. L<Bugzilla::Error::ThrowCodeError>.
  188. The hashref *may* contain an element called C<details>, which is a hashref
  189. that should be passed to L<Bugzilla::Error::ThrowCodeError> as the
  190. various fields to be used in the error message.
  191. =head2 C<AUTH_LOGINFAILED>
  192. An incorrect username or password was given.
  193. =head2 C<AUTH_NO_SUCH_USER>
  194. This is an optional more-specific version of C<AUTH_LOGINFAILED>.
  195. Modules should throw this error when they discover that the
  196. requested user account actually does not exist, according to them.
  197. That is, for example, L<Bugzilla::Auth::Verify::LDAP> would throw
  198. this if the user didn't exist in LDAP.
  199. The difference between C<AUTH_NO_SUCH_USER> and C<AUTH_LOGINFAILED>
  200. should never be communicated to the user, for security reasons.
  201. =head2 C<AUTH_DISABLED>
  202. The user successfully logged in, but their account has been disabled.
  203. Usually this is throw only by C<Bugzilla::Auth::login>.
  204. =head1 LOGIN TYPES
  205. The C<login> function (below) can do different types of login, depending
  206. on what constant you pass into it:
  207. =head2 C<LOGIN_OPTIONAL>
  208. A login is never required to access this data. Attempting to login is
  209. still useful, because this allows the page to be personalised. Note that
  210. an incorrect login will still trigger an error, even though the lack of
  211. a login will be OK.
  212. =head2 C<LOGIN_NORMAL>
  213. A login may or may not be required, depending on the setting of the
  214. I<requirelogin> parameter. This is the default if you don't specify a
  215. type.
  216. =head2 C<LOGIN_REQUIRED>
  217. A login is always required to access this data.
  218. =head1 METHODS
  219. These are methods that can be called on a C<Bugzilla::Auth> object
  220. itself.
  221. =head2 Login
  222. =over 4
  223. =item C<login($type)>
  224. Description: Logs a user in. For more details on how this works
  225. internally, see the section entitled "STRUCTURE."
  226. Params: $type - One of the Login Types from above.
  227. Returns: An authenticated C<Bugzilla::User>. Or, if the type was
  228. not C<LOGIN_REQUIRED>, then we return an
  229. empty C<Bugzilla::User> if no login data was passed in.
  230. =back
  231. =head2 Info Methods
  232. These are methods that give information about the Bugzilla::Auth object.
  233. =over 4
  234. =item C<can_change_password>
  235. Description: Tells you whether or not the current login system allows
  236. changing passwords.
  237. Params: None
  238. Returns: C<true> if users and administrators should be allowed to
  239. change passwords, C<false> otherwise.
  240. =item C<can_login>
  241. Description: Tells you whether or not the current login system allows
  242. users to log in through the web interface.
  243. Params: None
  244. Returns: C<true> if users can log in through the web interface,
  245. C<false> otherwise.
  246. =item C<can_logout>
  247. Description: Tells you whether or not the current login system allows
  248. users to log themselves out.
  249. Params: None
  250. Returns: C<true> if users can log themselves out, C<false> otherwise.
  251. If a user isn't logged in, we always return C<false>.
  252. =item C<user_can_create_account>
  253. Description: Tells you whether or not users are allowed to manually create
  254. their own accounts, based on the current login system in use.
  255. Note that this doesn't check the C<createemailregexp>
  256. parameter--you have to do that by yourself in your code.
  257. Params: None
  258. Returns: C<true> if users are allowed to create new Bugzilla accounts,
  259. C<false> otherwise.
  260. =item C<can_change_email>
  261. Description: Whether or not the current login system allows users to
  262. change their own email address.
  263. Params: None
  264. Returns: C<true> if users can change their own email address,
  265. C<false> otherwise.
  266. =back
  267. =head1 STRUCTURE
  268. This section is mostly interesting to developers who want to implement
  269. a new authentication type. It describes the general structure of the
  270. Bugzilla::Auth family, and how the C<login> function works.
  271. A C<Bugzilla::Auth> object is essentially a collection of a few other
  272. objects: the "Info Getter," the "Verifier," and the "Persistence
  273. Mechanism."
  274. They are used inside the C<login> function in the following order:
  275. =head2 The Info Getter
  276. This is a C<Bugzilla::Auth::Login> object. Basically, it gets the
  277. username and password from the user, somehow. Or, it just gets enough
  278. information to uniquely identify a user, and passes that on down the line.
  279. (For example, a C<user_id> is enough to uniquely identify a user,
  280. even without a username and password.)
  281. Some Info Getters don't require any verification. For example, if we got
  282. the C<user_id> from a Cookie, we don't need to check the username and
  283. password.
  284. If an Info Getter returns only a C<user_id> and no username/password,
  285. then it MUST NOT require verification. If an Info Getter requires
  286. verfication, then it MUST return at least a C<username>.
  287. =head2 The Verifier
  288. This verifies that the username and password are valid.
  289. It's possible that some methods of verification don't require a password.
  290. =head2 The Persistence Mechanism
  291. This makes it so that the user doesn't have to log in on every page.
  292. Normally this object just sends a cookie to the user's web browser,
  293. as that's the most common method of "login persistence."
  294. =head2 Other Things We Do
  295. After we verify the username and password, sometimes we automatically
  296. create an account in the Bugzilla database, for certain authentication
  297. types. We use the "Account Source" to get data about the user, and
  298. create them in the database. (Or, if their data has changed since the
  299. last time they logged in, their data gets updated.)
  300. =head2 The C<$login_data> Hash
  301. All of the C<Bugzilla::Auth::Login> and C<Bugzilla::Auth::Verify>
  302. methods take an argument called C<$login_data>. This is basically
  303. a hash that becomes more and more populated as we go through the
  304. C<login> function.
  305. All C<Bugzilla::Auth::Login> and C<Bugzilla::Auth::Verify> methods
  306. also *return* the C<$login_data> structure, when they succeed. They
  307. may have added new data to it.
  308. For all C<Bugzilla::Auth::Login> and C<Bugzilla::Auth::Verify> methods,
  309. the rule is "you must return the same hashref you were passed in." You can
  310. modify the hashref all you want, but you can't create a new one. The only
  311. time you can return a new one is if you're returning some error code
  312. instead of the C<$login_data> structure.
  313. Each C<Bugzilla::Auth::Login> or C<Bugzilla::Auth::Verify> method
  314. explains in its documentation which C<$login_data> elements are
  315. required by it, and which are set by it.
  316. Here are all of the elements that *may* be in C<$login_data>:
  317. =over 4
  318. =item C<user_id>
  319. A Bugzilla C<user_id> that uniquely identifies a user.
  320. =item C<username>
  321. The username that was provided by the user.
  322. =item C<bz_username>
  323. The username of this user inside of Bugzilla. Sometimes this differs from
  324. C<username>.
  325. =item C<password>
  326. The password provided by the user.
  327. =item C<realname>
  328. The real name of the user.
  329. =item C<extern_id>
  330. Some string that uniquely identifies the user in an external account
  331. source. If this C<extern_id> already exists in the database with
  332. a different username, the username will be *changed* to be the
  333. username specified in this C<$login_data>.
  334. That is, let's my extern_id is C<mkanat>. I already have an account
  335. in Bugzilla with the username of C<mkanat@foo.com>. But this time,
  336. when I log in, I have an extern_id of C<mkanat> and a C<username>
  337. of C<mkanat@bar.org>. So now, Bugzilla will automatically change my
  338. username to C<mkanat@bar.org> instead of C<mkanat@foo.com>.
  339. =item C<user>
  340. A L<Bugzilla::User> object representing the authenticated user.
  341. Note that C<Bugzilla::Auth::login> may modify this object at various points.
  342. =back