[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/password/ -> PasswordFactory.php (source)

   1  <?php
   2  /**
   3   * Implements the Password class for the MediaWiki software.
   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  /**
  24   * Factory class for creating and checking Password objects
  25   *
  26   * @since 1.24
  27   */
  28  final class PasswordFactory {
  29      /**
  30       * The default PasswordHash type
  31       *
  32       * @var string
  33       * @see PasswordFactory::setDefaultType
  34       */
  35      private $default = '';
  36  
  37      /**
  38       * Mapping of password types to classes
  39       * @var array
  40       * @see PasswordFactory::register
  41       * @see Setup.php
  42       */
  43      private $types = array(
  44          '' => array( 'type' => '', 'class' => 'InvalidPassword' ),
  45      );
  46  
  47      /**
  48       * Register a new type of password hash
  49       *
  50       * @param string $type Unique type name for the hash
  51       * @param array $config Array of configuration options
  52       */
  53  	public function register( $type, array $config ) {
  54          $config['type'] = $type;
  55          $this->types[$type] = $config;
  56      }
  57  
  58      /**
  59       * Set the default password type
  60       *
  61       * @throws InvalidArgumentException If the type is not registered
  62       * @param string $type Password hash type
  63       */
  64  	public function setDefaultType( $type ) {
  65          if ( !isset( $this->types[$type] ) ) {
  66              throw new InvalidArgumentException( "Invalid password type $type." );
  67          }
  68          $this->default = $type;
  69      }
  70  
  71      /**
  72       * Initialize the internal static variables using the global variables
  73       *
  74       * @param Config $config Configuration object to load data from
  75       */
  76  	public function init( Config $config ) {
  77          foreach ( $config->get( 'PasswordConfig' ) as $type => $options ) {
  78              $this->register( $type, $options );
  79          }
  80  
  81          $this->setDefaultType( $config->get( 'PasswordDefault' ) );
  82      }
  83  
  84      /**
  85       * Get the list of types of passwords
  86       *
  87       * @return array
  88       */
  89  	public function getTypes() {
  90          return $this->types;
  91      }
  92  
  93      /**
  94       * Create a new Hash object from an existing string hash
  95       *
  96       * Parse the type of a hash and create a new hash object based on the parsed type.
  97       * Pass the raw hash to the constructor of the new object. Use InvalidPassword type
  98       * if a null hash is given.
  99       *
 100       * @param string|null $hash Existing hash or null for an invalid password
 101       * @return Password
 102       * @throws PasswordError If hash is invalid or type is not recognized
 103       */
 104  	public function newFromCiphertext( $hash ) {
 105          if ( $hash === null || $hash === false || $hash === '' ) {
 106              return new InvalidPassword( $this, array( 'type' => '' ), null );
 107          } elseif ( $hash[0] !== ':' ) {
 108              throw new PasswordError( 'Invalid hash given' );
 109          }
 110  
 111          $type = substr( $hash, 1, strpos( $hash, ':', 1 ) - 1 );
 112          if ( !isset( $this->types[$type] ) ) {
 113              throw new PasswordError( "Unrecognized password hash type $type." );
 114          }
 115  
 116          $config = $this->types[$type];
 117  
 118          return new $config['class']( $this, $config, $hash );
 119      }
 120  
 121      /**
 122       * Make a new default password of the given type.
 123       *
 124       * @param string $type Existing type
 125       * @return Password
 126       * @throws PasswordError If hash is invalid or type is not recognized
 127       */
 128  	public function newFromType( $type ) {
 129          if ( !isset( $this->types[$type] ) ) {
 130              throw new PasswordError( "Unrecognized password hash type $type." );
 131          }
 132  
 133          $config = $this->types[$type];
 134  
 135          return new $config['class']( $this, $config );
 136      }
 137  
 138      /**
 139       * Create a new Hash object from a plaintext password
 140       *
 141       * If no existing object is given, make a new default object. If one is given, clone that
 142       * object. Then pass the plaintext to Password::crypt().
 143       *
 144       * @param string $password Plaintext password
 145       * @param Password|null $existing Optional existing hash to get options from
 146       * @return Password
 147       */
 148  	public function newFromPlaintext( $password, Password $existing = null ) {
 149          if ( $existing === null ) {
 150              $config = $this->types[$this->default];
 151              $obj = new $config['class']( $this, $config );
 152          } else {
 153              $obj = clone $existing;
 154          }
 155  
 156          $obj->crypt( $password );
 157  
 158          return $obj;
 159      }
 160  
 161      /**
 162       * Determine whether a password object needs updating
 163       *
 164       * Check whether the given password is of the default type. If it is,
 165       * pass off further needsUpdate checks to Password::needsUpdate.
 166       *
 167       * @param Password $password
 168       *
 169       * @return bool True if needs update, false otherwise
 170       */
 171  	public function needsUpdate( Password $password ) {
 172          if ( $password->getType() !== $this->default ) {
 173              return true;
 174          } else {
 175              return $password->needsUpdate();
 176          }
 177      }
 178  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1