diff --git a/README.md b/README.md index 084d844..94c3936 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,14 @@ To enable this, add the following value to the package config: 'allow_same_location_login' => true, ``` +## Using Paid API + +Be default, this package uses the free api provided by [ip-api.com](https://ip-api.com/). If you would like to use their [paid API](https://members.ip-api.com/) to increase rate limiting, you can set the following env value with your api key: + +``` +IP_API_KEY=your_api_key +``` + ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/config/config.php b/config/config.php index a2d0b6d..08f32a0 100644 --- a/config/config.php +++ b/config/config.php @@ -23,4 +23,8 @@ // This is useful when using a network which switches IP addresses frequently. Not recommended for most use cases. // Will check if given user has logged in from the given city, and region. 'allow_same_location_login' => false, + + // This package uses the https://members.ip-api.com/ API to get the location data for an IP address. + // You can use a paid API key to increase the rate limit. + 'ip-api-key' => env('IP_API_KEY'), ]; \ No newline at end of file diff --git a/src/Services/IPAddressDriver.php b/src/Services/IPAddressDriver.php index f4bb2c9..a9c69d9 100644 --- a/src/Services/IPAddressDriver.php +++ b/src/Services/IPAddressDriver.php @@ -21,7 +21,10 @@ public function __construct( public function handle(): void { - $ipLocationData = Http::retry(3)->get('http://ip-api.com/json/'.$this->ipAddress)?->json(); + $ipLocationData = Http::retry(3) + ->withQueryParameters(['api_key' => config('security-notifications.ip_api_key')]) + ->get('http://ip-api.com/json/'.$this->ipAddress) + ?->json(); throw_if(is_null($ipLocationData), new Exception('Failed to get IP location data for: '.$this->ipAddress)); diff --git a/tests/Unit/IPAddressHandlerTest.php b/tests/Unit/IPAddressHandlerTest.php index ae1ffed..72e9548 100644 --- a/tests/Unit/IPAddressHandlerTest.php +++ b/tests/Unit/IPAddressHandlerTest.php @@ -23,6 +23,8 @@ Http::shouldReceive('retry') ->andReturnSelf(); + Http::shouldReceive('withQueryParameters') + ->andReturnSelf(); Http::shouldReceive('get') ->with('http://ip-api.com/json/127.0.0.1') ->once() @@ -62,6 +64,8 @@ Http::shouldReceive('retry') ->andReturnSelf(); + Http::shouldReceive('withQueryParameters') + ->andReturnSelf(); Http::shouldReceive('get') ->with('http://ip-api.com/json/'.$login->ip_address) ->once() @@ -101,6 +105,8 @@ Http::shouldReceive('retry') ->andReturnSelf(); + Http::shouldReceive('withQueryParameters') + ->andReturnSelf(); Http::shouldReceive('get') ->with('http://ip-api.com/json/127.0.0.1') ->once() @@ -140,6 +146,8 @@ Http::shouldReceive('retry') ->andReturnSelf(); + Http::shouldReceive('withQueryParameters') + ->andReturnSelf(); Http::shouldReceive('get') ->with('http://ip-api.com/json/127.0.0.1') ->once() @@ -198,6 +206,8 @@ Http::shouldReceive('retry') ->andReturnSelf(); + Http::shouldReceive('withQueryParameters') + ->andReturnSelf(); Http::shouldReceive('get') ->with('http://ip-api.com/json/128.0.0.1') ->once()