Skip to content

Commit

Permalink
Add ability to use paid api key
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganTFox committed Oct 8, 2024
1 parent c9832af commit 5c1b688
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
5 changes: 4 additions & 1 deletion src/Services/IPAddressDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/IPAddressHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5c1b688

Please sign in to comment.