|
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 Aviat\AnimeClient\AnimeClient; |
|
20
|
|
|
use Aviat\AnimeClient\API\{ |
|
21
|
|
|
CacheTrait, |
|
22
|
|
|
Kitsu as K |
|
23
|
|
|
}; |
|
24
|
|
|
use Aviat\Ion\Di\{ContainerAware, ContainerInterface}; |
|
25
|
|
|
use Exception; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Kitsu API Authentication |
|
29
|
|
|
*/ |
|
30
|
|
|
class Auth { |
|
31
|
|
|
use CacheTrait; |
|
32
|
|
|
use ContainerAware; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Anime API Model |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Aviat\AnimeClient\API\Kitsu\Model |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $model; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Session object |
|
43
|
|
|
* |
|
44
|
|
|
* @var Aura\Session\Segment |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $segment; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor |
|
50
|
|
|
* |
|
51
|
|
|
* @param ContainerInterface $container |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(ContainerInterface $container) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setContainer($container); |
|
56
|
|
|
$this->setCache($container->get('cache')); |
|
57
|
|
|
$this->segment = $container->get('session') |
|
58
|
|
|
->getSegment(AnimeClient::SESSION_SEGMENT); |
|
59
|
|
|
$this->model = $container->get('kitsu-model'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Make the appropriate authentication call, |
|
64
|
|
|
* and save the resulting auth token if successful |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $password |
|
67
|
|
|
* @return boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public function authenticate($password) |
|
70
|
|
|
{ |
|
71
|
|
|
$config = $this->container->get('config'); |
|
72
|
|
|
$username = $config->get(['kitsu_username']); |
|
73
|
|
|
|
|
74
|
|
|
try |
|
75
|
|
|
{ |
|
76
|
|
|
$auth = $this->model->authenticate($username, $password); |
|
77
|
|
|
} |
|
78
|
|
|
catch (Exception $e) |
|
79
|
|
|
{ |
|
80
|
|
|
return FALSE; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (FALSE !== $auth) |
|
85
|
|
|
{ |
|
86
|
|
|
// Set the token in the cache for command line operations |
|
87
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY); |
|
88
|
|
|
$cacheItem->set($auth['access_token']); |
|
89
|
|
|
$cacheItem->save(); |
|
90
|
|
|
|
|
91
|
|
|
$this->segment->set('auth_token', $auth['access_token']); |
|
92
|
|
|
return TRUE; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return FALSE; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Check whether the current user is authenticated |
|
100
|
|
|
* |
|
101
|
|
|
* @return boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
public function is_authenticated() |
|
104
|
|
|
{ |
|
105
|
|
|
return ($this->get_auth_token() !== FALSE); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Clear authentication values |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function logout() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->segment->clear(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Retrieve the authentication token from the session |
|
120
|
|
|
* |
|
121
|
|
|
* @return string|false |
|
122
|
|
|
*/ |
|
123
|
|
|
public function get_auth_token() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->segment->get('auth_token', FALSE); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
// End of KitsuAuth.php |
This check looks for
@paramannotations 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.