Permalink
Browse files

add optional for custom auth

  • Loading branch information...
laurencei committed Feb 17, 2018
1 parent 6823d76 commit 939e9fcd7e963face3bcf91c6f1e053c39b8a1d0
@@ -2,6 +2,7 @@
namespace Eyewitness\Eye;
use Closure;
use ReflectionClass;
use Eyewitness\Eye\Api;
use Eyewitness\Eye\Logger;
@@ -24,6 +25,13 @@ class Eye
{
const EYE_VERSION = '3.0.0';
/**
* A callback that can be used to authenticate users.
*
* @var \Closure
*/
protected static $authUsing;
/**
* The Application witness.
*
@@ -450,4 +458,28 @@ public static function laravelVersionIs($operator, $version, $laravel = null)
return version_compare($laravel, $version, $operator);
}
/**
* Set an optional callback that can be used to authenticate users.
*
* @param \Closure $callback
* @return void
*/
public static function auth(Closure $callback)
{
static::$authUsing = $callback;
}
/**
* Determine if the given request can access Eyewitness.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
public static function check($request)
{
return (static::$authUsing ?: function () {
return false;
})($request);
}
}
@@ -15,7 +15,7 @@ class AuthController extends Controller
*/
public function login(Request $request)
{
if ($request->session()->has('eyewitness:auth')) {
if ($request->session()->has('eyewitness:auth') || Eye::check($request)) {
return redirect(route('eyewitness.dashboard').'#overview');
}
@@ -3,6 +3,7 @@
namespace Eyewitness\Eye\Http\Middleware;
use Closure;
use Eyewitness\Eye\Eye;
class Authenticate
{
@@ -15,10 +16,10 @@ class Authenticate
*/
public function handle($request, Closure $next)
{
if (! session('eyewitness:auth')) {
return redirect()->route('eyewitness.login')->withWarning('Sorry - you must login to Eyewitness first.');
if (session('eyewitness:auth') || app(Eye::class)->check($request)) {
return $next($request);
}
return $next($request);
return redirect()->route('eyewitness.login')->withWarning('Sorry - you must login to Eyewitness first.');
}
}
@@ -2,6 +2,7 @@
namespace Eyewitness\Eye\Test\Controllers;
use Eyewitness\Eye\Eye;
use Eyewitness\Eye\Test\TestCase;
class AuthControllerTest extends TestCase
@@ -65,4 +66,29 @@ public function test_authenticate_accepts_correct_details_and_logs_in()
$response->assertRedirect($this->home.'/dashboard#overview');
$response->assertSessionHas('eyewitness:auth', 1);
}
public function test_closure_auth_fails()
{
Eye::auth(function ($request) {
return false;
});
$response = $this->get($this->home.'/dashboard');
$response->assertRedirect($this->home);
$response->assertSessionMissing('eyewitness:auth');
$response->assertSessionHas('warning', 'Sorry - you must login to Eyewitness first.');
}
public function test_closure_auth_succeds()
{
Eye::auth(function ($request) {
return true;
});
$response = $this->get($this->home);
$response->assertSessionMissing('eyewitness:auth');
$response->assertRedirect($this->home.'/dashboard#overview');
}
}

0 comments on commit 939e9fc

Please sign in to comment.