UserPasswordPolicy.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Password policy checking for a user
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Check if a user's password complies with any password policies that apply to that
  24. * user, based on the user's group membership.
  25. * @since 1.26
  26. */
  27. class UserPasswordPolicy {
  28. /**
  29. * @var array
  30. */
  31. private $policies;
  32. /**
  33. * Mapping of statements to the function that will test the password for compliance. The
  34. * checking functions take the policy value, the user, and password, and return a Status
  35. * object indicating compliance.
  36. * @var array
  37. */
  38. private $policyCheckFunctions;
  39. /**
  40. * @param array $policies
  41. * @param array $checks mapping statement to its checking function. Checking functions are
  42. * called with the policy value for this user, the user object, and the password to check.
  43. */
  44. public function __construct( array $policies, array $checks ) {
  45. if ( !isset( $policies['default'] ) ) {
  46. throw new InvalidArgumentException(
  47. 'Must include a \'default\' password policy'
  48. );
  49. }
  50. $this->policies = $policies;
  51. foreach ( $checks as $statement => $check ) {
  52. if ( !is_callable( $check ) ) {
  53. throw new InvalidArgumentException(
  54. "Policy check functions must be callable. '$statement' isn't callable."
  55. );
  56. }
  57. $this->policyCheckFunctions[$statement] = $check;
  58. }
  59. }
  60. /**
  61. * Check if a password meets the effective password policy for a User.
  62. * @param User $user whose policy we are checking
  63. * @param string $password the password to check
  64. * @return Status error to indicate the password didn't meet the policy, or fatal to
  65. * indicate the user shouldn't be allowed to login. The status value will be an array,
  66. * potentially with the following keys:
  67. * - forceChange: do not allow the user to login without changing the password if invalid.
  68. * - suggestChangeOnLogin: prompt for a password change on login if the password is invalid.
  69. */
  70. public function checkUserPassword( User $user, $password ) {
  71. $effectivePolicy = $this->getPoliciesForUser( $user );
  72. return $this->checkPolicies(
  73. $user,
  74. $password,
  75. $effectivePolicy,
  76. $this->policyCheckFunctions
  77. );
  78. }
  79. /**
  80. * Check if a password meets the effective password policy for a User, using a set
  81. * of groups they may or may not belong to. This function does not use the DB, so can
  82. * be used in the installer.
  83. * @param User $user whose policy we are checking
  84. * @param string $password the password to check
  85. * @param array $groups list of groups to which we assume the user belongs
  86. * @return Status error to indicate the password didn't meet the policy, or fatal to
  87. * indicate the user shouldn't be allowed to login. The status value will be an array,
  88. * potentially with the following keys:
  89. * - forceChange: do not allow the user to login without changing the password if invalid.
  90. * - suggestChangeOnLogin: prompt for a password change on login if the password is invalid.
  91. */
  92. public function checkUserPasswordForGroups( User $user, $password, array $groups ) {
  93. $effectivePolicy = self::getPoliciesForGroups(
  94. $this->policies,
  95. $groups,
  96. $this->policies['default']
  97. );
  98. return $this->checkPolicies(
  99. $user,
  100. $password,
  101. $effectivePolicy,
  102. $this->policyCheckFunctions
  103. );
  104. }
  105. /**
  106. * @param User $user
  107. * @param string $password
  108. * @param array $policies
  109. * @param array $policyCheckFunctions
  110. * @return Status
  111. */
  112. private function checkPolicies( User $user, $password, $policies, $policyCheckFunctions ) {
  113. $status = Status::newGood( [] );
  114. $forceChange = false;
  115. $suggestChangeOnLogin = false;
  116. foreach ( $policies as $policy => $settings ) {
  117. if ( !isset( $policyCheckFunctions[$policy] ) ) {
  118. throw new DomainException( "Invalid password policy config. No check defined for '$policy'." );
  119. }
  120. if ( !is_array( $settings ) ) {
  121. // legacy format
  122. $settings = [ 'value' => $settings ];
  123. }
  124. if ( !array_key_exists( 'value', $settings ) ) {
  125. throw new DomainException( "Invalid password policy config. No value defined for '$policy'." );
  126. }
  127. $value = $settings['value'];
  128. /** @var StatusValue $policyStatus */
  129. $policyStatus = call_user_func(
  130. $policyCheckFunctions[$policy],
  131. $value,
  132. $user,
  133. $password
  134. );
  135. if ( !$policyStatus->isGood() ) {
  136. if ( !empty( $settings['forceChange'] ) ) {
  137. $forceChange = true;
  138. }
  139. if ( !empty( $settings['suggestChangeOnLogin'] ) ) {
  140. $suggestChangeOnLogin = true;
  141. }
  142. }
  143. $status->merge( $policyStatus );
  144. }
  145. if ( $status->isOK() ) {
  146. if ( $forceChange ) {
  147. $status->value['forceChange'] = true;
  148. } elseif ( $suggestChangeOnLogin ) {
  149. $status->value['suggestChangeOnLogin'] = true;
  150. }
  151. }
  152. return $status;
  153. }
  154. /**
  155. * Get the policy for a user, based on their group membership. Public so
  156. * UI elements can access and inform the user.
  157. * @param User $user
  158. * @return array the effective policy for $user
  159. */
  160. public function getPoliciesForUser( User $user ) {
  161. $effectivePolicy = self::getPoliciesForGroups(
  162. $this->policies,
  163. $user->getEffectiveGroups(),
  164. $this->policies['default']
  165. );
  166. Hooks::run( 'PasswordPoliciesForUser', [ $user, &$effectivePolicy ] );
  167. return $effectivePolicy;
  168. }
  169. /**
  170. * Utility function to get the effective policy from a list of policies, based
  171. * on a list of groups.
  172. * @param array $policies list of policies to consider
  173. * @param array $userGroups the groups from which we calculate the effective policy
  174. * @param array $defaultPolicy the default policy to start from
  175. * @return array effective policy
  176. */
  177. public static function getPoliciesForGroups( array $policies, array $userGroups,
  178. array $defaultPolicy
  179. ) {
  180. $effectivePolicy = $defaultPolicy;
  181. foreach ( $policies as $group => $policy ) {
  182. if ( in_array( $group, $userGroups ) ) {
  183. $effectivePolicy = self::maxOfPolicies(
  184. $effectivePolicy,
  185. $policies[$group]
  186. );
  187. }
  188. }
  189. return $effectivePolicy;
  190. }
  191. /**
  192. * Utility function to get a policy that is the most restrictive of $p1 and $p2. For
  193. * simplicity, we setup the policy values so the maximum value is always more restrictive.
  194. * It is also used recursively to merge settings within the same policy.
  195. * @param array $p1
  196. * @param array $p2
  197. * @return array containing the more restrictive values of $p1 and $p2
  198. */
  199. public static function maxOfPolicies( array $p1, array $p2 ) {
  200. $ret = [];
  201. $keys = array_merge( array_keys( $p1 ), array_keys( $p2 ) );
  202. foreach ( $keys as $key ) {
  203. if ( !isset( $p1[$key] ) ) {
  204. $ret[$key] = $p2[$key];
  205. } elseif ( !isset( $p2[$key] ) ) {
  206. $ret[$key] = $p1[$key];
  207. } elseif ( !is_array( $p1[$key] ) && !is_array( $p2[$key] ) ) {
  208. $ret[$key] = max( $p1[$key], $p2[$key] );
  209. } else {
  210. if ( !is_array( $p1[$key] ) ) {
  211. $p1[$key] = [ 'value' => $p1[$key] ];
  212. } elseif ( !is_array( $p2[$key] ) ) {
  213. $p2[$key] = [ 'value' => $p2[$key] ];
  214. }
  215. $ret[$key] = self::maxOfPolicies( $p1[$key], $p2[$key] );
  216. }
  217. }
  218. return $ret;
  219. }
  220. }