oauth_ro_auth.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright (c) 2011, 2012 Michiel de Jong <michiel@unhosted.org>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Connector_Sabre_Auth_ro_oauth extends Sabre_DAV_Auth_Backend_AbstractBasic {
  9. private $validTokens;
  10. private $category;
  11. public function __construct($validTokensArg, $categoryArg) {
  12. $this->validTokens = $validTokensArg;
  13. $this->category = $categoryArg;
  14. }
  15. /**
  16. * Validates a username and password
  17. *
  18. * This method should return true or false depending on if login
  19. * succeeded.
  20. *
  21. * @return bool
  22. */
  23. protected function validateUserPass($username, $password){
  24. //always give read-only:
  25. if(($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
  26. || (isset($this->validTokens[$password]))
  27. || (($_SERVER['REQUEST_METHOD'] == 'GET') && ($this->category == 'public'))
  28. ) {
  29. OC_Util::setUpFS();
  30. return true;
  31. } else {
  32. //var_export($_SERVER);
  33. //var_export($this->validTokens);
  34. //die('not getting in with "'.$username.'"/"'.$password.'"!');
  35. return false;
  36. }
  37. }
  38. //overwriting this to make it not automatically fail if no auth header is found:
  39. public function authenticate(Sabre_DAV_Server $server,$realm) {
  40. $auth = new Sabre_HTTP_BearerAuth();
  41. $auth->setHTTPRequest($server->httpRequest);
  42. $auth->setHTTPResponse($server->httpResponse);
  43. $auth->setRealm($realm);
  44. $userpass = $auth->getUserPass();
  45. if (!$userpass) {
  46. if(($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
  47. ||(($_SERVER['REQUEST_METHOD'] == 'GET') && ($this->category == 'public'))
  48. ) {
  49. $userpass = array('', '');
  50. } else {
  51. $auth->requireLogin();
  52. throw new Sabre_DAV_Exception_NotAuthenticated('No basic authentication headers were found');
  53. }
  54. }
  55. // Authenticates the user
  56. if (!$this->validateUserPass($userpass[0],$userpass[1])) {
  57. $auth->requireLogin();
  58. throw new Sabre_DAV_Exception_NotAuthenticated('Username or password does not match');
  59. }
  60. $this->currentUser = $userpass[0];
  61. return true;
  62. }
  63. }