Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// access_token is what we defined inside Auth::extend
// you can name this anything BUT should match with
// Auth::extend('HERE');
'driver' => 'api_key'
]
]
'driver' => 'api_key',
],
],
];
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateApiKeysTable extends Migration
{
Expand Down
4 changes: 1 addition & 3 deletions src/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class ApiKey extends Model
protected $with = ['user'];

protected $fillable = [
'user_id', 'type', 'api_key', 'name'
'user_id', 'type', 'api_key', 'name',
];

public function user()
{
return $this->belongsTo(User::class);

}

}
11 changes: 5 additions & 6 deletions src/ApiKeyGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace NrmlCo\LaravelApiKeys;

use Illuminate\Http\Request;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Http\Request;

class ApiKeyGuard implements Guard
{
Expand All @@ -31,19 +31,19 @@ public function __construct(UserProvider $provider, Request $request, $configura
*/
public function user()
{
if (!is_null($this->user)) {
if (! is_null($this->user)) {
return $this->user;
}
$user = null;
$apiKey = $this->getApiKeyForRequest();
if (!empty($apiKey)) {
if (! empty($apiKey)) {
// the token was found, how you want to pass?
$user = $this->provider->retrieveByToken($this->storageKey, $apiKey);
}

return $this->user = $user;
}


/**
* Get the apikey for the current request.
* @return string
Expand All @@ -60,6 +60,7 @@ public function getApiKeyForRequest()
if (empty($apiKey)) {
$apiKey = $this->request->bearerToken();
}

return $apiKey;
}

Expand All @@ -79,7 +80,5 @@ public function validate(array $credentials = [])
if ($this->provider->retrieveByCredentials($credentials)) {
return true;
}


}
}
42 changes: 30 additions & 12 deletions src/ApiKeyToUserProvider.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
<?php namespace NrmlCo\LaravelApiKeys;
use NrmlCo\LaravelApiKeys\ApiKey;
<?php

namespace NrmlCo\LaravelApiKeys;

use App\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;

class ApiKeyToUserProvider implements UserProvider
{
private $apiKey;
private $user;
public function __construct (User $user, ApiKey $apiKey) {

public function __construct(User $user, ApiKey $apiKey)
{
$this->user = $user;
$this->apiKey = $apiKey;
}
public function retrieveById ($identifier) {

public function retrieveById($identifier)
{
return $this->user->find($identifier);
}
public function retrieveByToken ($identifier, $apiKey) {

public function retrieveByToken($identifier, $apiKey)
{
$apiKey = $this->apiKey->with('user')->where($identifier, $apiKey)->first();
if($apiKey){
if ($apiKey) {
$apiKey->touch();
}

return $apiKey && $apiKey->user ? $apiKey->user : null;
}
public function updateRememberToken (Authenticatable $user, $token) {

public function updateRememberToken(Authenticatable $user, $token)
{
// update via remember token not necessary
}
public function retrieveByCredentials (array $credentials) {

public function retrieveByCredentials(array $credentials)
{
// implementation upto user.
// how he wants to implement -
// let's try to assume that the credentials ['username', 'password'] given
$user = $this->user;
$apiKey = $this->apiKey;
foreach ($credentials as $credentialKey => $credentialValue) {
if (!Str::contains($credentialKey, 'password')) {
if (! Str::contains($credentialKey, 'password')) {
$apiKey->where($credentialKey, $credentialValue);
}
}
$apiKey->first();

return $apiKey->user;
}
public function validateCredentials (Authenticatable $user, array $credentials) {

public function validateCredentials(Authenticatable $user, array $credentials)
{
$plain = $credentials['password'];

return app('hash')->check($plain, $user->getAuthPassword());
}
}
7 changes: 2 additions & 5 deletions src/ApiKeyType.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?php


namespace NrmlCo\LaravelApiKeys;


class ApiKeyType
{
public const SANDBOX='SANDBOX';
public const PRODUCTION='PRODUCTION';

public const SANDBOX = 'SANDBOX';
public const PRODUCTION = 'PRODUCTION';
}
3 changes: 2 additions & 1 deletion src/HasApiKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

trait HasApiKeys
{
public function apiKeys(){
public function apiKeys()
{
return $this->hasMany(ApiKey::class);
}
}
11 changes: 5 additions & 6 deletions src/LaravelApiKeys.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

namespace NrmlCo\LaravelApiKeys;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use NrmlCo\LaravelApiKeys\ApiKeyType;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;

class LaravelApiKeys
{
public static function create( $apiKeyType = null, $data = [])
public static function create($apiKeyType = null, $data = [])
{
if ($apiKeyType === null)
{
if ($apiKeyType === null) {
$apiKeyType = ApiKeyType::SANDBOX;
}

Expand All @@ -32,6 +30,7 @@ public static function register()
// automatically build the DI, put it as reference
$userProvider = app(ApiKeyToUserProvider::class);
$request = app('request');

return new ApiKeyGuard($userProvider, $request, $config);
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/LaravelApiKeysServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\ServiceProvider;


class LaravelApiKeysServiceProvider extends ServiceProvider
{
/**
Expand All @@ -30,8 +29,8 @@ public function register()
// automatically build the DI, put it as reference
$userProvider = app(ApiKeyToUserProvider::class);
$request = app('request');

return new ApiKeyGuard($userProvider, $request, $config);
});

}
}