forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_web_token.py
More file actions
29 lines (25 loc) · 783 Bytes
/
json_web_token.py
File metadata and controls
29 lines (25 loc) · 783 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
import jwt
from werkzeug.test import create_environ
from flask import Request
from operator import itemgetter
from datetime import datetime as dt, timedelta
from os import environ
def tokenize(
parameters={},
iat=None,
exp=None,
# TODO: SUPER_SECRET_SALT isn't actually a salt! Give this a better name.
secret=environ.get("SUPER_SECRET_SALT", ""),
exp_period=1,
):
if not iat:
iat = dt.timestamp(dt.utcnow())
if not exp:
exp = dt.timestamp(dt.utcnow() + timedelta(hours=exp_period))
return jwt.encode(
{"exp": exp, "iat": iat, "parameters": parameters}, secret, algorithm="HS256",
).decode("utf-8")
def auth_header(token):
env = create_environ()
env.update(HTTP_AUTHORIZATION=token)
return Request(env)