PDOBasicAuth.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Sabre\DAV\Auth\Backend;
  3. /**
  4. * This is an authentication backend that uses a database to manage passwords.
  5. *
  6. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  7. * @license http://sabre.io/license/ Modified BSD License
  8. */
  9. class PDOBasicAuth extends AbstractBasic
  10. {
  11. /**
  12. * Reference to PDO connection.
  13. *
  14. * @var PDO
  15. */
  16. protected $pdo;
  17. /**
  18. * PDO table name we'll be using.
  19. *
  20. * @var string
  21. */
  22. protected $tableName;
  23. /**
  24. * PDO digest column name we'll be using
  25. * (i.e. digest, password, password_hash).
  26. *
  27. * @var string
  28. */
  29. protected $digestColumn;
  30. /**
  31. * PDO uuid(unique user identifier) column name we'll be using
  32. * (i.e. username, email).
  33. *
  34. * @var string
  35. */
  36. protected $uuidColumn;
  37. /**
  38. * Digest prefix:
  39. * if the backend you are using for is prefixing
  40. * your password hashes set this option to your prefix to
  41. * cut it off before verfiying.
  42. *
  43. * @var string
  44. */
  45. protected $digestPrefix;
  46. /**
  47. * Creates the backend object.
  48. *
  49. * If the filename argument is passed in, it will parse out the specified file fist.
  50. */
  51. public function __construct(\PDO $pdo, array $options = [])
  52. {
  53. $this->pdo = $pdo;
  54. if (isset($options['tableName'])) {
  55. $this->tableName = $options['tableName'];
  56. } else {
  57. $this->tableName = 'users';
  58. }
  59. if (isset($options['digestColumn'])) {
  60. $this->digestColumn = $options['digestColumn'];
  61. } else {
  62. $this->digestColumn = 'digest';
  63. }
  64. if (isset($options['uuidColumn'])) {
  65. $this->uuidColumn = $options['uuidColumn'];
  66. } else {
  67. $this->uuidColumn = 'username';
  68. }
  69. if (isset($options['digestPrefix'])) {
  70. $this->digestPrefix = $options['digestPrefix'];
  71. }
  72. }
  73. /**
  74. * Validates a username and password.
  75. *
  76. * This method should return true or false depending on if login
  77. * succeeded.
  78. *
  79. * @param string $username
  80. * @param string $password
  81. *
  82. * @return bool
  83. */
  84. public function validateUserPass($username, $password)
  85. {
  86. $stmt = $this->pdo->prepare('SELECT '.$this->digestColumn.' FROM '.$this->tableName.' WHERE '.$this->uuidColumn.' = ?');
  87. $stmt->execute([$username]);
  88. $result = $stmt->fetchAll();
  89. if (!count($result)) {
  90. return false;
  91. } else {
  92. $digest = $result[0][$this->digestColumn];
  93. if (isset($this->digestPrefix)) {
  94. $digest = substr($digest, strlen($this->digestPrefix));
  95. }
  96. if (password_verify($password, $digest)) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. }
  102. }