forked from davidejones/netlify-cms-oauth-provider-python
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstate_management.py
More file actions
37 lines (24 loc) · 837 Bytes
/
state_management.py
File metadata and controls
37 lines (24 loc) · 837 Bytes
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
from uuid import uuid4
from google.cloud import firestore
from os import environ
STATE_STORAGE_COLLECTION = environ.get('STATE_STORAGE_COLLECTION')
def state_management_enabled():
return True if STATE_STORAGE_COLLECTION else False
def save_state(state):
db = firestore.Client()
state_storage = db.collection(STATE_STORAGE_COLLECTION)
state_storage.document(state).set({'created': firestore.SERVER_TIMESTAMP})
return
def create_state():
state = str(uuid4())
save_state(state)
return state
def validate_state(state):
db = firestore.Client()
state_doc_ref = db.collection(STATE_STORAGE_COLLECTION).document(state)
state_doc = state_doc_ref.get()
state_valid = False
if state_doc.exists:
state_valid = True
state_doc_ref.delete()
return state_valid