2
2
3
3
namespace BoxedCode \Laravel \Auth \Device ;
4
4
5
- use BoxedCode \Laravel \Auth \Device \AuthBrokerResponse ;
6
5
use BoxedCode \Laravel \Auth \Device \Contracts \AuthBroker as BrokerContract ;
7
6
use BoxedCode \Laravel \Auth \Device \Contracts \HasDeviceAuthorizations ;
8
7
use Illuminate \Contracts \Events \Dispatcher as EventDispatcher ;
@@ -13,21 +12,21 @@ class AuthBroker implements BrokerContract
13
12
{
14
13
/**
15
14
* The configuration array.
16
- *
15
+ *
17
16
* @var array
18
17
*/
19
18
protected $ config ;
20
19
21
20
/**
22
21
* The event dispatcher instance.
23
- *
22
+ *
24
23
* @var \Illuminate\Contracts\Events\Dispatcher
25
24
*/
26
25
protected $ events ;
27
26
28
27
/**
29
28
* Create a new broker instance.
30
- *
29
+ *
31
30
* @param array $config
32
31
*/
33
32
public function __construct (array $ config = [])
@@ -37,11 +36,12 @@ public function __construct(array $config = [])
37
36
38
37
/**
39
38
* 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
39
+ *
40
+ * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
41
+ * @param string $fingerprint
42
+ * @param string $browser
43
+ * @param string $ip
44
+ *
45
45
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
46
46
*/
47
47
public function challenge (HasDeviceAuthorizations $ user , $ fingerprint , $ browser , $ ip )
@@ -57,29 +57,30 @@ public function challenge(HasDeviceAuthorizations $user, $fingerprint, $browser,
57
57
// Check that the device is not already authorized.
58
58
if ($ authorization = $ this ->findExistingVerifiedAuthorization ($ user , $ fingerprint )) {
59
59
return $ this ->respond (static ::DEVICE_ALREADY_AUTHORIZED , [
60
- 'authorization ' => $ authorization
60
+ 'authorization ' => $ authorization,
61
61
]);
62
62
}
63
63
64
64
// Create a new authorization.
65
65
$ authorization = $ this ->newAuthorization ($ user , $ fingerprint , $ browser , $ ip );
66
66
67
- // Send the request and verification token
67
+ // Send the request and verification token
68
68
$ user ->notify (new $ this ->config ['notification ' ]($ authorization ->verify_token , $ browser , $ ip ));
69
69
70
70
$ this ->event (new Events \Challenged ($ authorization ));
71
71
72
72
return $ this ->respond (static ::USER_CHALLENGED , [
73
- 'authorization ' => $ authorization
73
+ 'authorization ' => $ authorization,
74
74
]);
75
- }
75
+ }
76
76
77
77
/**
78
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
79
+ *
80
+ * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
81
+ * @param string $fingerprint
82
+ * @param string $token
83
+ *
83
84
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
84
85
*/
85
86
public function verifyAndAuthorize (HasDeviceAuthorizations $ user , $ fingerprint , $ token )
@@ -110,17 +111,18 @@ public function verifyAndAuthorize(HasDeviceAuthorizations $user, $fingerprint,
110
111
$ this ->event (new Events \Authorized ($ authorization ));
111
112
112
113
return $ this ->respond (static ::DEVICE_AUTHORIZED , [
113
- 'authorization ' => $ authorization
114
+ 'authorization ' => $ authorization,
114
115
]);
115
116
}
116
117
117
118
/**
118
119
* 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
120
+ *
121
+ * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
122
+ * @param string $fingerprint
123
+ * @param string $browser
124
+ * @param string $ip
125
+ *
124
126
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
125
127
*/
126
128
public function authorize (HasDeviceAuthorizations $ user , $ fingerprint , $ browser , $ ip )
@@ -133,23 +135,27 @@ public function authorize(HasDeviceAuthorizations $user, $fingerprint, $browser,
133
135
// Check the device is not already verified.
134
136
if ($ authorization = $ this ->findExistingVerifiedAuthorization ($ user , $ fingerprint )) {
135
137
return $ this ->respond (static ::DEVICE_ALREADY_AUTHORIZED , [
136
- 'authorization ' => $ authorization
138
+ 'authorization ' => $ authorization,
137
139
]);
138
140
}
139
141
140
142
// Create a new verified authorization.
141
143
$ authorization = $ this ->newAuthorization (
142
- $ user , $ fingerprint , $ browser , $ ip , $ verified_at = now ()
144
+ $ user ,
145
+ $ fingerprint ,
146
+ $ browser ,
147
+ $ ip ,
148
+ $ verified_at = now ()
143
149
);
144
150
145
151
return $ this ->respond (static ::DEVICE_AUTHORIZED , [
146
- 'authorization ' => $ authorization
152
+ 'authorization ' => $ authorization,
147
153
]);
148
154
}
149
155
150
156
/**
151
157
* Set the event dispatcher.
152
- *
158
+ *
153
159
* @param EventDispatcher $events
154
160
*/
155
161
public function setEventDispatcher (EventDispatcher $ events )
@@ -161,7 +167,7 @@ public function setEventDispatcher(EventDispatcher $events)
161
167
162
168
/**
163
169
* Get the event dispatcher.
164
- *
170
+ *
165
171
* @return \Illuminate\Contracts\Events\Dispatcher
166
172
*/
167
173
public function getEventDispatcher ()
@@ -178,15 +184,15 @@ protected function event()
178
184
{
179
185
if ($ this ->events ) {
180
186
call_user_func_array (
181
- [$ this ->events , 'dispatch ' ],
187
+ [$ this ->events , 'dispatch ' ],
182
188
func_get_args ()
183
189
);
184
190
}
185
191
}
186
192
187
193
/**
188
194
* Generate a new verification token.
189
- *
195
+ *
190
196
* @return string
191
197
*/
192
198
protected function newVerifyToken ()
@@ -196,40 +202,43 @@ protected function newVerifyToken()
196
202
197
203
/**
198
204
* 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
+ *
206
+ * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
207
+ * @param string $fingerprint
208
+ * @param string $browser
209
+ * @param string $ip
210
+ * @param DateTime|null $verified_at
211
+ *
205
212
* @return \BoxedCode\Laravel\Auth\Device\Contracts\DeviceAuthorization
206
213
*/
207
- protected function newAuthorization (HasDeviceAuthorizations $ user ,
208
- $ fingerprint ,
209
- $ browser ,
210
- $ ip ,
211
- $ verified_at = null
214
+ protected function newAuthorization (
215
+ HasDeviceAuthorizations $ user ,
216
+ $ fingerprint ,
217
+ $ browser ,
218
+ $ ip ,
219
+ $ verified_at = null
212
220
) {
213
221
$ algorithm = $ this ->config ['fingerprints ' ]['algorithm ' ];
214
222
215
223
$ fingerprintHash = hash ($ algorithm , $ fingerprint );
216
224
217
225
// Create the authorizations
218
226
return $ user ->deviceAuthorizations ()->create ([
219
- 'uuid ' => Str::uuid (),
220
- 'fingerprint ' => $ fingerprintHash ,
221
- 'browser ' => $ browser ,
222
- 'ip ' => $ ip ,
227
+ 'uuid ' => Str::uuid (),
228
+ 'fingerprint ' => $ fingerprintHash ,
229
+ 'browser ' => $ browser ,
230
+ 'ip ' => $ ip ,
223
231
'verify_token ' => $ token = $ this ->newVerifyToken (),
224
- 'verified_at ' => $ verified_at ,
232
+ 'verified_at ' => $ verified_at ,
225
233
]);
226
234
}
227
235
228
236
/**
229
237
* Find an existing verified verification by fingerprint.
230
- *
231
- * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
232
- * @param string $fingerprint
238
+ *
239
+ * @param \BoxedCode\Laravel\Auth\Device\Contracts\HasDeviceAuthorizations $user
240
+ * @param string $fingerprint
241
+ *
233
242
* @return \BoxedCode\Laravel\Auth\Device\Contracts\DeviceAuthorization
234
243
*/
235
244
protected function findExistingVerifiedAuthorization (HasDeviceAuthorizations $ user , $ fingerprint )
@@ -247,13 +256,14 @@ protected function findExistingVerifiedAuthorization(HasDeviceAuthorizations $us
247
256
248
257
/**
249
258
* Create a new broker response instance.
250
- *
251
- * @param string $outcome
252
- * @param array $payload
259
+ *
260
+ * @param string $outcome
261
+ * @param array $payload
262
+ *
253
263
* @return \BoxedCode\Laravel\Auth\Device\AuthBrokerResponse
254
264
*/
255
265
protected function respond ($ outcome , array $ payload = [])
256
266
{
257
267
return new AuthBrokerResponse ($ outcome , $ payload );
258
268
}
259
- }
269
+ }
0 commit comments