Skip to content

Commit

Permalink
Extract generate token method (#488)
Browse files Browse the repository at this point in the history
* Extract generate token method

* Update comment

* Update comment

* Update HasApiTokens.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
mowangjuanzi and taylorotwell committed Dec 19, 2023
1 parent 4fc91ed commit 8c10436
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/HasApiTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ public function tokenCan(string $ability)
*/
public function createToken(string $name, array $abilities = ['*'], DateTimeInterface $expiresAt = null)
{
$plainTextToken = sprintf(
'%s%s%s',
config('sanctum.token_prefix', ''),
$tokenEntropy = Str::random(40),
hash('crc32b', $tokenEntropy)
);
$plainTextToken = $this->generateTokenString();

$token = $this->tokens()->create([
'name' => $name,
Expand All @@ -62,6 +57,21 @@ public function createToken(string $name, array $abilities = ['*'], DateTimeInte
return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
}

/**
* Generate the token string.
*
* @return string
*/
public function generateTokenString()
{
return sprintf(
'%s%s%s',
config('sanctum.token_prefix', ''),
$tokenEntropy = Str::random(40),
hash('crc32b', $tokenEntropy)
);
}

/**
* Get the access token currently associated with the user.
*
Expand Down

0 comments on commit 8c10436

Please sign in to comment.