IP reputation drivers | laravel-suspicious-login | Spatie       

 SPATIE  

laravel-suspicious-login
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-suspicious-login](https://spatie.be/docs/laravel-suspicious-login/v1)  Advanced-usage  IP reputation drivers

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-suspicious-login/v1) 

  IP reputation drivers    
- [ Introduction ](https://spatie.be/docs/laravel-suspicious-login/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-suspicious-login/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-suspicious-login/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-suspicious-login/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-suspicious-login/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-suspicious-login/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-suspicious-login/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/getting-started)
- [ How a login is scored ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/how-a-login-is-scored)
- [ Choosing a preset ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/choosing-a-preset)
- [ Customizing presets ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/customizing-presets)
- [ Working with login attempts ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/working-with-login-attempts)
- [ Adding custom properties ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/adding-custom-properties)
- [ Recording logins manually ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/recording-logins-manually)

Available checks
----------------

- [ Overview ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/overview)
- [ New device ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-device)
- [ New IP address ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-ip)
- [ New country ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-country)
- [ Impossible travel ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/impossible-travel)
- [ Allowed and blocked countries ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/country)
- [ Failed attempts ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/failed-attempts)
- [ Credential stuffing ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/credential-stuffing)
- [ Unusual time ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/unusual-time)
- [ Suspicious user agent ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/suspicious-user-agent)
- [ IP reputation ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/ip-reputation)
- [ Multiple countries ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/multiple-countries)

Responding to suspicious logins
-------------------------------

- [ Notifying users ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/notifying-users)
- [ Blocking logins ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/blocking-logins)
- [ Writing your own actions ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/writing-your-own-actions)
- [ Using events ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/using-events)

Advanced usage
--------------

- [ Creating custom checks ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/creating-custom-checks)
- [ Geolocation drivers ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/geolocation-drivers)
- [ IP reputation drivers ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/ip-reputation-drivers)
- [ Device fingerprints ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/device-fingerprints)
- [ Using your own model ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/using-your-own-model)

 IP reputation drivers
=====================

###  On this page 

1. [ Tor ](#content-tor)
2. [ AbuseIPDB ](#content-abuseipdb)
3. [ Creating your own driver ](#content-creating-your-own-driver)

The [IP reputation check](/docs/laravel-suspicious-login/v1/available-checks/ip-reputation) asks an IP reputation driver whether an IP address is a Tor exit node, and how likely it is to be abusive. Only that check uses these drivers, and only the `Strict` preset contains it.

To enable Tor detection with the `Strict` preset, set these values in `config/suspicious-login.php`, leaving the other options in place:

```
'preset' => Spatie\SuspiciousLogin\Presets\Strict::class,

'ip_reputation' => [
    'driver' => 'tor',
    // ...
],
```

DriverTor exit nodesAbuse scoreNeeds`null`nononothing, this is the default`tor`yesnonothing`abuseipdb`yesyesan AbuseIPDB API keyIf you use another preset, [add `IpReputationCheck` to it](/docs/laravel-suspicious-login/v1/basic-usage/customizing-presets). Selecting a driver does not add the check for you. The check adds points; it doesn't block a login on its own.

Results are cached per IP address for a day. The `ip_reputation.cache_ttl` config value is in seconds. Private and reserved IP addresses are never looked up.

Cached results are shared between drivers. To refresh a specific address after switching drivers:

```
use Illuminate\Support\Facades\Cache;

Cache::forget('suspicious-login:ip-reputation:8.8.8.8');
```

Tor
-----------------------------------------------------------------

The `tor` driver downloads [the list of Tor exit nodes](https://check.torproject.org/torbulkexitlist) published by the Tor Project, and keeps it in your cache for an hour. It costs nothing and needs no account.

AbuseIPDB
-----------------------------------------------------------------------------------

The `abuseipdb` driver calls the [AbuseIPDB](https://www.abuseipdb.com) API. It returns whether the IP address is a Tor exit node, and an abuse confidence score from 0 to 100 based on what other sites reported in the last 90 days. Create a free account, generate an API key and configure it in `config/suspicious-login.php`:

```
'ip_reputation' => [
    'driver' => 'abuseipdb',
    'abuseipdb' => [
        'key' => env('ABUSEIPDB_KEY'),
    ],
    // ...
],
```

Keep the key in your deployment's secrets or `.env` file rather than committing it to source control.

When the API can't be reached or returns an unsuccessful response, this driver returns no Tor flag or abuse score. The reputation check then adds no points, but other checks can still flag or block the login.

Creating your own driver
--------------------------------------------------------------------------------------------------------------------------------

A driver implements `Spatie\SuspiciousLogin\Drivers\IpReputation\IpReputationDriver` and returns an `IpReputation`:

```
namespace App\SuspiciousLogin;

use Spatie\SuspiciousLogin\Drivers\IpReputation\IpReputation;
use Spatie\SuspiciousLogin\Drivers\IpReputation\IpReputationDriver;

class MyIpReputationDriver implements IpReputationDriver
{
    public function check(string $ip): IpReputation
    {
        $result = MyReputationService::check($ip);

        return new IpReputation(
            isTor: $result->isTor,
            abuseScore: $result->riskPercentage,
        );
    }
}
```

Register it in the `boot` method of a service provider:

```
use App\SuspiciousLogin\MyIpReputationDriver;
use Spatie\SuspiciousLogin\Drivers\IpReputation\IpReputationManager;

app(IpReputationManager::class)->extend('my-driver', fn () => new MyIpReputationDriver);
```

Then select the driver in `config/suspicious-login.php`:

```
'ip_reputation' => [
    'driver' => 'my-driver',
    // ...
],
```

Its results use the same per-IP cache as the built-in drivers. Add `IpReputationCheck::new()` to your preset if you are not using `Strict`; selecting a driver does not add the check for you.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
