Skip to content

Commit

Permalink
Remove custom contract in favor of events
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbjr committed May 27, 2017
1 parent 65619b0 commit f5ba2bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 68 deletions.
17 changes: 1 addition & 16 deletions config/apiguard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/*
|--------------------------------------------------------------------------
| Key name
| Header Key
|--------------------------------------------------------------------------
|
| This is the name of the variable that will provide us the API key in the
Expand All @@ -13,21 +13,6 @@
*/
'header_key' => 'X-Authorization',

/*
|--------------------------------------------------------------------------
| Authentication Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to authenticate users. Example:
|
| Chrisbjr\ApiGuard\Auth\Sentinel:class
|
| You can set up your own authentication provider here by creating a class
| that implements the ApiGuardAuthContract interface.
|
*/
'auth' => null,

'models' => [

'api_key' => 'Chrisbjr\ApiGuard\Models\ApiKey',
Expand Down
17 changes: 0 additions & 17 deletions src/Auth/Contracts/ApiGuardAuthContract.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/Auth/Sentinel.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/Events/ApiKeyAuthenticated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Chrisbjr\ApiGuard\Events;

use Chrisbjr\ApiGuard\Models\ApiKey;
use Illuminate\Queue\SerializesModels;

class ApiKeyAuthenticated
{
use SerializesModels;

public $request;

public $apiKey;

/**
* Create a new event instance.
*
* @param $request
* @param ApiKey $apiKey
*/
public function __construct($request, ApiKey $apiKey)
{
$this->request = $request;
$this->apiKey = $apiKey;
}
}
14 changes: 6 additions & 8 deletions src/Http/Middleware/AuthenticateApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Chrisbjr\ApiGuard\Http\Middleware;

use Carbon\Carbon;
use Chrisbjr\ApiGuard\Events\ApiKeyAuthenticated;
use Chrisbjr\ApiGuard\Models\Device;
use Closure;

Expand Down Expand Up @@ -33,23 +34,20 @@ public function handle($request, Closure $next, $guard = null)
'last_ip_address' => $request->ip(),
]);

$user = $apiKey->apikeyable;

if (! empty(config('apiguard.auth'))) {
$apiGuardAuth = app(config('apiguard.auth'));
$apiGuardAuth->authenticate($user);
}
$apikeyable = $apiKey->apikeyable;

// Bind the user or object to the request
// By doing this, we can now get the specified user through the request object in the controller using:
// $request->user()
$request->setUserResolver(function () use ($user) {
return $user;
$request->setUserResolver(function () use ($apikeyable) {
return $apikeyable;
});

// Attach the apikey object to the request
$request->apiKey = $apiKey;

event(new ApiKeyAuthenticated($request, $apiKey));

return $next($request);
}

Expand Down

0 comments on commit f5ba2bb

Please sign in to comment.