forked from rapidez/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Magento customer auth guard (rapidez#355)
- Loading branch information
1 parent
3b04145
commit 5c096b4
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters