Bearer Lookup
Requires incoming requests include an API token that is looked up in a datastore. Requests without a valid token are rejected.
| Requires a postgresql or redis datastore to supply the connection. |
Description
This lets you validate API keys you already manage in your own database. It can associate a user ID and tenant ID with the request, but does not support authorization options (allowed layers or bounding box).
Lookup results can optionally be cached in memory to decrease database load. Use this with caution since a revoked token can keep working until the cache entry expires. Rejections are cached too, but outright errors (such as an inability to connect to the database) are not.
Configuration
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
The ID of the datastore to use |
string |
Yes |
None |
|
The header to extract the token from. If this is |
string |
No |
Authorization |
|
An encoding to apply to the token before looking it up. One of |
string |
No |
None |
|
How many lookup results to keep in memory. Set to -1 to disable |
int |
No |
-1 |
|
How many seconds a lookup result is reused before the datastore is checked again |
uint32 |
No |
600 |
|
Options for a postgresql datastore. Required with a postgresql datastore and must not be set for a redis datastore. See PostgreSQL |
PostgreSQL |
Conditional |
None |
|
Options for a redis datastore. Required with a redis datastore and must not be set for a postgresql datastore. See Redis |
Redis |
Conditional |
None |
PostgreSQL
The query is executed with the auth token passed in as the $1 bind parameter. It should be a SELECT that returns a single row. If no rows or more than one row are returned, authentication fails.
The returned row should have a mix of the columns in the following table. Any other columns are ignored. A column that is present but has the wrong data type causes a warning to be logged and authentication to fail.
Example
-
YAML
-
JSON
datastores:
- name: postgresql
id: auth_db
host: pg.example.com
user: tilegroxy
password: env.PGPASSWORD
database: accounts
authentication:
name: bearer
datastore: auth_db
postgresql:
query: SELECT user_id, tenant_id FROM api_keys WHERE token_hash = $1 AND revoked = false
{
"datastores": [
{
"name": "postgresql",
"id": "auth_db",
"host": "pg.example.com",
"user": "tilegroxy",
"password": "env.PGPASSWORD",
"database": "accounts"
}
],
"authentication": {
"name": "bearer",
"datastore": "auth_db",
"postgresql": {
"query": "SELECT user_id, tenant_id FROM api_keys WHERE token_hash = $1 AND revoked = false"
}
}
}
Configuration
| Column | Meaning |
|---|---|
|
A boolean used as an additional check. Authentication passes if true and fails if false or NULL |
|
A string used as the user identifier, if non-null |
|
A string used as the tenant identifier, if non-null. See How to: Serve Multiple Tenants |
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
The SQL used to look up the token. Must reference the token as |
string |
Yes |
None |
Redis
The auth token is used as a key, with a required prefix. Authentication passes if the key exists and is of the right data type. Setting a user ID and tenant ID requires the entry to be a Hash.
Example
-
YAML
-
JSON
datastores:
- name: redis
id: auth_redis
host: redis.example.com
authentication:
name: bearer
datastore: auth_redis
redis:
keyPrefix: "apikey:"
allowedType: hash
{
"datastores": [
{
"name": "redis",
"id": "auth_redis",
"host": "redis.example.com"
}
],
"authentication": {
"name": "bearer",
"datastore": "auth_redis",
"redis": {
"keyPrefix": "apikey:",
"allowedType": "hash"
}
}
}
A matching token can be created with HSET apikey:abc123 user alice tenant acme.
Configuration
| Parameter | Description | Type | Required | Default |
|---|---|---|---|---|
|
Prepended to the token to form the key. Only keys starting with this prefix can be used as tokens, so use one that nothing else in the database shares |
string |
Yes |
None |
|
The hash field containing the user ID |
string |
No |
user |
|
The hash field containing the tenant ID |
string |
No |
tenant |
|
The type of key that is accepted as a token. One of |
string |
No |
Any type |