Skip to content

Commit 4702294

Browse files
committed
Added Location Tagging
1 parent 5189247 commit 4702294

File tree

8 files changed

+227
-9
lines changed

8 files changed

+227
-9
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ php artisan vendor:publish --tag=auth-logger-translations
159159
```
160160
*These are optional files. You don't need to publish them for the package to work. They exist only for cases where you want to make any changes to the files yourself.*
161161

162+
## Location Tagging (GeoIP) - Experimental
163+
164+
*This is an experimental release of location tagging. Accuracy is not 100% guaranteed. Use it at your own risk.*
165+
166+
Location tagging is disabled by default. In order to use location tagging in your auth logger messages and mails, first publish the `geoip` config file using the artisan command below:
167+
168+
```bash
169+
php artisan vendor:publish --tag=auth-logger-geoip-config
170+
```
171+
172+
Next, open the **config/auth-logger.php** file and set **location_tagging** to **true**, alternatively you can set **AUTH_LOGGER_LOCATION_TAGGING=true** in your .env file.
173+
174+
```bash
175+
'location_tagging' => env('AUTH_LOGGER_LOCATION_TAGGING', true),
176+
```
177+
Once enabled, the NewDeviceAlert notifier will pass the location (fetched using the IP Address) to the mail markdown.
178+
179+
If you have any suggestions or improvements that could be made to this, be sure to open a PR for it.
180+
162181
## Testing
163182

164183
``` bash

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"illuminate/http": "^7.0|^8.0",
2727
"illuminate/notifications": "^7.0|^8.0",
2828
"illuminate/support": "^7.0|^8.0",
29-
"spatie/laravel-package-tools": "^1.4"
29+
"spatie/laravel-package-tools": "^1.4",
30+
"torann/geoip": "^3.0"
31+
3032
},
3133
"require-dev": {
3234
"orchestra/testbench": "^6.0",

config/auth-logger.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040

4141
'older' => env('AUTH_LOGGER_OLDER_THAN', 31),
4242

43+
/*
44+
|--------------------------------------------------------------------------
45+
| Enable Location Tagging
46+
|--------------------------------------------------------------------------
47+
|
48+
| Disabled by default. When enabled, this will make use of the GeoIP package
49+
| from Torann to fetch the users location using their IP address. Besure to
50+
| read and follow the instructions in the readme before enabling this feature.
51+
|
52+
*/
53+
'location_tagging' => env('AUTH_LOGGER_LOCATION_TAGGING', false),
54+
4355
/*
4456
|--------------------------------------------------------------------------
4557
| Customizing Slack Messages

config/geoip.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Logging Configuration
8+
|--------------------------------------------------------------------------
9+
|
10+
| Here you may configure the log settings for when a location is not found
11+
| for the IP provided.
12+
|
13+
*/
14+
15+
'log_failures' => true,
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| Include Currency in Results
20+
|--------------------------------------------------------------------------
21+
|
22+
| When enabled the system will do it's best in deciding the user's currency
23+
| by matching their ISO code to a preset list of currencies.
24+
|
25+
*/
26+
27+
'include_currency' => true,
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| Default Service
32+
|--------------------------------------------------------------------------
33+
|
34+
| Here you may specify the default storage driver that should be used
35+
| by the framework.
36+
|
37+
| Supported: "maxmind_database", "maxmind_api", "ipapi"
38+
|
39+
*/
40+
41+
'service' => 'ipapi',
42+
43+
/*
44+
|--------------------------------------------------------------------------
45+
| Storage Specific Configuration
46+
|--------------------------------------------------------------------------
47+
|
48+
| Here you may configure as many storage drivers as you wish.
49+
|
50+
*/
51+
52+
'services' => [
53+
54+
'maxmind_database' => [
55+
'class' => \Torann\GeoIP\Services\MaxMindDatabase::class,
56+
'database_path' => storage_path('app/geoip.mmdb'),
57+
'update_url' => sprintf('https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')),
58+
'locales' => ['en'],
59+
],
60+
61+
'maxmind_api' => [
62+
'class' => \Torann\GeoIP\Services\MaxMindWebService::class,
63+
'user_id' => env('MAXMIND_USER_ID'),
64+
'license_key' => env('MAXMIND_LICENSE_KEY'),
65+
'locales' => ['en'],
66+
],
67+
68+
'ipapi' => [
69+
'class' => \Torann\GeoIP\Services\IPApi::class,
70+
'secure' => true,
71+
'key' => env('IPAPI_KEY'),
72+
'continent_path' => storage_path('app/continents.json'),
73+
'lang' => 'en',
74+
],
75+
76+
'ipgeolocation' => [
77+
'class' => \Torann\GeoIP\Services\IPGeoLocation::class,
78+
'secure' => true,
79+
'key' => env('IPGEOLOCATION_KEY'),
80+
'continent_path' => storage_path('app/continents.json'),
81+
'lang' => 'en',
82+
],
83+
84+
'ipdata' => [
85+
'class' => \Torann\GeoIP\Services\IPData::class,
86+
'key' => env('IPDATA_API_KEY'),
87+
'secure' => true,
88+
],
89+
90+
'ipfinder' => [
91+
'class' => \Torann\GeoIP\Services\IPFinder::class,
92+
'key' => env('IPFINDER_API_KEY'),
93+
'secure' => true,
94+
'locales' => ['en'],
95+
],
96+
97+
],
98+
99+
/*
100+
|--------------------------------------------------------------------------
101+
| Default Cache Driver
102+
|--------------------------------------------------------------------------
103+
|
104+
| Here you may specify the type of caching that should be used
105+
| by the package.
106+
|
107+
| Options:
108+
|
109+
| all - All location are cached
110+
| some - Cache only the requesting user
111+
| none - Disable cached
112+
|
113+
*/
114+
115+
'cache' => 'all',
116+
117+
/*
118+
|--------------------------------------------------------------------------
119+
| Cache Tags
120+
|--------------------------------------------------------------------------
121+
|
122+
| Cache tags are not supported when using the file or database cache
123+
| drivers in Laravel. This is done so that only locations can be cleared.
124+
|
125+
*/
126+
127+
'cache_tags' => env('CACHE_DRIVER', 'array') === "file" ? false : ['torann-geoip-location'],
128+
129+
/*
130+
|--------------------------------------------------------------------------
131+
| Cache Expiration
132+
|--------------------------------------------------------------------------
133+
|
134+
| Define how long cached location are valid.
135+
|
136+
*/
137+
138+
'cache_expires' => 30,
139+
140+
/*
141+
|--------------------------------------------------------------------------
142+
| Default Location
143+
|--------------------------------------------------------------------------
144+
|
145+
| Return when a location is not found.
146+
|
147+
*/
148+
149+
'default_location' => [
150+
'ip' => '127.0.0.0',
151+
'iso_code' => 'US',
152+
'country' => 'Personal Computer',
153+
'city' => 'Localhost',
154+
'state' => 'DEV',
155+
'state_name' => 'Connecticut',
156+
'postal_code' => '06510',
157+
'lat' => 41.31,
158+
'lon' => -72.92,
159+
'timezone' => 'America/New_York',
160+
'continent' => 'NA',
161+
'default' => true,
162+
'currency' => 'USD',
163+
],
164+
165+
];

resources/lang/en/messages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
'browser' => 'Browser',
2424
'platform' => 'Platform',
2525
'device' => 'Device',
26+
'location' => 'Location',
2627
'ignore' => 'If this was you, you can ignore this alert. If you suspect any suspicious activity on your account, please change your password.',
2728
];

resources/views/emails/new-device-alert.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
@if($deviceName)
2424
<b>@lang('auth-logger::messages.device'):</b> {{ $deviceName }}
2525
@endif
26+
@if($location)
27+
<b>@lang('auth-logger::messages.location'):</b> {{ $location->city }} ({{ $location->state }}), {{ $location->country }}.
28+
@endif
2629
</td>
2730
</tr>
2831
</table>

src/AuthLoggerServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ public function configurePackage(Package $package): void
2323
public function packageRegistered()
2424
{
2525
$this->app->register(EventServiceProvider::class);
26+
27+
$this->publishes([
28+
__DIR__ . '/../config/geoip.php' => config_path('geoip.php'),
29+
], 'auth-logger-geoip-config');
2630
}
2731
}

src/Notifications/NewDeviceAlert.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class NewDeviceAlert extends Notification implements ShouldQueue
4949
*/
5050
public $deviceName = '';
5151

52+
/**
53+
* User's Location (if enabled in config).
54+
*/
55+
public $location;
56+
5257
/**
5358
* Create a new notification instance.
5459
*
@@ -74,6 +79,10 @@ public function __construct(AuthLogger $authLogger)
7479
} elseif ($this->agent->isDesktop()) {
7580
$this->deviceType = 'desktop';
7681
}
82+
83+
if(config('auth-logger.location_tagging')) {
84+
$this->location = geoip($this->authLogger->ip_address);
85+
}
7786
}
7887

7988
/**
@@ -107,6 +116,7 @@ public function toMail($notifiable)
107116
'platformVersion' => $this->agent->version($this->platform),
108117
'deviceType' => $this->deviceType,
109118
'deviceName' => $this->deviceName,
119+
'location' => $this->location
110120
]);
111121
}
112122

@@ -125,14 +135,16 @@ public function toSlack($notifiable)
125135
->warning()
126136
->content(Lang::get('auth-logger::messages.content', ['app' => config('app.name')]))
127137
->attachment(function ($attachment) use ($notifiable) {
128-
$attachment->fields([
129-
'Account' => $notifiable->email,
130-
'Time' => $this->authLogger->login_at->toCookieString(),
131-
'IP Address' => $this->authLogger->ip_address,
132-
'Browser' => $this->browser.' ('.$this->agent->version($this->browser).')',
133-
'Platform' => $this->platform.' ('.$this->agent->version($this->platform).')',
134-
'URL' => config('app.url'),
135-
]);
138+
$attachment
139+
->fields([
140+
'Account' => $notifiable->email,
141+
'Time' => $this->authLogger->login_at->toCookieString(),
142+
'IP Address' => $this->authLogger->ip_address,
143+
'Browser' => $this->browser.' ('.$this->agent->version($this->browser).')',
144+
'Platform' => $this->platform.' ('.$this->agent->version($this->platform).')',
145+
'URL' => config('app.url'),
146+
'Location' => $this->location ? $this->location->city .' ('.$this->location->state .'), ' . $this->location->country : 'Disabled',
147+
]);
136148
});
137149
}
138150
}

0 commit comments

Comments
 (0)