|
| 1 | +#!/usr/bin/env python |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import secrets |
| 5 | + |
| 6 | +from flask import Flask, Response, request, render_template |
| 7 | + |
| 8 | +logging.getLogger().setLevel(logging.DEBUG) |
| 9 | +logging.getLogger('flask_pyoidc').setLevel(logging.ERROR) |
| 10 | +logging.getLogger('oic').setLevel(logging.ERROR) |
| 11 | +logging.getLogger('jwkest').setLevel(logging.ERROR) |
| 12 | +logging.getLogger('urllib3').setLevel(logging.ERROR) |
| 13 | +logging.getLogger('werkzeug').setLevel(logging.ERROR) |
| 14 | + |
| 15 | +app = Flask(__name__, template_folder='templates', static_folder='static') |
| 16 | + |
| 17 | +nonces = {} |
| 18 | + |
| 19 | + |
| 20 | +def debug(request): |
| 21 | + for header in request.headers: |
| 22 | + logging.debug(header) |
| 23 | + for key, value in request.form.items(): |
| 24 | + logging.debug(f'POST {key}: {value}') |
| 25 | + |
| 26 | + |
| 27 | +@app.route('/api/users/proxy_authz_eb', methods=['POST']) |
| 28 | +def api(): |
| 29 | + logging.debug('-> /api/users/proxy_authz_eb') |
| 30 | + debug(request) |
| 31 | + |
| 32 | + uid = request.form.get('user_id') |
| 33 | + continue_url = request.form.get('continue_url') |
| 34 | + service_entity_id = request.form.get('service_id') |
| 35 | + issuer_id = request.form.get('issuer_id') |
| 36 | + |
| 37 | + nonce = secrets.token_urlsafe() |
| 38 | + nonces[nonce] = (uid, continue_url, service_entity_id, issuer_id) |
| 39 | + |
| 40 | + response = Response(status=200) |
| 41 | + body = { |
| 42 | + 'msg': 'interrupt', |
| 43 | + # 'msg': 'skip', |
| 44 | + 'nonce': nonce, |
| 45 | + 'attributes': { |
| 46 | + 'urn:mace:dir:attribute-def:eduPersonEntitlement': [ |
| 47 | + uid, |
| 48 | + nonce, |
| 49 | + 'urn:foobar' |
| 50 | + ] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + logging.debug(f'<- {body}') |
| 55 | + response.data = json.dumps(body) |
| 56 | + |
| 57 | + return response |
| 58 | + |
| 59 | + |
| 60 | +@app.route('/api/users/interrupt', methods=['GET']) |
| 61 | +def interrupt(): |
| 62 | + logging.debug('-> /api/users/interrupt') |
| 63 | + nonce = request.args.get('nonce') |
| 64 | + (uid, continue_url, service_entity_id, issuer_id) = nonces.get(nonce, ('unknown', '/', '/', '')) |
| 65 | + response = render_template('interrupt.j2', uid=uid, |
| 66 | + service_entity_id=service_entity_id, issuer_id=issuer_id, url=continue_url) |
| 67 | + |
| 68 | + return response |
| 69 | + |
| 70 | + |
| 71 | +@app.route('/api/users/attributes', methods=['POST']) |
| 72 | +def entitlements(): |
| 73 | + logging.debug('-> /api/users/attributes') |
| 74 | + debug(request) |
| 75 | + |
| 76 | + nonce = request.form.get('nonce') |
| 77 | + (uid, _, _, _) = nonces.pop(nonce) |
| 78 | + |
| 79 | + response = Response(status=200) |
| 80 | + body = { |
| 81 | + 'attributes': { |
| 82 | + 'urn:mace:dir:attribute-def:eduPersonEntitlement': [ |
| 83 | + uid, |
| 84 | + nonce, |
| 85 | + 'urn:foobar', |
| 86 | + ] |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + logging.debug(f'<- {body}') |
| 91 | + response.data = json.dumps(body) |
| 92 | + |
| 93 | + return response |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + app.run(host='0.0.0.0', port=12345, debug=True) |
0 commit comments