-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
56 lines (42 loc) · 1.52 KB
/
utils.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
from flask import Flask
from flask_heroku import Heroku
from flask_sqlalchemy import SQLAlchemy
import os
import requests
auth_base_url = "https://jobhub-authentication-staging.herokuapp.com"
# app initialization
app = Flask(__name__)
app.debug = True
# Config
db_uri = os.environ.get("DATABASE_URL", "")
if not db_uri:
db_name = os.environ.get("PSQL_USER", "")
db_pw = os.environ.get("PSQL_PW", "")
db_uri = f'postgresql://{db_name}:{db_pw}@localhost/jobapplications'
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
heroku = Heroku(app)
db = SQLAlchemy(app)
def validate_authentication(auth_headers, admin=False):
print(auth_headers)
if 'Authorization' not in auth_headers:
return False
auth_token = auth_headers['Authorization']
response = query_auth(auth_token)
print("Response", response)
# If the token is not verified, it is invalid by default
if 'verified' not in response:
return False
# The request requires admin privileges
if admin and response['type'].lower() != 'recruiter':
return False
return '_id' in response
def query_auth(auth_token):
"""
Simple wrapper around auth API, re-used in other parts of the code.
"""
headers = {'content-type': 'application/json', 'Authorization': f"{auth_token}"}
url = f"{auth_base_url}/users/self"
print("Headers", headers, "url", url)
return requests.get(url, headers=headers).json()