Repository navigation
Expand file tree
/
Copy path40-API-Key-JDBC-Store.yaml
More file actions
66 lines (64 loc) · 2.1 KB
/
Copy path40-API-Key-JDBC-Store.yaml
File metadata and controls
66 lines (64 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# yaml-language-server: $schema=https://www.membrane-api.io/v7.7.1.json
#
# Tutorial: JDBC API Key Store
#
# Store API keys and scopes in a PostgreSQL database. Membrane reads them via JDBC
# and auto-creates the required tables on first start.
#
# 1.) Install the PostgreSQL JDBC driver:
# Membrane does not bundle database drivers. Download the driver (a single .jar)
# from https://jdbc.postgresql.org/download/ and place it in Membrane's lib/
# directory. Membrane picks up every .jar in lib/ on startup automatically.
#
# 2.) Start PostgreSQL:
# docker run --rm --name postgres -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres
#
# 3.) Start Membrane:
# Linux/Mac:
# ./membrane.sh -c 40-API-Key-JDBC-Store.yaml
# Windows:
# membrane.cmd -c 40-API-Key-JDBC-Store.yaml
#
# Membrane connects to PostgreSQL and creates the key and scope tables.
#
# 4.) Insert test keys:
# docker exec -i postgres psql -U postgres -d postgres -c "INSERT INTO key (apikey) VALUES ('demo-key'), ('admin-key')"
# docker exec -i postgres psql -U postgres -d postgres -c "INSERT INTO scope (apikey, scope) VALUES ('admin-key', 'admin')"
#
# 5.) Test:
#
# No key → 401:
# curl -i localhost:2000
#
# Valid key (no scopes):
# curl localhost:2000 -H "X-Api-Key: demo-key"
#
# Valid key with admin scope:
# curl localhost:2000 -H "X-Api-Key: admin-key"
components:
dataSource:
bean:
class: org.apache.commons.dbcp2.BasicDataSource
properties:
- name: driverClassName
value: org.postgresql.Driver
- name: url
value: jdbc:postgresql://localhost:5432/postgres
- name: username
value: postgres
- name: password
value: secret
---
api:
port: 2000
target:
url: https://api.predic8.de
flow:
- apiKey:
stores:
- databaseApiKeyStore:
datasource: '#/components/dataSource'
keyTable: key # Membrane creates these tables automatically
scopeTable: scope # if they don't exist (autoCreate: true)
extractors:
- header: X-Api-Key