Skip to content

Commit

Permalink
Magento customer auth guard (rapidez#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Oct 10, 2023
1 parent 3b04145 commit 5c096b4
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
'attribute' => Rapidez\Core\Models\Attribute::class,
'product' => Rapidez\Core\Models\Product::class,
'category' => Rapidez\Core\Models\Category::class,
'oauth_token' => Rapidez\Core\Models\OauthToken::class,
'customer' => Rapidez\Core\Models\Customer::class,
'config' => Rapidez\Core\Models\Config::class,
'option_swatch' => Rapidez\Core\Models\OptionSwatch::class,
'option_value' => Rapidez\Core\Models\OptionValue::class,
Expand Down
48 changes: 48 additions & 0 deletions src/Auth/MagentoCustomerTokenGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rapidez\Core\Auth;

use Illuminate\Auth\TokenGuard;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class MagentoCustomerTokenGuard extends TokenGuard implements Guard
{
/**
* Get the currently authenticated user.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
// If we've already retrieved the user for the current request we can just
// return it back immediately. We do not want to fetch the user data on
// every call to this method because that would be tremendously slow.
if (! is_null($this->user)) {
return $this->user;
}

$token = $this->getTokenForRequest();

return $this->user = empty($token) ? null : $this->retrieveByToken($token);
}

/**
* Validate a user's credentials.
*
* @return bool
*/
public function validate(array $credentials = [])
{
if (empty($credentials[$this->inputKey])) {
return false;
}

return (bool) $this->retrieveByToken($credentials[$this->inputKey]);
}

protected function retrieveByToken($token)
{
return config('rapidez.models.customer')::whereToken($token)->first();
}
}
50 changes: 50 additions & 0 deletions src/Models/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rapidez\Core\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Builder;
use Rapidez\Core\Actions\DecodeJwt;

class Customer extends Model implements AuthenticatableContract
{
use Authenticatable;

protected $primaryKey = 'entity_id';

protected $table = 'customer_entity';

protected $hidden = [
'password_hash',
'rp_token',
'rp_token_created_at',
'confirmation',
];

public function oauthTokens()
{
return $this->hasMany(config('rapidez.models.oauth_token'), 'customer_id');
}

public function getRememberTokenName()
{
return '';
}

public function scopeWhereToken(Builder $query, string $token)
{
$query->when(
DecodeJwt::isJwt($token),
fn (Builder $query) => $query
->where(
$this->qualifyColumn('customer_id'),
DecodeJwt::decode($token)
->claims()
->get('uid')
),
fn (Builder $query) => $query
->whereHas('oauthTokens', fn (Builder $query) => $query->where('token', $token))
);
}
}
18 changes: 18 additions & 0 deletions src/Models/OauthToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rapidez\Core\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;

class OauthToken extends Model
{
protected $table = 'oauth_token';

protected static function booting(): void
{
static::addGlobalScope('active', function (Builder $builder) {
$builder
->where('revoked', 0);
});
}
}
19 changes: 19 additions & 0 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rapidez\Core;

use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
Expand All @@ -12,6 +13,7 @@
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use Rapidez\Core\Auth\MagentoCustomerTokenGuard;
use Rapidez\Core\Commands\IndexCategoriesCommand;
use Rapidez\Core\Commands\IndexProductsCommand;
use Rapidez\Core\Commands\InstallCommand;
Expand All @@ -36,6 +38,7 @@ class RapidezServiceProvider extends ServiceProvider
public function boot()
{
$this
->bootAuth()
->bootCommands()
->bootPublishables()
->bootRoutes()
Expand All @@ -57,6 +60,22 @@ public function register()
->registerExceptionHandlers();
}

protected function bootAuth(): self
{
auth()->extend('magento-customer', function (Application $app, string $name, array $config) {
return new MagentoCustomerTokenGuard(auth()->createUserProvider($config['provider']), request(), 'token', 'token');
});

config([
'auth.guards.magento-customer' => [
'driver' => 'magento-customer',
'provider' => 'users',
],
]);

return $this;
}

protected function bootCommands(): self
{
$this->commands([
Expand Down

0 comments on commit 5c096b4

Please sign in to comment.