auth-utils.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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('database.php');
  16. function check_web_auth($username, $token, $timestamp, $api_key, $sk) {
  17. // Validates authentication using a web services token
  18. global $adodb;
  19. // Using the valid_api_key function from nixtape/2.0/index.php would be appropriate here
  20. if (strlen($api_key) != 32) {
  21. return false;
  22. }
  23. $adodb->SetFetchMode(ADODB_FETCH_ASSOC); // this query should get the uniqueid and then return it on success
  24. $result = $adodb->GetOne('SELECT username FROM Auth WHERE '
  25. //. 'expires > ' . time() . ' AND ' // session keys have an infinite lifetime
  26. . 'sk = ' . $adodb->qstr($sk)
  27. );
  28. if (!$result) {
  29. // TODO: Log failures somewhere
  30. return false;
  31. }
  32. return $result == $username;
  33. }
  34. function check_standard_auth($username, $token, $timestamp) {
  35. // Validates authentication using a standard authentication token
  36. global $adodb;
  37. $adodb->SetFetchMode(ADODB_FETCH_ASSOC); // this query should get the uniqueid and then return it on success
  38. $pass = $adodb->GetOne('SELECT password FROM Users WHERE lower(username) = lower(' . $adodb->qstr($username) . ')');
  39. if (!$pass) {
  40. // TODO: Log failures somewhere
  41. return false;
  42. }
  43. $check_token = md5($pass . $timestamp);
  44. return $check_token == $token;
  45. }
  46. /**
  47. * Checks if the session is still valid. Assumes $sessionID is already quoted.
  48. */
  49. function check_session($sessionID) {
  50. global $adodb;
  51. $session = $adodb->GetOne('SELECT expires from Scrobble_Sessions WHERE sessionid = ' . $sessionID);
  52. if (!$session) {
  53. return(false);
  54. }
  55. return($session >= time());
  56. }