1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Anime List Client |
4
|
|
|
* |
5
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* PHP version 7 |
8
|
|
|
* |
9
|
|
|
* @package AnimeListClient |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @version 4.0 |
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\API\Kitsu; |
18
|
|
|
|
19
|
|
|
use const Aviat\AnimeClient\SESSION_SEGMENT; |
20
|
|
|
|
21
|
|
|
use Aviat\AnimeClient\AnimeClient; |
22
|
|
|
use Aviat\AnimeClient\API\{ |
23
|
|
|
CacheTrait, |
24
|
|
|
Kitsu as K |
25
|
|
|
}; |
26
|
|
|
use Aviat\Ion\Di\{ContainerAware, ContainerInterface}; |
27
|
|
|
use Exception; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Kitsu API Authentication |
31
|
|
|
*/ |
32
|
|
|
class Auth { |
33
|
|
|
use CacheTrait; |
34
|
|
|
use ContainerAware; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Anime API Model |
38
|
|
|
* |
39
|
|
|
* @var \Aviat\AnimeClient\API\Kitsu\Model |
40
|
|
|
*/ |
41
|
|
|
protected $model; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Session object |
45
|
|
|
* |
46
|
|
|
* @var Aura\Session\Segment |
47
|
|
|
*/ |
48
|
|
|
protected $segment; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor |
52
|
|
|
* |
53
|
|
|
* @param ContainerInterface $container |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct(ContainerInterface $container) |
56
|
|
|
{ |
57
|
|
|
$this->setContainer($container); |
58
|
|
|
$this->setCache($container->get('cache')); |
59
|
|
|
$this->segment = $container->get('session') |
60
|
|
|
->getSegment(SESSION_SEGMENT); |
61
|
|
|
$this->model = $container->get('kitsu-model'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Make the appropriate authentication call, |
66
|
|
|
* and save the resulting auth token if successful |
67
|
|
|
* |
68
|
|
|
* @param string $password |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
public function authenticate($password) |
72
|
|
|
{ |
73
|
|
|
$config = $this->container->get('config'); |
74
|
|
|
$username = $config->get(['kitsu_username']); |
75
|
|
|
|
76
|
|
|
try |
77
|
|
|
{ |
78
|
|
|
$auth = $this->model->authenticate($username, $password); |
79
|
|
|
} |
80
|
|
|
catch (Exception $e) |
81
|
|
|
{ |
82
|
|
|
return FALSE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
if (FALSE !== $auth) |
87
|
|
|
{ |
88
|
|
|
// Set the token in the cache for command line operations |
89
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY); |
90
|
|
|
$cacheItem->set($auth['access_token']); |
91
|
|
|
$cacheItem->save(); |
92
|
|
|
|
93
|
|
|
$this->segment->set('auth_token', $auth['access_token']); |
94
|
|
|
return TRUE; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return FALSE; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check whether the current user is authenticated |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function is_authenticated() |
106
|
|
|
{ |
107
|
|
|
return ($this->get_auth_token() !== FALSE); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Clear authentication values |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function logout() |
116
|
|
|
{ |
117
|
|
|
$this->segment->clear(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Retrieve the authentication token from the session |
122
|
|
|
* |
123
|
|
|
* @return string|false |
124
|
|
|
*/ |
125
|
|
|
public function get_auth_token() |
126
|
|
|
{ |
127
|
|
|
return $this->segment->get('auth_token', FALSE); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
// End of KitsuAuth.php |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.