Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
179 lines (146 loc) · 6.86 KB
/
Copy pathapp.py
File metadata and controls
179 lines (146 loc) · 6.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from flask import Flask, render_template, jsonify, session, request, redirect, url_for
from configparser import ConfigParser
from flask_talisman import Talisman
from flask_session import Session
from flask_cors import CORS
import qrcode
import requests
import json
app = Flask(__name__)
CORS(app)
talisman = Talisman(app, content_security_policy={
'default-src': ["'self'", "*", "'unsafe-inline'"],
'frame-ancestors': ["'self'", '*'],
'style-src-elem': ["'self'", 'data:']
})
app.config['SESSION_COOKIE_SAMESITE'] = 'None'
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_FILE_THRESHOLD'] = 500 # Set a threshold for the number of files before cleanup
Session(app)
config = ConfigParser()
config.read('config.ini')
app.secret_key = config.get('DEFAULT', 'SECRET_KEY', )
connection_url = config.get('ENDPOINTS', 'CONNECTION_URL').strip("'")
issuer_url = config.get('ENDPOINTS', 'ISSUER_URL').strip("'")
cred_def = config.get('CREDENTIAL_DEFINITION', 'CREDENTIAL_DEFINITION').strip("'")
attr1 = config.get('ATTRIBUTES', 'ATTR1').strip("'")
attr2 = config.get('ATTRIBUTES', 'ATTR2').strip("'")
attr3 = config.get('ATTRIBUTES', 'ATTR3').strip("'")
# allow site to be embedded in educa.ch
# potentially used to emebed as iFrame
#@app.after_request
#def add_header(response):
# response.headers['X-Frame-Options'] = 'ALLOW-FROM https://educa.ch'
# return response
@app.route('/de')
def set_language_de():
session['lang'] = 'de'
return redirect(url_for('index'))
@app.route('/fr')
def set_language_fr():
session['lang'] = 'fr'
return redirect(url_for('index'))
@app.route('/')
def index():
# set default language to german if not set
if 'lang' not in session:
session['lang'] = 'de'
url = connection_url+ '/connection/invitation'
response = requests.post(url)
data = json.loads(response.text)
dynamic_url = data['invitationUrl']
session['connection'] = data['connectionId']
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=6, border=4)
qr.add_data(dynamic_url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("static/images/dynamic_url_qr.png") # Save the QR code image to a file
# Set the prompt based on the language
if session['lang'] == 'de':
prompt = 'Scannen Sie den QR-Code mit Ihrer elektronischen Brieftasche (Lissi Wallet) und folgen Sie den weiteren Schritten in der App.'
#notice = 'Hinweis: Der Educa Agent dient nur zu Demonstrationszwecken und ist daher nicht verifiziert. Bitte nehmen Sie die Verbindungseinladung trotzdem an.'
elif session['lang'] == 'fr':
prompt = 'Scannez le code QR avec votre portefeuille électronique (Lissi wallet) et suivez les étapes suivantes dans l\'application.'
#notice = 'Remarque: l\'Educa Agent ne sert qu\'à des fins de démonstration et n\'est donc pas vérifié. Veuillez tout de même accepter l\'invitation pour vous connecter.'
return render_template('index.html', qr_image='static/images/dynamic_url_qr.png', prompt=prompt)
@app.route('/check-connection/')
def check_connection():
# Check the connection status by making a GET request to the API endpoint
url = connection_url+ '/connection/' + session['connection']
response = requests.get(url)
if response.text == '"established"':
# Connection has been established
return jsonify({'status': 'connected'})
else:
# Connection has not been established
return jsonify({'status': 'not connected'})
@app.route('/name', methods=['POST', 'GET'])
def name():
if request.method == 'POST':
prename = request.form['prename']
surname = request.form['surname']
institution = request.form['institution']
# Make a POST request to the API endpoint
url = issuer_url + '/issue/process'
data = {
"connectionId": session['connection'],
"credentialDefinitionId": cred_def,
"attributes": {
attr1: prename,
attr2: surname,
attr3: institution
},
"userId": "Anonymous"
}
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
json=data,
headers=headers)
if response.status_code == 200:
data = json.loads(response.text)
session['processId'] = data['processId']
if session['lang'] == 'de':
prompt = 'Bitte akzeptieren Sie den gesendeten digitalen Nachweis in Ihrer elektronischen Brieftasche (Lissi Wallet).'
#notice = 'Hinweis: Der Educa Agent dient nur zu Demonstrationszwecken und ist daher nicht verifiziert. Bitte nehmen Sie den digitalen Nachweis trotzdem an.'
elif session['lang'] == 'fr':
prompt = 'Veuillez accepter le justificatif numérique envoyé dans votre portefeuille électronique (Lissi wallet).'
#notice = 'Remarque: l\'Educa Agent ne sert qu\'à des fins de démonstration et n\'est donc pas vérifié. Veuillez tout de même accepter le justificatif numérique.'
return render_template('loading.html', prompt=prompt)
else:
return render_template('failure.html')
else:
# Set the prompt based on the language
if session['lang'] == 'de':
prompt = 'Bitte füllen Sie das Formular aus und klicken Sie auf «absenden».'
prename = 'Vorname:'
surname = 'Name:'
button = 'absenden'
elif session['lang'] == 'fr':
prompt = 'Veuillez remplir le formulaire et cliquer sur «envoyer».'
prename = 'Prénom:'
surname = 'Nom:'
button = 'envoyer'
#just show the page
return render_template('name.html', prompt=prompt, button=button, prename=prename, surname=surname)
@app.route('/loading/')
def loading():
# Check the Acception status by making a GET request to the API endpoint
url = issuer_url+ '/issue/process/' + session['processId'] + '/state'
response = requests.get(url)
if response.text != '"IN_PROGRESS"':
# Credential has been accepted
return jsonify({'status': 'accepted'})
else:
# Credential has not been accepted
return jsonify({'status': 'not accepted'})
@app.route('/success')
def success():
if session['lang'] == 'de':
prompt = 'Herzliche Gratulation, Sie haben soeben die Teilnahmebestätigung zur Fachtagung als digitalen Nachweis erhalten.'
elif session['lang'] == 'fr':
prompt = 'Félicitations, vous venez de recevoir votre confirmation de participation au colloque sous forme de justificatif numérique.'
return render_template('success.html', prompt=prompt)
if __name__ == "__main__":
app.run(debug=False)