Skip to content

Commit

Permalink
refactor(users): Move API token creation to method
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Apr 3, 2024
1 parent 49aa9f2 commit 68ecd10
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/Listeners/Auth/Cas/Cas.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function handleAuthenticate($event)
{
$user = new User;
$user->name = $cas->getAttribute('fullname');
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();

$attrs = $cas->getAttributes();
if (isset($attrs['puid']))
Expand Down Expand Up @@ -173,7 +173,7 @@ public function handleAuthenticate($event)

if (!$user->api_token)
{
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
$user->save();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Auth/Ldap/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ protected function getDatabaseUser(LdapUser $ldapuser): ?User
protected function createDatabaseUser(LdapUser $ldapuser): User
{
$user = new User;
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();

$userusername = new UserUsername;

Expand Down
2 changes: 1 addition & 1 deletion app/Modules/Users/Console/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle(): int

$user = new User;
$user->name = $name;
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
$user->password = Hash::make($password);

$user->setDefaultRole();
Expand Down
2 changes: 1 addition & 1 deletion app/Modules/Users/Console/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function handle(): void

/*if (!$user->api_token)
{
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
$update = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public function store(Request $request)
{
$user->setDefaultRole();
}
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
}
if (!$user->puid)
{
Expand Down
4 changes: 2 additions & 2 deletions app/Modules/Users/Http/Controllers/Site/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function callback(Request $request)
{
$user = new User;
$user->name = $cas->getAttribute('fullname');
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();

$attrs = $cas->getAttributes();
if (isset($attrs['puid']))
Expand Down Expand Up @@ -177,7 +177,7 @@ public function callback(Request $request)

if (!$user->api_token)
{
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
$user->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function store(Request $request): RedirectResponse

$user = new User;
$user->name = $request->input('name');
$user->api_token = Str::random(60);
$user->api_token = $user->generateApiToken();
$user->password = Hash::make($request->input('password'));

$user->setDefaultRole();
Expand Down
17 changes: 16 additions & 1 deletion app/Modules/Users/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public static function createFromUsername(string $username): User
}

$user->name = $user->name ?: $username;
$user->api_token = Str::random(60);
$user->api_token = $this->generateApiToken();

$user->setDefaultRole();

Expand All @@ -914,6 +914,21 @@ public static function createFromUsername(string $username): User
return $user;
}

/**
* Generate an API token
*
* @return string
*/
public function generateApiToken(): string
{
return sprintf(
'%s%s%s',
config('module.users.token_prefix', ''),
$tokenEntropy = Str::random(52),
hash('crc32b', $tokenEntropy)
);
}

/**
* Get user avatar
*
Expand Down

0 comments on commit 68ecd10

Please sign in to comment.