Skip to content

Commit 00407a0

Browse files
committed
feat: add ActionInterface::getType()
1 parent bcfd0a1 commit 00407a0

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/Authentication/Actions/ActionInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ public function handle(IncomingRequest $request);
3939
* @return Response|string
4040
*/
4141
public function verify(IncomingRequest $request);
42+
43+
/**
44+
* Returns the string type of the action class.
45+
* E.g., 'email_2fa', 'email_activate'.
46+
*/
47+
public function getType(): string;
4248
}

src/Authentication/Actions/Email2FA.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class Email2FA implements ActionInterface
1818
{
19+
private string $type = 'email_2fa';
20+
1921
/**
2022
* Displays the "Hey we're going to send you an number to your email"
2123
* message to the user with a prompt to continue.
@@ -61,7 +63,7 @@ public function handle(IncomingRequest $request)
6163
/** @var UserIdentityModel $identityModel */
6264
$identityModel = model(UserIdentityModel::class);
6365

64-
$identity = $identityModel->getIdentityByType($user->getAuthId(), 'email_2fa');
66+
$identity = $identityModel->getIdentityByType($user->getAuthId(), $this->type);
6567

6668
if (empty($identity)) {
6769
return redirect()->route('auth-action-show')->with('error', lang('Auth.need2FA'));
@@ -95,7 +97,7 @@ public function verify(IncomingRequest $request)
9597
$authenticator = auth('session')->getAuthenticator();
9698

9799
// Token mismatch? Let them try again...
98-
if (! $authenticator->checkAction('email_2fa', $token)) {
100+
if (! $authenticator->checkAction($this->type, $token)) {
99101
session()->setFlashdata('error', lang('Auth.invalid2FAToken'));
100102

101103
return view(setting('Auth.views')['action_email_2fa_verify']);
@@ -124,17 +126,22 @@ private function createIdentity(User $user): void
124126
$userIdentityModel = model(UserIdentityModel::class);
125127

126128
// Delete any previous activation identities
127-
$userIdentityModel->deleteIdentitiesByType($user->getAuthId(), 'email_2fa');
129+
$userIdentityModel->deleteIdentitiesByType($user->getAuthId(), $this->type);
128130

129131
// Create an identity for our 2fa hash
130132
$code = random_string('nozero', 6);
131133

132134
$userIdentityModel->insert([
133135
'user_id' => $user->getAuthId(),
134-
'type' => 'email_2fa',
136+
'type' => $this->type,
135137
'secret' => $code,
136138
'name' => 'login',
137139
'extra' => lang('Auth.need2FA'),
138140
]);
139141
}
142+
143+
public function getType(): string
144+
{
145+
return $this->type;
146+
}
140147
}

src/Authentication/Actions/EmailActivator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class EmailActivator implements ActionInterface
1515
{
16+
private string $type = 'email_activate';
17+
1618
/**
1719
* Shows the initial screen to the user telling them
1820
* that an email was just sent to them with a link
@@ -75,7 +77,7 @@ public function verify(IncomingRequest $request)
7577
$authenticator = auth('session')->getAuthenticator();
7678

7779
// No match - let them try again.
78-
if (! $authenticator->checkAction('email_activate', $token)) {
80+
if (! $authenticator->checkAction($this->type, $token)) {
7981
session()->setFlashdata('error', lang('Auth.invalidActivateToken'));
8082

8183
return view(setting('Auth.views')['action_email_activate_show']);
@@ -110,19 +112,24 @@ private function createIdentity(User $user): string
110112
/** @var UserIdentityModel $userIdentityModel */
111113
$userIdentityModel = model(UserIdentityModel::class);
112114

113-
$userIdentityModel->deleteIdentitiesByType($user->getAuthId(), 'email_activate');
115+
$userIdentityModel->deleteIdentitiesByType($user->getAuthId(), $this->type);
114116

115117
// Create an identity for our activation hash
116118
$code = random_string('nozero', 6);
117119

118120
$userIdentityModel->insert([
119121
'user_id' => $user->getAuthId(),
120-
'type' => 'email_activate',
122+
'type' => $this->type,
121123
'secret' => $code,
122124
'name' => 'register',
123125
'extra' => lang('Auth.needVerification'),
124126
]);
125127

126128
return $code;
127129
}
130+
131+
public function getType(): string
132+
{
133+
return $this->type;
134+
}
128135
}

0 commit comments

Comments
 (0)