Skip to content

Commit

Permalink
little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanomatteo committed Oct 22, 2020
1 parent 2e2e0c7 commit 8601e00
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ $device = \DeviceTracker::detectFindAndUpdate();
\DeviceTracker::flagCurrentAsVerified();

// flag as verfified for a specific user
\DeviceTracker::flagAsVerified($device, $user);
\DeviceTracker::flagAsVerified($device, $user_id);

// flag as verfified for a specific user by device uuid
\DeviceTracker::flagAsVerifiedByUuid($device_uuid, $user_id);



// if you are using laravel/ui (classic scaffolding)
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// the device identifier cookie
'device_cookie' => 'device_uuid',

'session_key' => 'laravel-device-tracking',

// must implement: IvanoMatteo\LaravelDeviceTracking\DeviceHijackingDetector
'hijacking_detector' => DeviceHijackingDetectorDefault::class,
];
2 changes: 1 addition & 1 deletion src/DeviceHijackingDetectorDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function detect(Device $device, ?Model $user)
{
if($device->exists){ //exists in db
if ($device->isDirty('device_type')) {
return 'device_type missmatch';
return 'device_type mismatch';
}

// EXAMPLES --------------------
Expand Down
6 changes: 1 addition & 5 deletions src/Http/Middleware/DeviceDetectMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ public function handle($request, Closure $next)
/** @var LaravelDeviceTracking */
$ldt = resolve('laravel-device-tracking');

$sessionMd5 = session('laravel-device-tracking');
$currentMd5 = md5($request->userAgent() . $ldt->getCookieID());

if (!$sessionMd5 || $currentMd5 !== $sessionMd5) {
if ($ldt->checkSessionDeviceHash() === false) {
$ldt->detectFindAndUpdate();
session(['laravel-device-tracking' => $currentMd5]);
}

}
Expand Down
67 changes: 63 additions & 4 deletions src/LaravelDeviceTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use IvanoMatteo\LaravelDeviceTracking\Events\UserSeenFromNewDevice;
use IvanoMatteo\LaravelDeviceTracking\Events\UserSeenFromUnverifiedDevice;
use IvanoMatteo\LaravelDeviceTracking\Models\Device;
use IvanoMatteo\LaravelDeviceTracking\Models\DeviceUser;
use Symfony\Component\HttpKernel\Exception\HttpException;

class LaravelDeviceTracking
Expand Down Expand Up @@ -56,10 +57,14 @@ public function detect()

// other metadata
$data = [
'is_bot' => $isBot,
'version' => $browser->browserVersion(),
'engine' => $browser->browserEngine(),
'bot' => $isBot,
'ips' => request()->ips(),
'platform_family' => $browser->platformFamily(),
'platform_name' => $browser->platformName(),
'platform_version' => $browser->platformVersion(),
'device_model' => $browser->deviceModel(),
'ip_addresses' => request()->ips(),
'user_agent' => \Str::limit(request()->header('user-agent'), 512),
];

Expand All @@ -72,10 +77,55 @@ public function detect()
}


/**
* return true if match
* return false if not match
* return null if web guard is not logged in
*
* @return bool|null
*/
function checkSessionDeviceHash()
{
if (\Auth::guard('web')->check()) {

$sessionMd5 = session(config('laravel-device-tracking.session_key'));
$currentMd5 = md5(request()->userAgent() . $this->getCookieID());

if (!$sessionMd5 || $currentMd5 !== $sessionMd5) {
return false;
} else {
return true;
}
}

return null;
}

/**
* id web guard is logged in, this function will store
* the device hash in the session
*/
function setSessionDeviceHash()
{
if (\Auth::guard('web')->check()) {

$currentMd5 = md5(request()->userAgent() . $this->getCookieID());
session([config('laravel-device-tracking.session_key') => $currentMd5]);
}
}

/**
* retrieve the device identifier from cookie
* @return string
* */
function getCookieID()
{
return \Str::limit(request()->cookie(config('laravel-device-tracking.device_cookie')), 255, '');
}

/**
* set the device identifier cookie
*/
function setCookieID($id)
{
\Cookie::queue(\Cookie::forever(
Expand Down Expand Up @@ -116,10 +166,18 @@ function findCurrentDevice($orNew = false, $update = false)
}


function flagAsVerified(Device $device, $user)
function flagAsVerified(Device $device, $user_id)
{
$device->pivot()
->where('user_id', '=', $user)
->where('user_id', '=', $user_id)
->update(['verified_at' => now()]);
}
function flagAsVerifiedByUuid($device_uuid, $user)
{
DeviceUser::where('user_id', '=', $user)
->whereHas('device', function ($q) use ($device_uuid) {
$q->where('device_uuid', '=', $device_uuid);
})
->update(['verified_at' => now()]);
}

Expand Down Expand Up @@ -213,6 +271,7 @@ public function detectFindAndUpdate(bool $reDetectDevice = false)
}
}

$this->setSessionDeviceHash();
$this->setCookieID($this->currentDevice->device_uuid);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function touch()
/**
* @return string user class fqn
*/
private function getUserClass(){
static function getUserClass(){
$u = config('laravel-device-tracking.user_model');

if(!$u){
Expand All @@ -74,7 +74,7 @@ private function getUserClass(){

function user()
{
return $this->belongsToMany($this->getUserClass(), 'device_user')
return $this->belongsToMany(static::getUserClass(), 'device_user')
->using(DeviceUser::class)
->withPivot('verified_at')->withTimestamps();
}
Expand Down
8 changes: 7 additions & 1 deletion src/Models/DeviceUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace IvanoMatteo\LaravelDeviceTracking\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Pivot;

/**
Expand All @@ -26,5 +27,10 @@
*/
class DeviceUser extends Pivot
{

function device(){
return $this->belongsTo(Device::class);
}
function user(){
return $this->belongsTo(Device::getUserClass());
}
}

0 comments on commit 8601e00

Please sign in to comment.