token_authorization.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace phpnotify;
  3. use \TypeError as TypeError;
  4. /**
  5. * That class could be used to authorize by token.
  6. *
  7. * This could be used to authenticate by previously generated token, which
  8. * is most secure option in most cases.
  9. */
  10. class token_authorization extends authorization_method {
  11. /**
  12. * This store authorization token.
  13. * @var string
  14. */
  15. private string $token;
  16. /**
  17. * This create new authorization method.
  18. *
  19. * This create new authorization method from given authorization token.
  20. * It check that token is not blank and also triming it. When token is
  21. * empty, then raise TypeError.
  22. *
  23. * @throws TypeError When given token is empty.
  24. *
  25. * @param string $token Authorization token to use.
  26. */
  27. public function __construct(string $token) {
  28. $this->token = trim($token);
  29. if (strlen($this->token) === 0) {
  30. throw new TypeError('Authorization token could not being empty.');
  31. }
  32. }
  33. /**
  34. * When use authorization by token, header is required.
  35. *
  36. * @return bool It return true, because header is required.
  37. */
  38. public function is_header_required(): bool {
  39. return true;
  40. }
  41. /**
  42. * It return content of the header, with given token.
  43. *
  44. * @return string Content of the authorization header.
  45. */
  46. public function header_content(): string {
  47. return 'Bearer '.$this->token;
  48. }
  49. }