|
1 | 1 | import os
|
| 2 | +import secrets |
| 3 | +from tempfile import mkstemp |
2 | 4 | import logging
|
3 | 5 |
|
| 6 | + |
4 | 7 | logger = logging.getLogger(__name__)
|
5 |
| -logger.setLevel('INFO') |
| 8 | +logger.setLevel("INFO") |
6 | 9 |
|
7 | 10 | HERE = os.path.dirname(os.path.abspath(__file__))
|
8 | 11 |
|
9 | 12 |
|
10 | 13 | def _trame_mappath(path):
|
| 14 | + # Append authKey to URL if we are at the base-url |
| 15 | + if path in ("/", "/index.html"): |
| 16 | + path += f"?secret={_auth_key}" |
| 17 | + |
11 | 18 | return path
|
12 | 19 |
|
13 | 20 |
|
14 | 21 | def setup_trame():
|
15 |
| - """ Setup commands and and return a dictionary compatible |
16 |
| - with jupyter-server-proxy. |
17 |
| - """ |
| 22 | + # Generate authKey |
| 23 | + global _auth_key |
| 24 | + _auth_key = secrets.token_urlsafe(32) |
| 25 | + |
| 26 | + # Write authKey to tempfile |
| 27 | + tempfile, temppath = mkstemp() |
| 28 | + logger.info(f"Created tempfile with secure password for trame at {temppath}") |
18 | 29 |
|
19 |
| - # create command |
| 30 | + with open(tempfile, "w") as file: |
| 31 | + file.write(_auth_key) |
| 32 | + |
| 33 | + # Create command |
20 | 34 | cmd = [
|
21 |
| - os.path.join(HERE, 'share/launch_trame.sh'), "{port}" |
| 35 | + os.path.join(HERE, "share/launch_trame.sh"), |
| 36 | + "{port}", |
| 37 | + "--authKeyFile", |
| 38 | + temppath, |
22 | 39 | ]
|
23 |
| - |
24 |
| - logger.info('Command: ' + ' '.join(cmd)) |
| 40 | + |
| 41 | + logger.info(f"jupyter-trame-proxy command: {' '.join(cmd)}") |
25 | 42 |
|
26 | 43 | return {
|
27 |
| - 'command': cmd, |
28 |
| - 'mappath': _trame_mappath, |
29 |
| - 'absolute_url': False, |
30 |
| - 'timeout': 90, |
31 |
| - 'new_browser_tab': True, |
32 |
| - 'launcher_entry': { |
33 |
| - 'enabled': True, |
34 |
| - 'icon_path': os.path.join(HERE, 'icons/logo.svg'), |
35 |
| - 'title': 'ParaView trame', |
| 44 | + "command": cmd, |
| 45 | + "mappath": _trame_mappath, |
| 46 | + "absolute_url": False, |
| 47 | + "timeout": 90, |
| 48 | + "new_browser_tab": True, |
| 49 | + "launcher_entry": { |
| 50 | + "enabled": True, |
| 51 | + "icon_path": os.path.join(HERE, "icons/logo.svg"), |
| 52 | + "title": "ParaView trame", |
36 | 53 | },
|
37 | 54 | }
|
0 commit comments