auth.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // This program is free software: you can redistribute it and/or modify
  2. // it under the terms of the GNU General Public License as published by
  3. // the Free Software Foundation, either version 3 of the License, or
  4. // (at your option) any later version.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License for more details.
  9. // You should have received a copy of the GNU General Public License
  10. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  11. pragma solidity >=0.4.23;
  12. interface DSAuthority {
  13. function canCall(
  14. address src, address dst, bytes4 sig
  15. ) external view returns (bool);
  16. }
  17. contract DSAuthEvents {
  18. event LogSetAuthority (address indexed authority);
  19. event LogSetOwner (address indexed owner);
  20. }
  21. contract DSAuth is DSAuthEvents {
  22. DSAuthority public authority;
  23. address public owner;
  24. constructor() public {
  25. owner = msg.sender;
  26. emit LogSetOwner(msg.sender);
  27. }
  28. function setOwner(address owner_)
  29. public
  30. auth
  31. {
  32. owner = owner_;
  33. emit LogSetOwner(owner);
  34. }
  35. function setAuthority(DSAuthority authority_)
  36. public
  37. auth
  38. {
  39. authority = authority_;
  40. emit LogSetAuthority(address(authority));
  41. }
  42. modifier auth {
  43. require(isAuthorized(msg.sender, msg.sig), "ds-auth-unauthorized");
  44. _;
  45. }
  46. function isAuthorized(address src, bytes4 sig) internal view returns (bool) {
  47. if (src == address(this)) {
  48. return true;
  49. } else if (src == owner) {
  50. return true;
  51. } else if (authority == DSAuthority(0)) {
  52. return false;
  53. } else {
  54. return authority.canCall(src, address(this), sig);
  55. }
  56. }
  57. }