HummingBirdAnimeClient/src/Auth/HummingbirdAuth.php

105 lines
2.0 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
2015-11-16 11:40:01 -05:00
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
2015-09-17 23:11:18 -04:00
namespace Aviat\AnimeClient\Auth;
use Aviat\Ion\Di\ContainerInterface;
2016-01-04 10:53:03 -05:00
use Aviat\AnimeClient\AnimeClient;
2015-09-17 23:11:18 -04:00
2015-10-09 14:34:55 -04:00
/**
* Hummingbird API Authentication
*/
2015-09-17 23:11:18 -04:00
class HummingbirdAuth {
use \Aviat\Ion\Di\ContainerAware;
/**
* Anime API Model
*
2015-10-16 12:53:55 -04:00
* @var \Aviat\AnimeClient\Model\API
2015-09-17 23:11:18 -04:00
*/
protected $model;
/**
* Session object
*
* @var Aura\Session\Segment
*/
2015-10-14 09:20:52 -04:00
protected $segment;
2015-09-17 23:11:18 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
2015-10-14 09:20:52 -04:00
$this->segment = $container->get('session')
2016-01-04 10:53:03 -05:00
->getSegment(AnimeClient::SESSION_SEGMENT);
2015-12-08 16:39:49 -05:00
$this->model = $container->get('api-model');
2015-09-17 23:11:18 -04:00
}
2015-10-09 14:34:55 -04:00
/**
* Make the appropriate authentication call,
* and save the resulting auth token if successful
*
* @param string $password
* @return boolean
*/
2015-10-14 09:20:52 -04:00
public function authenticate($password)
2015-09-17 23:11:18 -04:00
{
2015-10-16 12:53:55 -04:00
$username = $this->container->get('config')
->get('hummingbird_username');
2015-10-14 09:20:52 -04:00
$auth_token = $this->model->authenticate($username, $password);
if (FALSE !== $auth_token)
{
$this->segment->set('auth_token', $auth_token);
return TRUE;
}
return FALSE;
}
/**
* Check whether the current user is authenticated
2015-10-16 12:53:55 -04:00
*
2015-10-14 09:20:52 -04:00
* @return boolean
*/
public function is_authenticated()
{
return ($this->get_auth_token() !== FALSE);
}
/**
* Clear authentication values
*
* @return void
*/
2015-11-13 11:33:27 -05:00
public function logout()
2015-10-14 09:20:52 -04:00
{
$this->segment->clear();
2015-09-17 23:11:18 -04:00
}
2015-10-09 14:34:55 -04:00
/**
* Retrieve the authentication token from the session
*
2015-10-14 09:20:52 -04:00
* @return string|false
2015-10-09 14:34:55 -04:00
*/
2015-09-17 23:11:18 -04:00
public function get_auth_token()
{
2015-10-14 09:20:52 -04:00
return $this->segment->get('auth_token', FALSE);
2015-09-17 23:11:18 -04:00
}
}
// End of HummingbirdAuth.php