ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilSetupPasswordManager.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
11 private $encoderName;
12
15
21 public function __construct(array $config = [])
22 {
23 if (!empty($config)) {
24 foreach ($config as $key => $value) {
25 switch (strtolower($key)) {
26 case 'password_encoder':
27 $this->setEncoderName($value);
28 break;
29
30 case 'encoder_factory':
31 $this->setEncoderFactory($value);
32 break;
33 }
34 }
35 }
36
37 if (!$this->getEncoderName()) {
38 throw new \Exception(sprintf('"password_encoder" must be set in %s.', json_encode($config)));
39 }
40
41 if (!($this->getEncoderFactory() instanceof \ilSetupPasswordEncoderFactory)) {
42 throw new \ilUserException(sprintf(
43 '"encoder_factory" must be instance of \ilSetupPasswordEncoderFactory and set in %s.',
44 json_encode($config)
45 ));
46 }
47 }
48
53 {
55 }
56
61 {
62 $this->encoderFactory = $encoderFactory;
63 }
64
68 public function getEncoderName() : string
69 {
70 return $this->encoderName;
71 }
72
76 public function setEncoderName(string $encoderName) : void
77 {
78 $this->encoderName = $encoderName;
79 }
80
87 public function encodePassword(string $raw) : string
88 {
89 $encoder = $this->getEncoderFactory()->getEncoderByName($this->getEncoderName());
90
91 return $encoder->encodePassword($raw, '');
92 }
93
103 public function verifyPassword(string $encoded, string $raw, callable $passwordReHashCallback) : bool
104 {
105 $currentEncoder = $this->getEncoderFactory()->getFirstEncoderForEncodedPasswordAndMatchers(
106 $encoded,
107 [
108 'md5' => function ($encoded) {
109 return is_string($encoded) && strlen($encoded) === 32;
110 },
111 'bcryptphp' => function ($encoded) {
112 return is_string($encoded) && substr($encoded, 0, 4) === '$2y$' && strlen($encoded) === 60;
113 }
114 ]
115 );
116
117 if ($this->getEncoderName() != $currentEncoder->getName()) {
118 if ($currentEncoder->isPasswordValid($encoded, $raw, '')) {
119 $passwordReHashCallback($raw);
120
121 return true;
122 }
123 } else {
124 if ($currentEncoder->isPasswordValid($encoded, $raw, '')) {
125 if ($currentEncoder->requiresReencoding($encoded)) {
126 $passwordReHashCallback($raw);
127 }
128
129 return true;
130 }
131 }
132
133 return false;
134 }
135}
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.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68