Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 5dc6d077

Přidáno uživatelem Matěj Zeman před asi 2 roky(ů)

security fix for all endpoints. Added view for Licenses and html template on "/" with information about server endpoints.

Zobrazit rozdíly:

server/sql_app/api/auth.py
1 1
from fastapi import Depends, APIRouter, Form
2 2
from fastapi import Request
3
from fastapi.responses import HTMLResponse
3
from fastapi.responses import HTMLResponse, RedirectResponse
4 4
from fastapi.templating import Jinja2Templates
5 5
from fastapi_jwt_auth import AuthJWT
6 6
from pydantic import BaseModel
server/sql_app/api/bodydevices_web.py
87 87

  
88 88

  
89 89
@body_device_web.get("/body-device-license/{device_id}", response_class=HTMLResponse)
90
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db)):
90
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db),
91
                          Authorize: AuthJWT = Depends()):
91 92
    """
92 93
    Returns template with one body device and all available licenses that can be assigned to it.
93 94
    """
95
    Authorize.jwt_optional()
96
    current_user = Authorize.get_jwt_subject()
97
    if current_user != "admin":
98
        return RedirectResponse(url=f"/logs-web", status_code=303)
94 99
    device = crud.get_body_device(db, device_id)
95 100
    dev_licenses = crud.get_bodydevice_license(db, device_id)
96 101
    lic_names = []
......
109 114

  
110 115

  
111 116
@body_device_web.post("/body-devices-web/{device_id}")
112
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db)):
117
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db),
118
                       Authorize: AuthJWT = Depends()):
113 119
    """
114 120
    Endpoint called from template for connecting body device with license. Adds entry to bodydevices_licenses
115 121
    table and redirects to body-devices-web endpoint
116 122
    """
123
    Authorize.jwt_optional()
124
    current_user = Authorize.get_jwt_subject()
125
    if current_user != "admin":
126
        return RedirectResponse(url=f"/logs-web", status_code=303)
117 127
    crud.create_body_device_license(db, device_id, int(lic), datetime.now())
118 128
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
119 129

  
120 130

  
121 131
@body_device_web.post("/body-devices-web-del/{device_id}")
122
async def delete_post(device_id: int, b_lic: str = Form(...), db: Session = Depends(get_db)):
132
async def delete_post(device_id: int, b_lic: str = Form(...), db: Session = Depends(get_db),
133
                      Authorize: AuthJWT = Depends()):
123 134
    """
124 135
    Endpoint called from template for connecting body device with license. Adds entry to devices_licenses
125 136
    table and redirects to body-devices-web endpoint
126 137
    """
138
    Authorize.jwt_optional()
139
    current_user = Authorize.get_jwt_subject()
140
    if current_user != "admin":
141
        return RedirectResponse(url=f"/logs-web", status_code=303)
127 142
    crud.delete_bodydevice_license(db, device_id, int(b_lic))
128 143
    return RedirectResponse(url=f"/body-devices-web", status_code=303)
server/sql_app/api/devices_web.py
87 87

  
88 88

  
89 89
@device_web.get("/device-license/{device_id}", response_class=HTMLResponse)
90
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db)):
90
async def connect_dev_lic(request: Request, device_id: int, db: Session = Depends(get_db),
91
                          Authorize: AuthJWT = Depends()):
91 92
    """
92 93
    Returns template with one device and all available licenses that can be assigned to it.
93 94
    """
95
    Authorize.jwt_optional()
96
    current_user = Authorize.get_jwt_subject()
97
    if current_user != "admin":
98
        return RedirectResponse(url=f"/logs-web", status_code=303)
94 99
    device = crud.get_device(db, device_id)
95 100
    dev_licenses = crud.get_device_licenses(db, device_id)
96 101
    lic_names = []
......
109 114

  
110 115

  
111 116
@device_web.post("/devices-web/{device_id}")
112
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db)):
117
async def connect_post(device_id: int, lic: str = Form(...), db: Session = Depends(get_db),
118
                       Authorize: AuthJWT = Depends()):
113 119
    """
114 120
    Endpoint called from template for connecting device with license. Adds entry to devices_licenses
115 121
    table and redirects to devices-web endpoint
116 122
    """
123
    Authorize.jwt_optional()
124
    current_user = Authorize.get_jwt_subject()
125
    if current_user != "admin":
126
        return RedirectResponse(url=f"/logs-web", status_code=303)
117 127
    crud.create_device_license(db, device_id, int(lic), datetime.now())
118 128
    return RedirectResponse(url=f"/devices-web", status_code=303)
119 129

  
120 130

  
121 131
@device_web.post("/devices-web-del/{device_id}")
122
async def delete_post(device_id: int, lic_del: str = Form(...), db: Session = Depends(get_db)):
132
async def delete_post(device_id: int, lic_del: str = Form(...), db: Session = Depends(get_db),
133
                      Authorize: AuthJWT = Depends()):
123 134
    """
124 135
    Endpoint called from template for deleting device-license connection. Adds entry to bodydevices_licenses
125 136
    table and redirects to devices-web endpoint
126 137
    """
138
    Authorize.jwt_optional()
139
    current_user = Authorize.get_jwt_subject()
140
    if current_user != "admin":
141
        return RedirectResponse(url=f"/logs-web", status_code=303)
127 142
    crud.delete_device_license(db, device_id, int(lic_del))
128 143
    return RedirectResponse(url=f"/devices-web", status_code=303)
server/sql_app/api/licenses_web.py
7 7
from ..database import SessionLocal, engine
8 8
from fastapi import FastAPI, Request
9 9
from fastapi.responses import HTMLResponse, RedirectResponse
10
from fastapi_jwt_auth import AuthJWT
10 11
from fastapi.staticfiles import StaticFiles
11 12
from fastapi.templating import Jinja2Templates
12 13

  
......
30 31

  
31 32

  
32 33
@licenses_web.get("/license-create", response_class=HTMLResponse)
33
async def licenses_create_web(request: Request):
34
async def licenses_create_web(request: Request, Authorize: AuthJWT = Depends()):
34 35
    """
35 36
    Returns template with Form for creating new license.
36 37
    """
37
    return templates.TemplateResponse("license_create.html", {"request": request})
38
    Authorize.jwt_optional()
39
    current_user = Authorize.get_jwt_subject()
40
    if current_user != "admin":
41
        return RedirectResponse(url=f"/logs-web", status_code=303)
42
    return templates.TemplateResponse("license_create.html", {"request": request, "minimum_date": date.today()})
38 43

  
39 44

  
40 45
@licenses_web.get("/licenses-web", response_class=HTMLResponse)
41
async def read_licenses_web(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
46
async def read_licenses_web(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db),
47
                            Authorize: AuthJWT = Depends()):
42 48
    """
43 49
    Returns template with all licenses currently saved in database
44 50
    """
51
    Authorize.jwt_optional()
52
    current_user = Authorize.get_jwt_subject()
45 53
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
46
    return templates.TemplateResponse("licenses.html", {"request": request, "licenses": licenses})
47

  
54
    if current_user == "admin":
55
        return templates.TemplateResponse("licenses.html", {"request": request, "licenses": licenses,
56
                                                            "user": current_user})
57
    else:
58
        current_user = "guest"
59
        return templates.TemplateResponse("licenses_normal.html", {"request": request, "licenses": licenses,
60
                                                            "user": current_user})
48 61

  
49 62
@licenses_web.post("/licenses-web")
50
def create_license(name: str = Form(...), expdate: date = Form(...), db: Session = Depends(get_db)):
63
def create_license(name: str = Form(...), expdate: date = Form(...), db: Session = Depends(get_db),
64
                   Authorize: AuthJWT = Depends()):
51 65
    """
52 66
    Endpoint called from create license form. Creates new license and redirects to devices-web endpoint
53 67
    """
54
    db_license = crud.create_license(db, name, expdate)
55
    if db_license is None:
56
        print("something went wrong")
68
    Authorize.jwt_optional()
69
    current_user = Authorize.get_jwt_subject()
70
    if current_user != "admin":
71
        return RedirectResponse(url=f"/logs-web", status_code=303)
72
    licenses = crud.get_licenses(db, 0, 100)
73
    licenses_names = []
74
    for l in licenses:
75
        licenses_names.append(l.name)
76
    if name not in licenses_names:
77
        db_license = crud.create_license(db, name, expdate)
78
        if db_license is None:
79
            print("something went wrong")
57 80
    return RedirectResponse(url=f"/devices-web", status_code=303)
server/sql_app/api/pcs_web.py
44 44

  
45 45

  
46 46
@pcs_web.get("/pc-team/{pc_id}", response_class=HTMLResponse)
47
async def connect_pc_team(request: Request, pc_id: int, db: Session = Depends(get_db)):
47
async def connect_pc_team(request: Request, pc_id: int, db: Session = Depends(get_db),
48
                          Authorize: AuthJWT = Depends()):
48 49
    """
49 50
    Returns template with Form for connecting pc with team
50 51
    """
52
    Authorize.jwt_optional()
53
    current_user = Authorize.get_jwt_subject()
54
    if current_user != "admin":
55
        return RedirectResponse(url=f"/logs-web", status_code=303)
51 56
    pc = crud.get_pc(db, pc_id)
52 57
    teams = crud.get_teams(db, 0, 100)
53 58
    return templates.TemplateResponse("pcteam.html",
......
55 60

  
56 61

  
57 62
@pcs_web.post("/pcs-web/{pc_id}")
58
async def connect_post(pc_id: int, team: str = Form(...), db: Session = Depends(get_db)):
63
async def connect_post(pc_id: int, team: str = Form(...), db: Session = Depends(get_db),
64
                       Authorize: AuthJWT = Depends()):
59 65
    """
60 66
    Endpoint called from within form for connecting pc with team. Updates certain pc with new team.
61 67
    """
68
    Authorize.jwt_optional()
69
    current_user = Authorize.get_jwt_subject()
70
    if current_user != "admin":
71
        return RedirectResponse(url=f"/logs-web", status_code=303)
62 72
    old_pc = crud.update_pc(db, pc_id, team)
63 73
    return RedirectResponse(url=f"/pcs-web", status_code=303)
server/sql_app/api/teams_web.py
45 45

  
46 46

  
47 47
@teams_web.get("/team-create", response_class=HTMLResponse)
48
async def team_create_web(request: Request):
48
async def team_create_web(request: Request, Authorize: AuthJWT = Depends()):
49 49
    """
50 50
    Returns template with form for creating new team
51 51
    """
52
    Authorize.jwt_optional()
53
    current_user = Authorize.get_jwt_subject()
54
    if current_user != "admin":
55
        return RedirectResponse(url=f"/logs-web", status_code=303)
52 56
    return templates.TemplateResponse("team_create.html", {"request": request})
53 57

  
54 58

  
55 59
@teams_web.post("/teams-web-con")
56
def create_team(name: str = Form(...), db: Session = Depends(get_db)):
60
def create_team(name: str = Form(...), db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
57 61
    """
58 62
    Endpoint called from within form for creating new team. Creates new team and returns all teams in database
59 63
    """
60
    team = crud.create_team(db, name)
61
    if team is None:
62
        print("something went wrong")
64
    Authorize.jwt_optional()
65
    current_user = Authorize.get_jwt_subject()
66
    if current_user != "admin":
67
        return RedirectResponse(url=f"/logs-web", status_code=303)
68
    teams = crud.get_teams(db, 0, 100)
69
    teams_names = []
70
    for t in teams:
71
        teams_names.append(t.name)
72
    if name not in teams_names:
73
        team = crud.create_team(db, name)
74
        if team is None:
75
            print("something went wrong")
63 76
    return RedirectResponse(url=f"/teams-web", status_code=303)
server/sql_app/api/usb_logs_web.py
53 53
@usblogs_web.post("/logs-web", response_class=HTMLResponse)
54 54
async def filter_logs(request: Request, pc: str = Form("all"), team: str = Form("all"), lic: str = Form("all"),
55 55
                      skip: int = 0, limit: int = 100,
56
                      db: Session = Depends(get_db)):
56
                      db: Session = Depends(get_db), Authorize: AuthJWT = Depends()):
57 57
    """
58 58
    Endpoint used for filtering usb logs by user given form inputs.
59 59
    """
60
    Authorize.jwt_optional()
61
    current_user = Authorize.get_jwt_subject()
60 62
    log = crud.get_filtered_logs(db, pc, team, lic)
61 63
    logs_ids = []
62 64
    for l in log:
......
65 67
    pc_obj = crud.get_pcs(db, skip=skip, limit=limit)
66 68
    teams = crud.get_teams(db, skip=skip, limit=limit)
67 69
    licenses = crud.get_licenses(db, skip=skip, limit=limit)
70
    if current_user != "admin":
71
        current_user = "guest"
68 72
    return templates.TemplateResponse("logs.html", {"request": request, "logs": logs, "pcs": pc_obj, "teams": teams,
69
                                                    "licenses": licenses})
73
                                                    "licenses": licenses, "user": current_user})
74

  
70 75

  
76
@usblogs_web.get("/", response_class=HTMLResponse)
77
async def crossroad(request: Request):
78
    return templates.TemplateResponse("crossroad.html", {"request": request})
server/sql_app/main.py
36 36

  
37 37
'''
38 38
if __name__ == "__main__":
39
    uvicorn.run(app, host="192.168.176.1", port=8000)
39
    uvicorn.run(app, host="192.168.0.22", port=8000)
40 40
'''
server/templates/auth/login.html
8 8
<form action="/login" method="post">
9 9
  <label for="username">Username:</label><br>
10 10
  <input type="text" id="username" name="username"><br><br>
11
    <label for="password">Expiration Date</label>
12
  <input type="text" id="password" name="password">
11
    <label for="password">Password</label>
12
  <input type="password" id="password" name="password">
13 13
  <input type="submit" value="Submit">
14 14
</form>
15 15
</body>
server/templates/body-devices/body_devices.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/body-devices/body_devices_normal.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/devices/devices.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/devices/devices_normal.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/ld-logs/ldlogs.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/licenses/license_create.html
9 9
  <label for="name">Name:</label><br>
10 10
  <input type="text" id="name" name="name"><br><br>
11 11
    <label for="expdate">Expiration Date</label>
12
  <input type="date" id="expdate" name="expdate">
12
  <input type="date" id="expdate" name="expdate" min={{minimum_date}}>
13 13
  <input type="submit" value="Submit">
14 14
</form>
15 15
</body>
server/templates/licenses/licenses.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
......
23 24
    <input type="submit" value="Login" />
24 25
</form>
25 26
</div>
27
<div style='float:left'>
26 28
<form action="/logout" method="get">
27 29
    <input type="submit" value="Logout" />
28 30
</form>
31
</div>
32
<h4>{{user}}</h4>
29 33
<table>
30 34
    <TR>
31 35
        <TH>ID</TH>
......
40 44
    </TR>
41 45
    {% endfor %}
42 46
</table>
47
<form action="/license-create" method="get">
48
    <input type="submit" value="Add">
49
</form>
43 50
</body>
44 51
</html>
server/templates/licenses/licenses_normal.html
1
<html>
2
<head>
3
    <title>Licenses Details</title>
4
</head>
5
<body>
6
<div style='float:left'>
7
<form action="" method="get">
8
  <label for="view">Choose view:</label>
9
  <select id="view" name="view" onchange="this.form.action=this.value;">
10
      <option value=""></option>
11
      <option value="/logs-web">Logs</option>
12
      <option value="/ldlogs-web">LD Logs</option>
13
      <option value="/devices-web">Devices</option>
14
      <option value="/body-devices-web">Body Devices</option>
15
      <option value="/teams-web">Teams</option>
16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
18
  </select>
19
  <input type="submit" value="OK">
20
</form>
21
</div>
22
<div style='float:left'>
23
<form action="/login" method="get">
24
    <input type="submit" value="Login" />
25
</form>
26
</div>
27
<div style='float:left'>
28
<form action="/logout" method="get">
29
    <input type="submit" value="Logout" />
30
</form>
31
</div>
32
<h4>{{user}}</h4>
33
<table>
34
    <TR>
35
        <TH>ID</TH>
36
        <TH>Name</TH>
37
        <TH>Expiration Date</TH>
38
    </TR>
39
    {% for license in licenses %}
40
    <TR>
41
        <TD class="ID">{{license.id}}</TD>
42
        <TD class="Vendor ID">{{license.name}}</TD>
43
        <TD class="Product ID">{{license.expiration_date}}</TD>
44
    </TR>
45
    {% endfor %}
46
</table>
47
</body>
48
</html>
server/templates/pcs/pcs.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/pcs/pcs_normal.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
server/templates/teams/teams.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
......
33 34
    <TR>
34 35
        <TH>ID</TH>
35 36
        <TH>Name</TH>
37
        <TH>Members</TH>
36 38
    </TR>
37 39
    {% for team in teams %}
38 40
    <TR>
39 41
        <TD class="ID">{{team.id}}</TD>
40
        <TD class="Vendor ID">{{team.name}}</TD>
42
        <TD class="Name">{{team.name}}</TD>
43
        <TD class="Members">
44
        {% for ppl in team.pcs %}
45
            {{ppl.username}}<BR>
46
        {% endfor %}
47
        </TD>
41 48
    </TR>
42 49
    {% endfor %}
43 50
</table>
server/templates/teams/teams_normal.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>
......
33 34
    <TR>
34 35
        <TH>ID</TH>
35 36
        <TH>Name</TH>
37
        <TH>Members</TH>
36 38
    </TR>
37 39
    {% for team in teams %}
38 40
    <TR>
39 41
        <TD class="ID">{{team.id}}</TD>
40 42
        <TD class="Vendor ID">{{team.name}}</TD>
43
        <TD class="Members">
44
        {% for ppl in team.pcs %}
45
            {{ppl.username}}<BR>
46
        {% endfor %}
47
        </TD>
41 48
    </TR>
42 49
    {% endfor %}
43 50
</table>
server/templates/usb-logs/crossroad.html
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="UTF-8">
5
    <title>Crossroad</title>
6
</head>
7
<body>
8
<form action="" method="get">
9
  <label for="view">Choose view:</label>
10
  <select id="view" name="view" onchange="this.form.action=this.value;">
11
      <option value=""></option>
12
      <option value="/logs-web">Logs</option>
13
      <option value="/ldlogs-web">LD Logs</option>
14
      <option value="/devices-web">Devices</option>
15
      <option value="/body-devices-web">Body Devices</option>
16
      <option value="/teams-web">Teams</option>
17
      <option value="/pcs-web">PCs</option>
18
      <option value="/licenses-web">Licenses</option>
19
  </select>
20
  <input type="submit" value="OK">
21
</form>
22
<h3>Available endpoints are accessible from selectBox above</h3>
23
<h4>Endpoints are:</h4>
24
<table>
25
    <TR>
26
        <TH>URL</TH>
27
        <TH>Purpose</TH>
28
    </TR>
29
    <TR>
30
        <TD class="URL">/logs-web</TD>
31
        <TD class="Purpose">Shows all saves Keyman logs. User can filter through license, team and user</TD>
32
    </TR>
33
    <TR>
34
        <TD class="URL">/ldlogs-web</TD>
35
        <TD class="Purpose">Shows all saves LD debugger logs. User can filter through license, team and user</TD>
36
    </TR>
37
    <TR>
38
        <TD class="URL">/devices-web</TD>
39
        <TD class="Purpose">Shows all Keyman devices saved in database and its last state (connected, disconnected)</TD>
40
    </TR>
41
    <TR>
42
        <TD class="URL">/body-devices-web</TD>
43
        <TD class="Purpose">Shows all LD Body devices saved in database and its last state (connected, disconnected)</TD>
44
    </TR>
45
    <TR>
46
        <TD class="URL">/teams-web</TD>
47
        <TD class="Purpose">Shows all teams currently saved in database and its members</TD>
48
    </TR>
49
    <TR>
50
        <TD class="URL">/pcs-web</TD>
51
        <TD class="Purpose">Shows all PCS currently saved in database and its team</TD>
52
    </TR>
53
    <TR>
54
        <TD class="URL">/licenses-web</TD>
55
        <TD class="Purpose">Shows all Licenses currently saved in database and its expiration date</TD>
56
    </TR>
57
</table>
58
</body>
59
</html>
server/templates/usb-logs/logs.html
14 14
      <option value="/body-devices-web">Body Devices</option>
15 15
      <option value="/teams-web">Teams</option>
16 16
      <option value="/pcs-web">PCs</option>
17
      <option value="/licenses-web">Licenses</option>
17 18
  </select>
18 19
  <input type="submit" value="OK">
19 20
</form>

Také k dispozici: Unified diff