-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_app.py
68 lines (58 loc) · 2.03 KB
/
function_app.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
import logging
import uuid
import sys
import time
import jwt
import os
import base64
import json
import math
import azure.functions as func
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="http_trigger_qlikcloud_jwt_token")
def http_trigger_qlikcloud_jwt_token(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
private_key = base64.b64decode(os.environ['private_key'])
issuer = os.environ['issuer']
kid = os.environ['kid']
new_GUID = str(uuid.uuid4())
def create_JWT(private_key, GUID):
sub = 'ANON\\' + GUID
name = 'Anonymous'
email = GUID + '@example.com'
current_time = math.trunc(time.time())
not_before = round(current_time - 5 * 60) # account for clock skew
expir = round(current_time + 5 * 60)
groups = ['anonymous']
claim = {
"sub": sub,
"subType": "user",
"name": name,
"email": email,
"email_verified": True,
'iss': issuer,
'iat': current_time,
'nbf': not_before,
'exp': expir,
'jti': str(uuid.uuid4()),
'aud': 'qlik.api/login/jwt-session',
'groups': groups
}
token = jwt.encode(claim, private_key,
algorithm='RS256',
headers={'alg': 'RS256',
'kid': kid,
'typ': 'JWT'}
)
return token
response = create_JWT(private_key, new_GUID)
except Exception as e:
response = str(e)
return func.HttpResponse(
json.dumps({
"body": response
}),
mimetype="application/json",
status_code=200
)