Skip to content

Commit

Permalink
Wwi21/46 implementing rate limiting (#176)
Browse files Browse the repository at this point in the history
* Basic logic for token checking

* Initial Firebase Setup

* Implemented token system

* Linting checks

* Linting checks

* firebase_admin dependency added

* added requirement

* firebase_admin added to specific requirements

* adds service account certificate env var from github secrests

* removes superflous dependency 'pnmlbpmntransformer'

* 💚

* 💚 Changes service account certificate env var handling

* Check Tokens Function added

* adds basic decodation of base64 service account certificate

* Refactor service account certificate handling

* fixes set_force_std_xml flag position

* now uses correct var for decoding base64

* added tempfile for secret

* remove unused dependency

* test prints added

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* t

* test

* test

* test

* test

* test

* improved code quality

* final touches

* fixes cd pipeline: sets necessary env var for gcp service account certificate

* fix cd pipeline: also sets env var in matrix job

* replaces print message for missing env var with exception

* swaps checkout and env set step in cd pipeline

* removes workflow dispatch and finally fixes CD env var (hopefully)

* Initial Setup for refreshTokens endpoint

* fixed app.py

* fix method name

* added matrix build for deployment of refresh tokens

* Fixed jsonify error

* adjusted check_token with timestamp logic

* added pytz requirement

---------

Co-authored-by: Jeldrik Merkelbach <[email protected]>
Co-authored-by: Niyada <[email protected]>
  • Loading branch information
3 people authored Jun 25, 2024
1 parent 765a544 commit f38e640
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/checkTokens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from firebase_admin import credentials, firestore
from flask import jsonify
import os
import pytz
from datetime import datetime, timedelta

GCP_SERVICE_ACCOUNT_CERTIFICATE_BASE64 = os.getenv( "GCP_SERVICE_ACCOUNT_CERTIFICATE" )
if( GCP_SERVICE_ACCOUNT_CERTIFICATE_BASE64 is None ):
Expand Down Expand Up @@ -36,8 +38,22 @@ def check_tokens(request):
doc = doc_ref.get()
if doc.exists:
tokens = doc.to_dict().get("tokens", 0)
timestamp = doc.to_dict().get("tokens_last_replenished", 0)
current_time = datetime.now(pytz.utc)
time_difference = current_time - timestamp

if tokens <= 0:
return jsonify({"error": "No tokens available"}), 400
if time_difference >= timedelta(hours=1):
doc_ref.update({
"tokens": 99,
"tokens_last_replenished": current_time
})
return jsonify({"tokens": 99})
else:
return jsonify({"error":
"No tokens available, and last replenish" +
"was less than an hour ago." +
"Please try again later."}), 400
else:
doc_ref.update({"tokens": tokens-1})
return jsonify({"tokens": tokens-1}), 200
Expand Down
3 changes: 2 additions & 1 deletion src/checkTokens/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
firebase_admin==6.5.0
functions-framework==3.7.0
functions-framework==3.7.0
pytz==2024.1

0 comments on commit f38e640

Please sign in to comment.