Geolocation drivers | laravel-suspicious-login-go | Spatie       

 SPATIE  

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

spatie.be/open-source

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

 Version   v1      

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

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

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

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

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

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

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

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

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

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

 Geolocation drivers
===================

###  On this page 

1. [ Caching ](#content-caching)
2. [ ipinfo ](#content-ipinfo)
3. [ Cloudflare ](#content-cloudflare)
4. [ MaxMind ](#content-maxmind)
5. [ Disabling location lookups ](#content-disabling-location-lookups)
6. [ Creating your own driver ](#content-creating-your-own-driver)

Four checks need to know where a login comes from. New country, multiple countries, impossible travel, and allowed or blocked countries use a driver to turn an IP address into a country, and optionally a city and coordinates.

The default `IPInfoDriver` works without a token. It sends public IP addresses to ipinfo.io, subject to request limits. Choose a different driver in the package configuration:

DriverCountryCoordinatesNeeds`IPInfoDriver`yesyesOptional token`CloudflareDriver`yesWith visitor location headersVerified Cloudflare origin`MaxMindDriver`yesyesLocal database file`NullGeolocation`nonoNothingPrivate and reserved IPs are never looked up. When a location is unavailable, location checks pass. Other checks still contribute.

Caching
-----------------------------------------------------------------------------

Locations are cached per IP for a week. Change `cfg.LocationTTL = 24 * time.Hour` to cache for one day, or set it to zero to bypass caching. Import `time` for durations.

Cache entries are shared between drivers. After switching drivers, an address may retain its old location until expiry. To refresh an address:

```
err := cache.Forget(ctx, "suspicious-login:location:8.8.8.8")
```

Import `go.laravel.com/cache` and handle the error. Cloudflare is an exception. Its headers describe only the current request and are never cached or reused across requests.

ipinfo
--------------------------------------------------------------------------

```
cfg.Geolocation = suspiciouslogin.IPInfoDriver{Token: token}
```

The initial driver reads `IPINFO_TOKEN` from the environment. Keep tokens in deployment secrets rather than source. No token is needed within ipinfo's free limits. Requests time out after two seconds. Network errors, unsuccessful HTTP responses and invalid data leave location unknown. Request cancellation remains an error.

Cloudflare
--------------------------------------------------------------------------------------

`CloudflareDriver` reads `CF-IPCountry`, `CF-IPCity`, `CF-IPLatitude` and `CF-IPLongitude` from the current request without external calls:

```
cfg.Geolocation = suspiciouslogin.CloudflareDriver{Trusted: isVerifiedCloudflareRequest}
```

`isVerifiedCloudflareRequest` is your application's `func(*web.Request) bool`. It must verify that the request actually arrived through Cloudflare, for example through restricted ingress and validated proxy identity. Never return true merely because a `CF-*` header exists. Without the callback, the driver trusts nothing.

Configure Laravel's trusted proxies so `r.IP()` resolves the visitor, not the edge server. Direct requests must not be able to spoof location headers. Enable Cloudflare's "Add visitor location headers" managed transform for coordinates. The driver only resolves the current request's IP, and `XX` countries are unknown.

MaxMind
-----------------------------------------------------------------------------

Lookups against a local GeoLite2 City or GeoIP2 City database don't leave your server:

```
driver, err := suspiciouslogin.OpenMaxMind("storage/app/geoip/GeoLite2-City.mmdb")
// Stop startup on err. Keep driver open while the application runs.
cfg.Geolocation = driver
```

The Go reader dependency is included. Download the database with a [MaxMind account](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data), keep it current with `geoipupdate`, and close the driver at application shutdown. It is safe for concurrent lookups, but do not close it while requests are using it.

Disabling location lookups
--------------------------------------------------------------------------------------------------------------------------------------

```
cfg.Geolocation = suspiciouslogin.NullGeolocation{}
```

This prevents new lookups. Existing per-IP cache entries may still be returned until expiry. Bypass or clear that cache for an immediate change. Existing attempts keep their locations.

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

Implement `Geolocation` or use its function adapter:

```
cfg.Geolocation = suspiciouslogin.GeolocationFunc(func(ctx context.Context, ip string) (*suspiciouslogin.Location, error) {
    return &suspiciouslogin.Location{CountryCode: "BE", City: "Antwerp"}, nil
})
```

Replace the example result with your lookup. Return nil when unknown and propagate errors when login should fail instead. `Latitude` and `Longitude` are nullable `*float64`. Service-level caching and private-IP filtering apply to custom drivers too. Custom request-header drivers should set `LocationTTL` to zero.

 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)
