2015-03-20 18:30:45 -06:00
|
|
|
<?php
|
|
|
|
|
2015-04-19 08:52:39 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-04-19 08:52:39 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 18:30:45 -06:00
|
|
|
namespace CachetHQ\Cachet\Http\Middleware;
|
|
|
|
|
|
|
|
use CachetHQ\Cachet\Models\User;
|
|
|
|
use Closure;
|
2015-05-20 17:01:26 -05:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2015-03-20 18:30:45 -06:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2016-02-11 11:35:40 +00:00
|
|
|
use Illuminate\Http\Request;
|
2015-06-18 18:12:10 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2015-03-20 18:30:45 -06:00
|
|
|
|
2016-10-08 10:37:21 +01:00
|
|
|
/**
|
|
|
|
* This is the api authentication middleware class.
|
|
|
|
*
|
|
|
|
* @author Joseph Cohen <joe@alt-three.com>
|
|
|
|
* @author Graham Campbell <james@alt-three.com>
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
*/
|
2015-12-24 17:30:59 +00:00
|
|
|
class ApiAuthentication
|
2015-03-20 18:30:45 -06:00
|
|
|
{
|
2015-05-20 17:01:26 -05:00
|
|
|
/**
|
2015-06-10 14:03:31 +01:00
|
|
|
* The authentication guard instance.
|
2015-05-20 17:01:26 -05:00
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Auth\Guard
|
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/**
|
2015-12-24 17:30:59 +00:00
|
|
|
* Create a new api authentication middleware instance.
|
2015-05-20 17:01:26 -05:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth
|
2015-09-19 12:30:38 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2015-05-20 17:01:26 -05:00
|
|
|
*/
|
|
|
|
public function __construct(Guard $auth)
|
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:30:45 -06:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2015-12-24 17:30:59 +00:00
|
|
|
* @param bool $required
|
2015-03-20 18:30:45 -06:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-02-11 11:35:40 +00:00
|
|
|
public function handle(Request $request, Closure $next, $required = false)
|
2015-03-20 18:30:45 -06:00
|
|
|
{
|
2015-06-02 08:35:30 +01:00
|
|
|
if ($this->auth->guest()) {
|
|
|
|
if ($apiToken = $request->header('X-Cachet-Token')) {
|
|
|
|
try {
|
2015-06-17 14:06:18 +01:00
|
|
|
$this->auth->onceUsingId(User::findByApiToken($apiToken)->id);
|
2015-06-02 08:35:30 +01:00
|
|
|
} catch (ModelNotFoundException $e) {
|
2015-12-24 17:30:59 +00:00
|
|
|
if ($required) {
|
|
|
|
throw new HttpException(401);
|
|
|
|
}
|
2015-06-02 08:35:30 +01:00
|
|
|
}
|
2015-12-24 17:30:59 +00:00
|
|
|
} elseif ($required) {
|
2015-06-18 18:12:10 +01:00
|
|
|
throw new HttpException(401);
|
2015-03-20 18:30:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|