-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.py
564 lines (496 loc) · 19.1 KB
/
api.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#! /usr/bin/python3
import contextlib
import os
import logging
from logging.handlers import RotatingFileHandler
from .dependencies import *
from . import db
from . import settings
from fastapi.middleware.cors import CORSMiddleware
description = """
Folksonomy Engine API allows you to add free property/value pairs to Open Food Facts products.
The API use the main following variables:
* **product**: the product number
* **k**: "key", meaning the property or tag
* **v**: "value", the value for a related key
## See also
* [Project page](https://wiki.openfoodfacts.org/Folksonomy_Engine)
* [Folksonomy Engine github repository](https://github.com/openfoodfacts/folksonomy_engine)
* [Documented properties](https://wiki.openfoodfacts.org/Folksonomy/Property)
"""
# Setup FastAPI app lifespan
@contextlib.asynccontextmanager
async def app_lifespan(app: FastAPI):
async with app_logging():
try:
yield
finally:
await db.terminate()
app = FastAPI(title="Open Food Facts folksonomy REST API",
description=description, lifespan=app_lifespan)
# Allow anyone to call the API from their own apps
app.add_middleware(
CORSMiddleware,
# FastAPI doc related to allow_origin (to avoid CORS issues):
# "It's also possible to declare the list as "*" (a "wildcard") to say that all are allowed.
# But that will only allow certain types of communication, excluding everything that involves
# credentials: Cookies, Authorization headers like those used with Bearer Tokens, etc.
# So, for everything to work correctly, it's better to specify explicitly the allowed origins."
# => Workarround: use allow_origin_regex
# Source: https://github.com/tiangolo/fastapi/issues/133#issuecomment-646985050
allow_origin_regex='https?://.*',
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["*"],
)
# define route for authentication
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth", auto_error=False)
@contextlib.asynccontextmanager
async def app_logging():
logger = logging.getLogger("uvicorn.access")
handler = logging.handlers.RotatingFileHandler("api.log",mode="a",maxBytes = 100*1024, backupCount = 3)
handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
logger.addHandler(handler)
yield
@app.middleware("http")
async def initialize_transactions(request: Request, call_next):
"""middleware that enclose request processing in a transaction"""
# eventually log user
async with db.transaction():
response = await call_next(request)
return response
@app.get("/", status_code=status.HTTP_200_OK)
async def hello():
return {"message": "Hello folksonomy World! Tip: open /docs for documentation"}
async def get_current_user(token: str = Depends(oauth2_scheme)):
"""
Get current user and check token validity if present
"""
if token and '__U' in token:
cur = db.cursor()
await cur.execute(
"UPDATE auth SET last_use = current_timestamp AT TIME ZONE 'GMT' WHERE token = %s", (token,)
)
if cur.rowcount == 1:
return User(user_id=token.split('__U', 1)[0])
else:
return User(user_id=None)
def sanitize_data(k, v):
"""Some sanitization of data"""
k = k.strip()
v = v.strip() if v else v
return k, v
def check_owner_user(user: User, owner, allow_anonymous=False):
"""
Check authentication depending on current user and 'owner' of the data
"""
user = user.user_id if user is not None else None
if user is None and allow_anonymous == False:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication required",
headers={"WWW-Authenticate": "Bearer"},
)
if owner != '':
if user is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication required for '%s'" % owner,
headers={"WWW-Authenticate": "Bearer"},
)
if owner != user:
raise HTTPException(
status_code=422,
detail="owner should be '%s' or '' for public, but '%s' is authenticated" % (
owner, user),
)
return
def get_auth_server(request: Request):
"""
Get auth server URL from request
We deduce it by changing part of the request base URL
according to FOLKSONOMY_PREFIX and AUTH_PREFIX settings
"""
base_url = f"{request.base_url.scheme}://{request.base_url.netloc}"
# remove folksonomy prefix and add AUTH prefix
base_url = base_url.replace(settings.FOLKSONOMY_PREFIX or "", settings.AUTH_PREFIX or "")
return base_url
@app.post("/auth")
async def authentication(request: Request, response: Response, form_data: OAuth2PasswordRequestForm = Depends()):
"""
Authentication: provide user/password and get a bearer token in return
- **username**: Open Food Facts user_id (not email)
- **password**: user password (clear text, but HTTPS encrypted)
token is returned, to be used in later requests with usual "Authorization: bearer token" headers
"""
user_id = form_data.username
password = form_data.password
token = user_id+'__U'+str(uuid.uuid4())
auth_url = get_auth_server(request) + "/cgi/auth.pl"
auth_data={'user_id': user_id, 'password': password}
async with aiohttp.ClientSession() as http_session:
async with http_session.post(auth_url, data=auth_data) as resp:
status_code = resp.status
if status_code == 200:
cur, timing = await db.db_exec("""
DELETE FROM auth WHERE user_id = %s;
INSERT INTO auth (user_id, token, last_use) VALUES (%s,%s,current_timestamp AT TIME ZONE 'GMT');
""", (user_id, user_id, token))
if cur.rowcount == 1:
return {"access_token": token, "token_type": "bearer"}
elif status_code == 403:
await asyncio.sleep(settings.FAILED_AUTH_WAIT_TIME) # prevents brute-force
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
raise HTTPException(
status_code=500, detail="Server error")
@app.post("/auth_by_cookie")
async def authentication(request: Request, response: Response, session: Optional[str] = Cookie(None)):
"""
Authentication: provide Open Food Facts session cookie and get a bearer token in return
- **session cookie**: Open Food Facts session cookie
token is returned, to be used in later requests with usual "Authorization: bearer token" headers
"""
if not session or session =='':
raise HTTPException(
status_code=422, detail="Missing 'session' cookie")
try:
session_data = session.split('&')
user_id = session_data[session_data.index('user_id') + 1]
token = user_id + '__U' + str(uuid.uuid4())
except:
raise HTTPException(
status_code=422, detail="Malformed 'session' cookie")
auth_url = get_auth_server(request) + "/cgi/auth.pl"
async with aiohttp.ClientSession() as http_session:
async with http_session.post(auth_url, cookies={'session': session}) as resp:
status_code = resp.status
if status_code == 200:
cur, timing = await db.db_exec(
"""
DELETE FROM auth WHERE user_id = %s;
INSERT INTO auth (user_id, token, last_use) VALUES (%s,%s,current_timestamp AT TIME ZONE 'GMT');
""",
(user_id, user_id, token)
)
if cur.rowcount == 1:
return {"access_token": token, "token_type": "bearer"}
elif status_code == 403:
await asyncio.sleep(settings.FAILED_AUTH_WAIT_TIME) # prevents brute-force
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
raise HTTPException(
status_code=500, detail="Server error")
def property_where(owner: str, k: str, v: str):
"""Build a SQL condition on a property, filtering by owner and eventually key and value
"""
conditions = ['owner=%s']
params = [owner]
if k != '':
conditions.append('k=%s')
params.append(k)
if v != '':
conditions.append('v=%s')
params.append(v)
where = " AND ".join(conditions)
return where, params
@app.get("/products/stats", response_model=List[ProductStats])
async def product_stats(response: Response,
owner='', k='', v='',
user: User = Depends(get_current_user)):
"""
Get the list of products with tags statistics
The products list can be limited to some tags (k or k=v)
"""
check_owner_user(user, owner, allow_anonymous=True)
k, v = sanitize_data(k, v)
where, params = property_where(owner, k, v)
cur, timing = await db.db_exec("""
SELECT json_agg(j.j)::json FROM(
SELECT json_build_object(
'product',product,
'keys',count(*),
'last_edit',max(last_edit),
'editors',count(distinct(editor))
) as j
FROM folksonomy
WHERE %s
GROUP BY product) as j;
""" % where,
params
)
out = await cur.fetchone()
# cur, timing = await db.db_exec("""
# SELECT count(*)
# FROM folksonomy;
# """
# )
# out2 = await cur.fetchone()
# import pdb;pdb.set_trace()
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing":timing})
@app.get("/products", response_model=List[ProductList])
async def product_list(response: Response,
owner='', k='', v='',
user: User = Depends(get_current_user)):
"""
Get the list of products matching k or k=v
"""
if k == '':
return JSONResponse(status_code=422, content={"detail": {"msg": "missing value for k"}})
check_owner_user(user, owner, allow_anonymous=True)
k, v = sanitize_data(k, v)
where, params = property_where(owner, k, v)
cur, timing = await db.db_exec("""
SELECT coalesce(json_agg(j.j)::json, '[]'::json) FROM(
SELECT json_build_object(
'product',product,
'k',k,
'v',v
) as j
FROM folksonomy
WHERE %s
) as j;
""" % where,
params
)
out = await cur.fetchone()
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing":timing})
@app.get("/product/{product}", response_model=List[ProductTag])
async def product_tags_list(response: Response,
product: str, owner='',
user: User = Depends(get_current_user)):
"""
Get a list of existing tags for a product
"""
check_owner_user(user, owner, allow_anonymous=True)
cur, timing = await db.db_exec("""
SELECT json_agg(j)::json FROM(
SELECT * FROM folksonomy WHERE product = %s AND owner = %s ORDER BY k
) as j;
""",
(product, owner),
)
out = await cur.fetchone()
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
@app.get("/product/{product}/{k}", response_model=ProductTag)
async def product_tag(response: Response,
product: str, k: str, owner='',
user: User = Depends(get_current_user)):
"""
Get a specific tag or tag hierarchy on a product
- /product/xxx/key returns only the requested key
- /product/xxx/key* returns the key and subkeys (key:subkey)
"""
k, v = sanitize_data(k, None)
key = re.sub(r'[^a-z0-9_\:]', '', k)
check_owner_user(user, owner, allow_anonymous=True)
if k[-1:] == '*':
cur, timing = await db.db_exec(
"""
SELECT json_agg(j)::json FROM(
SELECT *
FROM folksonomy
WHERE product = %s AND owner = %s AND k ~ %s
ORDER BY k) as j;
""",
(product, owner, '^%s(:.|$)' % key),
)
else:
cur, timing = await db.db_exec(
"""
SELECT row_to_json(j) FROM(
SELECT *
FROM folksonomy
WHERE product = %s AND owner = %s AND k = %s
) as j;
""",
(product, owner, key),
)
out = await cur.fetchone()
if out:
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
else:
return JSONResponse(status_code=404, content=None)
@app.get("/product/{product}/{k}/versions", response_model=List[ProductTag])
async def product_tag_list_versions(response: Response,
product: str, k: str, owner='',
user: User = Depends(get_current_user)):
"""
Get a list of all versions of a tag for a product
"""
check_owner_user(user, owner, allow_anonymous=True)
k, v = sanitize_data(k, None)
cur, timing = await db.db_exec(
"""
SELECT json_agg(j)::json FROM(
SELECT *
FROM folksonomy_versions
WHERE product = %s AND owner = %s AND k = %s
ORDER BY version DESC
) as j;
""",
(product, owner, k),
)
out = await cur.fetchone()
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
@app.post("/product")
async def product_tag_add(response: Response,
product_tag: ProductTag,
user: User = Depends(get_current_user)):
"""
Create a new product tag (version=1)
- **product**: which product
- **k**: which key for the tag
- **v**: which value to set for the tag
- **version**: none or empty or 1
- **owner**: none or empty for public tags, or your own user_id
Be aware it's not possible to create the same tag twice. Though, you can update
a tag and add multiple values the way you want (don't forget to document how); comma
separated list is a good option.
"""
check_owner_user(user, product_tag.owner, allow_anonymous=False)
# enforce user
product_tag.editor = user.user_id
# note: version is checked by postgres routine
try:
query, params = db.create_product_tag_req(product_tag)
cur, timing = await db.db_exec(query, params)
except psycopg2.Error as e:
error_msg = re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1]
return JSONResponse(status_code=422, content={"detail": {"msg": error_msg}})
if cur.rowcount == 1:
return "ok"
return
@app.put("/product")
async def product_tag_update(response: Response,
product_tag: ProductTag,
user: User = Depends(get_current_user)):
"""
Update a product tag
- **product**: which product
- **k**: which key for the tag
- **v**: which value to set for the tag
- **version**: must be equal to previous version + 1
- **owner**: None or empty for public tags, or your own user_id
"""
check_owner_user(user, product_tag.owner, allow_anonymous=False)
# enforce user
product_tag.editor = user.user_id
try:
req, params = db.update_product_tag_req(product_tag)
cur, timing = await db.db_exec(req, params)
except psycopg2.Error as e:
raise HTTPException(
status_code=422,
detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1],
)
if cur.rowcount == 1:
return "ok"
elif cur.rowcount == 0: # non existing key
raise HTTPException(
status_code=404,
detail="Key was not found",
)
else:
raise HTTPException(
status_code=503,
detail="Doubious update - more than one row udpated",
)
@app.delete("/product/{product}/{k}")
async def product_tag_delete(response: Response,
product: str, k: str, version: int, owner='',
user: User = Depends(get_current_user)):
"""
Delete a product tag
"""
check_owner_user(user, owner, allow_anonymous=False)
k, v = sanitize_data(k, None)
try:
# Setting version to 0, this is seen as a reset,
# while maintaining history in folksonomy_versions
cur, timing = await db.db_exec(
"""
UPDATE folksonomy SET version = 0, editor = %s, comment = 'DELETE'
WHERE product = %s AND owner = %s AND k = %s AND version = %s;
""",
(user.user_id, product, owner, k, version),
)
except psycopg2.Error as e:
# note: transaction will be rolled back by the middleware
raise HTTPException(
status_code=422,
detail=re.sub(r'.*@@ (.*) @@\n.*$', r'\1', e.pgerror)[:-1],
)
if cur.rowcount != 1:
raise HTTPException(
status_code=422,
detail="Unknown product/k/version for this owner",
)
cur, timing = await db.db_exec(
"""
DELETE FROM folksonomy WHERE product = %s AND owner = %s AND k = %s AND version = 0;
""",
(product, owner, k.lower()),
)
if cur.rowcount == 1:
return "ok"
else:
# we have a conflict, return an error explaining conflict
cur, timing = await db.db_exec(
"""
SELECT version FROM folksonomy WHERE product = %s AND owner = %s AND k = %s
""",
(product, owner, k)
)
if cur.rowcount == 1:
out = await cur.fetchone()
raise HTTPException(
status_code=422,
detail="version mismatch, last version for this product/k is %s" % out[0],
)
else:
raise HTTPException(
status_code=404,
detail="Unknown product/k for this owner",
)
@app.get("/keys")
async def keys_list(response: Response,
owner='',
user: User = Depends(get_current_user)):
"""
Get the list of keys with statistics
The keys list can be restricted to private tags from some owner
"""
check_owner_user(user, owner, allow_anonymous=True)
cur, timing = await db.db_exec(
"""
SELECT json_agg(j.j)::json FROM(
SELECT json_build_object(
'k',k,
'count',count(*),
'values',count(distinct(v))
) as j
FROM folksonomy
WHERE owner=%s
GROUP BY k
ORDER BY count(*) DESC) as j;
""",
(owner,)
)
out = await cur.fetchone()
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
@app.get("/ping")
async def pong(response: Response):
"""
Check server health
"""
cur, timing = await db.db_exec("SELECT current_timestamp AT TIME ZONE 'GMT'",())
pong = await cur.fetchone()
return {"ping": "pong @ %s" % pong[0]}