| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace phpnotify;
- use \TypeError as TypeError;
- /**
- * That class could be used to authorize by token.
- *
- * This could be used to authenticate by previously generated token, which
- * is most secure option in most cases.
- */
- class token_authorization extends authorization_method {
- /**
- * This store authorization token.
- * @var string
- */
- private string $token;
- /**
- * This create new authorization method.
- *
- * This create new authorization method from given authorization token.
- * It check that token is not blank and also triming it. When token is
- * empty, then raise TypeError.
- *
- * @throws TypeError When given token is empty.
- *
- * @param string $token Authorization token to use.
- */
- public function __construct(string $token) {
- $this->token = trim($token);
- if (strlen($this->token) === 0) {
- throw new TypeError('Authorization token could not being empty.');
- }
- }
- /**
- * When use authorization by token, header is required.
- *
- * @return bool It return true, because header is required.
- */
- public function is_header_required(): bool {
- return true;
- }
- /**
- * It return content of the header, with given token.
- *
- * @return string Content of the authorization header.
- */
- public function header_content(): string {
- return 'Bearer '.$this->token;
- }
- }
|