Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Auth.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * Base for authentication modules
  12. */
  13. interface Auth
  14. {
  15. /**
  16. * Authentication module's name, e.g., "Login"
  17. *
  18. * @return string
  19. */
  20. public function getName();
  21. /**
  22. * Authenticates user
  23. *
  24. * @return AuthResult
  25. */
  26. public function authenticate();
  27. /**
  28. * Authenticates the user and initializes the session.
  29. */
  30. public function initSession($login, $md5Password, $rememberMe);
  31. /**
  32. * Accessor to set authentication token. If set, you can authenticate the tokenAuth by calling the authenticate()
  33. * method afterwards.
  34. *
  35. * @param string $token_auth authentication token
  36. */
  37. public function setTokenAuth($token_auth);
  38. /**
  39. * Accessor to set login name
  40. *
  41. * @param string $login user login
  42. */
  43. public function setLogin($login);
  44. }
  45. /**
  46. * Authentication result
  47. *
  48. */
  49. class AuthResult
  50. {
  51. const FAILURE = 0;
  52. const SUCCESS = 1;
  53. const SUCCESS_SUPERUSER_AUTH_CODE = 42;
  54. /**
  55. * token_auth parameter used to authenticate in the API
  56. *
  57. * @var string
  58. */
  59. protected $tokenAuth = null;
  60. /**
  61. * The login used to authenticate.
  62. *
  63. * @var string
  64. */
  65. protected $login = null;
  66. /**
  67. * The authentication result code. Can be self::FAILURE, self::SUCCESS, or
  68. * self::SUCCESS_SUPERUSER_AUTH_CODE.
  69. *
  70. * @var int
  71. */
  72. protected $code = null;
  73. /**
  74. * Constructor for AuthResult
  75. *
  76. * @param int $code
  77. * @param string $login identity
  78. * @param string $tokenAuth
  79. */
  80. public function __construct($code, $login, $tokenAuth)
  81. {
  82. $this->code = (int)$code;
  83. $this->login = $login;
  84. $this->tokenAuth = $tokenAuth;
  85. }
  86. /**
  87. * Returns the login used to authenticate.
  88. *
  89. * @return string
  90. */
  91. public function getIdentity()
  92. {
  93. return $this->login;
  94. }
  95. /**
  96. * Returns the token_auth to authenticate the current user in the API
  97. *
  98. * @return string
  99. */
  100. public function getTokenAuth()
  101. {
  102. return $this->tokenAuth;
  103. }
  104. /**
  105. * Returns the authentication result code.
  106. *
  107. * @return int
  108. */
  109. public function getCode()
  110. {
  111. return $this->code;
  112. }
  113. /**
  114. * Returns true if the user has Super User access, false otherwise.
  115. *
  116. * @return bool
  117. */
  118. public function hasSuperUserAccess()
  119. {
  120. return $this->getCode() == self::SUCCESS_SUPERUSER_AUTH_CODE;
  121. }
  122. /**
  123. * Returns true if this result was successfully authentication.
  124. *
  125. * @return bool
  126. */
  127. public function wasAuthenticationSuccessful()
  128. {
  129. return $this->code > self::FAILURE;
  130. }
  131. }