Skip to content

Commit db28f8d

Browse files
committed
Comments and minor refactoring.
1 parent 54581a9 commit db28f8d

18 files changed

+648
-71
lines changed

src/AuthBroker.php

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,57 @@
1111

1212
class AuthBroker implements BrokerContract
1313
{
14+
/**
15+
* The configuration array.
16+
*
17+
* @var array
18+
*/
1419
protected $config;
1520

21+
/**
22+
* The event dispatcher instance.
23+
*
24+
* @var \Illuminate\Contracts\Events\Dispatcher
25+
*/
1626
protected $events;
1727

28+
/**
29+
* Create a new broker instance.
30+
*
31+
* @param array $config
32+
*/
1833
public function __construct(array $config = [])
1934
{
2035
$this->config = $config;
2136
}
2237

38+
/**
39+
* Send a challenge to the user with a verification link.
40+
*
41+
* @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
42+
* @param string $fingerprint
43+
* @param string $browser
44+
* @param string $ip
45+
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
46+
*/
2347
public function challenge(HasDeviceAuthorizations $user, $fingerprint, $browser, $ip)
2448
{
2549
// Flush all other pending authorizations for this user.
26-
$user->devices()->pending()->delete();
50+
$user->deviceAuthorizations()->pending()->delete();
2751

28-
if ($response = $this->findExistingAuthorization($user, $fingerprint)) {
29-
return $response;
52+
// Check that the user can authorize devices.
53+
if (!$user->canAuthorizeDevice()) {
54+
return $this->respond(static::USER_CANNOT_AUTHORIZE_DEVICES);
3055
}
3156

57+
// Check that the device is not already authorized.
58+
if ($authorization = $this->findExistingVerifiedAuthorization($user, $fingerprint)) {
59+
return $this->respond(static::DEVICE_ALREADY_AUTHORIZED, [
60+
'authorization' => $authorization
61+
]);
62+
}
63+
64+
// Create a new authorization.
3265
$authorization = $this->newAuthorization($user, $fingerprint, $browser, $ip);
3366

3467
// Send the request and verification token
@@ -41,10 +74,18 @@ public function challenge(HasDeviceAuthorizations $user, $fingerprint, $browser,
4174
]);
4275
}
4376

44-
public function verify(HasDeviceAuthorizations $user, $fingerprint, $token)
77+
/**
78+
* Verify the challenge and authorize the user.
79+
*
80+
* @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
81+
* @param string $fingerprint
82+
* @param string $token
83+
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
84+
*/
85+
public function verifyAndAuthorize(HasDeviceAuthorizations $user, $fingerprint, $token)
4586
{
4687
// Verify the token.
47-
if (empty($token) || !($authorization = $user->devices()->pending($token)->first())) {
88+
if (empty($token) || !($authorization = $user->deviceAuthorizations()->pending($token)->first())) {
4889
return $this->respond(static::INVALID_TOKEN);
4990
}
5091

@@ -61,22 +102,42 @@ public function verify(HasDeviceAuthorizations $user, $fingerprint, $token)
61102
return $this->respond(static::INVALID_FINGERPRINT);
62103
}
63104

105+
$this->event(new Events\Verified($authorization));
106+
64107
// Mark the authorization as verified
65108
$authorization->fill(['verified_at' => now()])->save();
66109

67-
$this->event(new Events\Verified($authorization));
110+
$this->event(new Events\Authorized($authorization));
68111

69-
return $this->respond(static::DEVICE_VERIFIED, [
112+
return $this->respond(static::DEVICE_AUTHORIZED, [
70113
'authorization' => $authorization
71114
]);
72115
}
73116

117+
/**
118+
* Authorize a device without verification.
119+
*
120+
* @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
121+
* @param string $fingerprint
122+
* @param string $browser
123+
* @param string $ip
124+
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
125+
*/
74126
public function authorize(HasDeviceAuthorizations $user, $fingerprint, $browser, $ip)
75127
{
76-
if ($response = $this->findExistingAuthorization($user, $fingerprint)) {
77-
return $response;
128+
// Check that the user can authorize devices.
129+
if (!$user->canAuthorizeDevice()) {
130+
return $this->respond(static::USER_CANNOT_AUTHORIZE_DEVICES);
131+
}
132+
133+
// Check the device is not already verified.
134+
if ($authorization = $this->findExistingVerifiedAuthorization($user, $fingerprint)) {
135+
return $this->respond(static::DEVICE_ALREADY_AUTHORIZED, [
136+
'authorization' => $authorization
137+
]);
78138
}
79139

140+
// Create a new verified authorization.
80141
$authorization = $this->newAuthorization(
81142
$user, $fingerprint, $browser, $ip, $verified_at = now()
82143
);
@@ -123,11 +184,26 @@ protected function event()
123184
}
124185
}
125186

187+
/**
188+
* Generate a new verification token.
189+
*
190+
* @return string
191+
*/
126192
protected function newVerifyToken()
127193
{
128194
return Str::random(40);
129195
}
130196

197+
/**
198+
* Create a new authorization record.
199+
*
200+
* @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
201+
* @param string $fingerprint
202+
* @param string $browser
203+
* @param string $ip
204+
* @param DateTime|null $verified_at
205+
* @return \BoxedCode\Laravel\Auth\Device\Contracts\DeviceAuthorization
206+
*/
131207
protected function newAuthorization(HasDeviceAuthorizations $user,
132208
$fingerprint,
133209
$browser,
@@ -139,7 +215,7 @@ protected function newAuthorization(HasDeviceAuthorizations $user,
139215
$fingerprintHash = hash($algorithm, $fingerprint);
140216

141217
// Create the authorizations
142-
return $user->devices()->create([
218+
return $user->deviceAuthorizations()->create([
143219
'uuid' => Str::uuid(),
144220
'fingerprint' => $fingerprintHash,
145221
'browser' => $browser,
@@ -149,19 +225,33 @@ protected function newAuthorization(HasDeviceAuthorizations $user,
149225
]);
150226
}
151227

152-
protected function findExistingAuthorization(HasDeviceAuthorizations $user, $fingerprint)
228+
/**
229+
* Find an existing verified verification by fingerprint.
230+
*
231+
* @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
232+
* @param string $fingerprint
233+
* @return \BoxedCode\Laravel\Auth\Device\Contracts\DeviceAuthorization
234+
*/
235+
protected function findExistingVerifiedAuthorization(HasDeviceAuthorizations $user, $fingerprint)
153236
{
154237
$algorithm = $this->config['fingerprints']['algorithm'];
155238

156239
$fingerprintHash = hash($algorithm, $fingerprint);
157240

158-
if ($authorization = $user->devices()->fingerprint($fingerprintHash)->first()) {
159-
return $this->respond(static::DEVICE_ALREADY_AUTHORIZED, [
160-
'authorization' => $authorization
161-
]);
241+
if ($authorization = $user->deviceAuthorizations()->veifiedFingerprint($fingerprintHash)->first()) {
242+
return $authorization;
162243
}
244+
245+
return false;
163246
}
164247

248+
/**
249+
* Create a new broker response instance.
250+
*
251+
* @param string $outcome
252+
* @param array $payload
253+
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
254+
*/
165255
protected function respond($outcome, array $payload = [])
166256
{
167257
return new AuthBrokerResponse($outcome, $payload);

0 commit comments

Comments
 (0)