auth-utils.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* GNUkebox -- a free software server for recording your listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
  16. require_once($install_path . 'database.php');
  17. /**
  18. * Validate authentication using a web services token.
  19. *
  20. * @param string $username User name.
  21. * @param string $api_key 32 character API key.
  22. * @param string $sk Web services token.
  23. * @return bool
  24. */
  25. function check_web_auth($username, $api_key, $sk) {
  26. global $adodb;
  27. // Using the valid_api_key function from nixtape/2.0/index.php would be appropriate here
  28. if (strlen($api_key) != 32) {
  29. return false;
  30. }
  31. $query = 'SELECT username FROM Auth WHERE sk = ?';
  32. $params = array($sk);
  33. $result = $adodb->GetOne($query, $params);
  34. if (!$result) {
  35. // TODO: Log failures somewhere
  36. return false;
  37. }
  38. return $result == $username;
  39. }
  40. /**
  41. * Validates authentication using a standard authentication token.
  42. *
  43. * @param string $username User name.
  44. * @param string $token Token.
  45. * @param int $timestamp Timestamp in seconds since Epoch.
  46. * @return bool
  47. */
  48. function check_standard_auth($username, $token, $timestamp) {
  49. // Validates authentication using a standard authentication token
  50. global $adodb;
  51. $query = 'SELECT password FROM Users WHERE lower(username) = lower(?)';
  52. $params = array($username);
  53. $pass = $adodb->GetOne($query, $params);
  54. if (!$pass) {
  55. // TODO: Log failures somewhere
  56. return false;
  57. }
  58. $check_token = md5($pass . $timestamp);
  59. return $check_token == $token;
  60. }
  61. /**
  62. * Checks if the session is still valid.
  63. *
  64. * @param $sessionid Scrobble session id.
  65. * @return bool True if session exists and is still valid.
  66. */
  67. function check_session($sessionid) {
  68. global $adodb;
  69. $query = 'SELECT expires FROM Scrobble_Sessions WHERE sessionid = ? AND expires >= ?';
  70. $params = array($sessionid, time());
  71. $session = $adodb->GetOne($query, $params);
  72. return (bool) $session;
  73. }