ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSetupPasswordManager.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
13 private $encoderName;
14
19
25 public function __construct(array $config = [])
26 {
27 if (!empty($config)) {
28 foreach ($config as $key => $value) {
29 switch (strtolower($key)) {
30 case 'password_encoder':
31 $this->setEncoderName($value);
32 break;
33
34 case 'encoder_factory':
35 $this->setEncoderFactory($value);
36 break;
37 }
38 }
39 }
40
41 if (!$this->getEncoderName()) {
42 throw new \Exception(sprintf('"password_encoder" must be set in %s.', json_encode($config)));
43 }
44
45 if (!($this->getEncoderFactory() instanceof \ilSetupPasswordEncoderFactory)) {
46 throw new \ilUserException(sprintf('"encoder_factory" must be instance of \ilSetupPasswordEncoderFactory and set in %s.', json_encode($config)));
47 }
48 }
49
54 {
56 }
57
62 {
63 $this->encoderFactory = $encoderFactory;
64 }
65
69 public function getEncoderName() : string
70 {
71 return $this->encoderName;
72 }
73
77 public function setEncoderName(string $encoderName)
78 {
79 $this->encoderName = $encoderName;
80 }
81
88 public function encodePassword(string $raw) : string
89 {
90 $encoder = $this->getEncoderFactory()->getEncoderByName($this->getEncoderName());
91
92 return $encoder->encodePassword($raw, '');
93 }
94
104 public function verifyPassword(string $encoded, string $raw, callable $passwordReHashCallback) : bool
105 {
106 $currentEncoder = $this->getEncoderFactory()->getFirstEncoderForEncodedPasswordAndMatchers(
107 $encoded,
108 [
109 'md5' => function ($encoded) {
110 return is_string($encoded) && strlen($encoded) === 32;
111 },
112 'bcryptphp' => function ($encoded) {
113 return is_string($encoded) && substr($encoded, 0, 4) === '$2y$' && strlen($encoded) === 60;
114 }
115 ]
116 );
117
118 if ($this->getEncoderName() != $currentEncoder->getName()) {
119 if ($currentEncoder->isPasswordValid($encoded, $raw, '')) {
120 $passwordReHashCallback($raw);
121 return true;
122 }
123 } elseif ($currentEncoder->isPasswordValid($encoded, $raw, '')) {
124 if ($currentEncoder->requiresReencoding($encoded)) {
125 $passwordReHashCallback($raw);
126 }
127
128 return true;
129 }
130
131 return false;
132 }
133}
An exception for terminatinating execution or to throw for unit testing.
Class ilSetupPasswordManager.
verifyPassword(string $encoded, string $raw, callable $passwordReHashCallback)
Verifies if the passed raw password matches the encoded one, based on the current encoding strategy.
__construct(array $config=[])
ilSetupPasswordManager constructor.
setEncoderFactory(\ilSetupPasswordEncoderFactory $encoderFactory)
encodePassword(string $raw)
Encodes the raw password based on the relevant encoding strategy.
$key
Definition: croninfo.php:18
$config
Definition: bootstrap.php:15