1
|
<!doctype html>
|
2
|
<html lang="en">
|
3
|
<head>
|
4
|
<meta charset="utf-8">
|
5
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
|
6
|
<meta name="generator" content="pdoc 0.10.0" />
|
7
|
<title>sql_app.api.auth API documentation</title>
|
8
|
<meta name="description" content="" />
|
9
|
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
|
10
|
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
|
11
|
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
|
12
|
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
|
13
|
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
|
14
|
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
|
15
|
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
|
16
|
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
|
17
|
</head>
|
18
|
<body>
|
19
|
<main>
|
20
|
<article id="content">
|
21
|
<header>
|
22
|
<h1 class="title">Module <code>sql_app.api.auth</code></h1>
|
23
|
</header>
|
24
|
<section id="section-intro">
|
25
|
<details class="source">
|
26
|
<summary>
|
27
|
<span>Expand source code</span>
|
28
|
</summary>
|
29
|
<pre><code class="python">from fastapi import Depends, APIRouter, Form
|
30
|
from fastapi import Request
|
31
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
32
|
from fastapi.templating import Jinja2Templates
|
33
|
from fastapi_jwt_auth import AuthJWT
|
34
|
from sqlalchemy.orm import Session
|
35
|
from sql_app import crud
|
36
|
from passlib.context import CryptContext
|
37
|
from pydantic import BaseModel
|
38
|
from ..database import SessionLocal, engine
|
39
|
|
40
|
# Path to html templates used in this file
|
41
|
templates = Jinja2Templates(directory="../templates/auth")
|
42
|
|
43
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
44
|
|
45
|
# prefix used for all endpoints in this file
|
46
|
auth = APIRouter(prefix="")
|
47
|
|
48
|
|
49
|
# Dependency
|
50
|
def get_db():
|
51
|
db = SessionLocal()
|
52
|
try:
|
53
|
yield db
|
54
|
finally:
|
55
|
db.close()
|
56
|
|
57
|
|
58
|
class Settings(BaseModel):
|
59
|
authjwt_secret_key: str = "secret"
|
60
|
# Configure application to store and get JWT from cookies
|
61
|
authjwt_token_location: set = {"cookies"}
|
62
|
# Disable CSRF Protection for this example. default is True
|
63
|
authjwt_cookie_csrf_protect: bool = False
|
64
|
|
65
|
|
66
|
@AuthJWT.load_config
|
67
|
def get_config():
|
68
|
return Settings()
|
69
|
|
70
|
|
71
|
# admin username and password
|
72
|
fake_users_db = {
|
73
|
"admin": {
|
74
|
"username": "admin",
|
75
|
"password": "admin"
|
76
|
}
|
77
|
}
|
78
|
|
79
|
|
80
|
def verify_password(plain_password, hashed_password):
|
81
|
"""
|
82
|
Verifies plain text password with hashed password
|
83
|
"""
|
84
|
return pwd_context.verify(plain_password, hashed_password)
|
85
|
|
86
|
|
87
|
def get_hash_password(password):
|
88
|
"""
|
89
|
Returns hashed password
|
90
|
"""
|
91
|
return pwd_context.hash(password)
|
92
|
|
93
|
|
94
|
def auth_user(db, username: str, password: str):
|
95
|
"""
|
96
|
Determines if given password belongs to user with given username
|
97
|
"""
|
98
|
user = crud.find_user(db, username)
|
99
|
if not user:
|
100
|
return None
|
101
|
if not verify_password(password, user.password):
|
102
|
return None
|
103
|
return user
|
104
|
|
105
|
|
106
|
@auth.get("/signup", response_class=HTMLResponse)
|
107
|
async def signup_get(request: Request):
|
108
|
"""
|
109
|
return html template for signup
|
110
|
"""
|
111
|
return templates.TemplateResponse("signup.html", {"request": request})
|
112
|
|
113
|
|
114
|
@auth.post("/signup", response_class=HTMLResponse)
|
115
|
async def signup(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)):
|
116
|
"""
|
117
|
Endpoint called form signup template. Creates new user with role guest that can be changed by admin user
|
118
|
"""
|
119
|
users = crud.get_users(db, 0, 100)
|
120
|
users_names = []
|
121
|
for u in users:
|
122
|
users_names.append(u.username)
|
123
|
if username not in users_names:
|
124
|
new_user = crud.create_user(db, username, get_hash_password(password), "guest")
|
125
|
if new_user is None:
|
126
|
print("something went wrong")
|
127
|
return """
|
128
|
<html>
|
129
|
<head>
|
130
|
<title>Signup</title>
|
131
|
</head>
|
132
|
<body>
|
133
|
<h1>New user created. You can go back to previous page.</h1>
|
134
|
<form action="/logs-web" method="get">
|
135
|
<input type="submit" value="Home Page" />
|
136
|
</form>
|
137
|
</body>
|
138
|
</html>
|
139
|
"""
|
140
|
else:
|
141
|
return """
|
142
|
<html>
|
143
|
<head>
|
144
|
<title>Signup</title>
|
145
|
</head>
|
146
|
<body>
|
147
|
<h1>Username taken. Try to choose different username.</h1>
|
148
|
<form action="/logs-web" method="get">
|
149
|
<input type="submit" value="Home Page" />
|
150
|
</form>
|
151
|
</body>
|
152
|
</html>
|
153
|
"""
|
154
|
|
155
|
@auth.get("/login", response_class=HTMLResponse)
|
156
|
async def login_get(request: Request):
|
157
|
"""
|
158
|
return html template for login
|
159
|
"""
|
160
|
return templates.TemplateResponse("login.html", {"request": request})
|
161
|
|
162
|
|
163
|
@auth.post("/login", response_class=HTMLResponse)
|
164
|
async def login(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db),
|
165
|
Authorize: AuthJWT = Depends()):
|
166
|
"""
|
167
|
Endpoint called from login template. Checks if given username and password aligns with admin
|
168
|
username and password and returns token for browser according to given username and password
|
169
|
"""
|
170
|
user = auth_user(db, username, password)
|
171
|
if user != None:
|
172
|
if user.role == "admin":
|
173
|
access_token = Authorize.create_access_token(subject="admin", expires_time=False)
|
174
|
refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
|
175
|
else:
|
176
|
access_token = Authorize.create_access_token(subject="guest", expires_time=False)
|
177
|
refresh_token = Authorize.create_refresh_token(subject="guest", expires_time=False)
|
178
|
else:
|
179
|
usr = fake_users_db.get(username)
|
180
|
if usr != None:
|
181
|
if usr["username"] == username and usr["password"] == password:
|
182
|
access_token = Authorize.create_access_token(subject="admin", expires_time=False)
|
183
|
refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
|
184
|
else:
|
185
|
return """
|
186
|
<html>
|
187
|
<head>
|
188
|
<title>Login</title>
|
189
|
</head>
|
190
|
<body>
|
191
|
<h1>Wrong Username or Password</h1>
|
192
|
<form action="/login" method="get">
|
193
|
<input type="submit" value="Log again" />
|
194
|
</form>
|
195
|
<form action="/login" method="get">
|
196
|
<input type="submit" value="Home Page" />
|
197
|
</form>
|
198
|
</body>
|
199
|
</html>
|
200
|
"""
|
201
|
|
202
|
# Set the JWT cookies in the response
|
203
|
Authorize.set_access_cookies(access_token)
|
204
|
Authorize.set_refresh_cookies(refresh_token)
|
205
|
return """
|
206
|
<html>
|
207
|
<head>
|
208
|
<title>Login</title>
|
209
|
</head>
|
210
|
<body>
|
211
|
<h1>Now you are logged in, you can continue to previous page.</h1>
|
212
|
<form action="/logs-web" method="get">
|
213
|
<input type="submit" value="Home Page" />
|
214
|
</form>
|
215
|
</body>
|
216
|
</html>
|
217
|
"""
|
218
|
|
219
|
|
220
|
@auth.post('/refresh')
|
221
|
def refresh(Authorize: AuthJWT = Depends()):
|
222
|
"""
|
223
|
endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
|
224
|
unlimited.
|
225
|
"""
|
226
|
Authorize.jwt_refresh_token_required()
|
227
|
current_user = Authorize.get_jwt_subject()
|
228
|
new_access_token = Authorize.create_access_token(subject=current_user)
|
229
|
# Set the JWT cookies in the response
|
230
|
Authorize.set_access_cookies(new_access_token)
|
231
|
return {"msg": "The token has been refresh"}
|
232
|
|
233
|
|
234
|
@auth.get('/logout', response_class=HTMLResponse)
|
235
|
def logout(Authorize: AuthJWT = Depends()):
|
236
|
"""
|
237
|
Endpoint for deleting cookie token with acces role.
|
238
|
"""
|
239
|
Authorize.jwt_optional()
|
240
|
|
241
|
Authorize.unset_jwt_cookies()
|
242
|
return """
|
243
|
<html>
|
244
|
<head>
|
245
|
<title>Logout</title>
|
246
|
</head>
|
247
|
<body>
|
248
|
<h1>Logged Out</h1>
|
249
|
<form action="/logs-web" method="get">
|
250
|
<input type="submit" value="Back" />
|
251
|
</form>
|
252
|
</body>
|
253
|
</html>
|
254
|
"""</code></pre>
|
255
|
</details>
|
256
|
</section>
|
257
|
<section>
|
258
|
</section>
|
259
|
<section>
|
260
|
</section>
|
261
|
<section>
|
262
|
<h2 class="section-title" id="header-functions">Functions</h2>
|
263
|
<dl>
|
264
|
<dt id="sql_app.api.auth.auth_user"><code class="name flex">
|
265
|
<span>def <span class="ident">auth_user</span></span>(<span>db, username: str, password: str)</span>
|
266
|
</code></dt>
|
267
|
<dd>
|
268
|
<div class="desc"><p>Determines if given password belongs to user with given username</p></div>
|
269
|
<details class="source">
|
270
|
<summary>
|
271
|
<span>Expand source code</span>
|
272
|
</summary>
|
273
|
<pre><code class="python">def auth_user(db, username: str, password: str):
|
274
|
"""
|
275
|
Determines if given password belongs to user with given username
|
276
|
"""
|
277
|
user = crud.find_user(db, username)
|
278
|
if not user:
|
279
|
return None
|
280
|
if not verify_password(password, user.password):
|
281
|
return None
|
282
|
return user</code></pre>
|
283
|
</details>
|
284
|
</dd>
|
285
|
<dt id="sql_app.api.auth.get_db"><code class="name flex">
|
286
|
<span>def <span class="ident">get_db</span></span>(<span>)</span>
|
287
|
</code></dt>
|
288
|
<dd>
|
289
|
<div class="desc"></div>
|
290
|
<details class="source">
|
291
|
<summary>
|
292
|
<span>Expand source code</span>
|
293
|
</summary>
|
294
|
<pre><code class="python">def get_db():
|
295
|
db = SessionLocal()
|
296
|
try:
|
297
|
yield db
|
298
|
finally:
|
299
|
db.close()</code></pre>
|
300
|
</details>
|
301
|
</dd>
|
302
|
<dt id="sql_app.api.auth.get_hash_password"><code class="name flex">
|
303
|
<span>def <span class="ident">get_hash_password</span></span>(<span>password)</span>
|
304
|
</code></dt>
|
305
|
<dd>
|
306
|
<div class="desc"><p>Returns hashed password</p></div>
|
307
|
<details class="source">
|
308
|
<summary>
|
309
|
<span>Expand source code</span>
|
310
|
</summary>
|
311
|
<pre><code class="python">def get_hash_password(password):
|
312
|
"""
|
313
|
Returns hashed password
|
314
|
"""
|
315
|
return pwd_context.hash(password)</code></pre>
|
316
|
</details>
|
317
|
</dd>
|
318
|
<dt id="sql_app.api.auth.login"><code class="name flex">
|
319
|
<span>async def <span class="ident">login</span></span>(<span>username: str = Form(Ellipsis), password: str = Form(Ellipsis), db: sqlalchemy.orm.session.Session = Depends(get_db), Authorize: fastapi_jwt_auth.auth_jwt.AuthJWT = Depends(NoneType))</span>
|
320
|
</code></dt>
|
321
|
<dd>
|
322
|
<div class="desc"><p>Endpoint called from login template. Checks if given username and password aligns with admin
|
323
|
username and password and returns token for browser according to given username and password</p></div>
|
324
|
<details class="source">
|
325
|
<summary>
|
326
|
<span>Expand source code</span>
|
327
|
</summary>
|
328
|
<pre><code class="python">@auth.post("/login", response_class=HTMLResponse)
|
329
|
async def login(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db),
|
330
|
Authorize: AuthJWT = Depends()):
|
331
|
"""
|
332
|
Endpoint called from login template. Checks if given username and password aligns with admin
|
333
|
username and password and returns token for browser according to given username and password
|
334
|
"""
|
335
|
user = auth_user(db, username, password)
|
336
|
if user != None:
|
337
|
if user.role == "admin":
|
338
|
access_token = Authorize.create_access_token(subject="admin", expires_time=False)
|
339
|
refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
|
340
|
else:
|
341
|
access_token = Authorize.create_access_token(subject="guest", expires_time=False)
|
342
|
refresh_token = Authorize.create_refresh_token(subject="guest", expires_time=False)
|
343
|
else:
|
344
|
usr = fake_users_db.get(username)
|
345
|
if usr != None:
|
346
|
if usr["username"] == username and usr["password"] == password:
|
347
|
access_token = Authorize.create_access_token(subject="admin", expires_time=False)
|
348
|
refresh_token = Authorize.create_refresh_token(subject="admin", expires_time=False)
|
349
|
else:
|
350
|
return """
|
351
|
<html>
|
352
|
<head>
|
353
|
<title>Login</title>
|
354
|
</head>
|
355
|
<body>
|
356
|
<h1>Wrong Username or Password</h1>
|
357
|
<form action="/login" method="get">
|
358
|
<input type="submit" value="Log again" />
|
359
|
</form>
|
360
|
<form action="/login" method="get">
|
361
|
<input type="submit" value="Home Page" />
|
362
|
</form>
|
363
|
</body>
|
364
|
</html>
|
365
|
"""
|
366
|
|
367
|
# Set the JWT cookies in the response
|
368
|
Authorize.set_access_cookies(access_token)
|
369
|
Authorize.set_refresh_cookies(refresh_token)
|
370
|
return """
|
371
|
<html>
|
372
|
<head>
|
373
|
<title>Login</title>
|
374
|
</head>
|
375
|
<body>
|
376
|
<h1>Now you are logged in, you can continue to previous page.</h1>
|
377
|
<form action="/logs-web" method="get">
|
378
|
<input type="submit" value="Home Page" />
|
379
|
</form>
|
380
|
</body>
|
381
|
</html>
|
382
|
"""</code></pre>
|
383
|
</details>
|
384
|
</dd>
|
385
|
<dt id="sql_app.api.auth.login_get"><code class="name flex">
|
386
|
<span>async def <span class="ident">login_get</span></span>(<span>request: starlette.requests.Request)</span>
|
387
|
</code></dt>
|
388
|
<dd>
|
389
|
<div class="desc"><p>return html template for login</p></div>
|
390
|
<details class="source">
|
391
|
<summary>
|
392
|
<span>Expand source code</span>
|
393
|
</summary>
|
394
|
<pre><code class="python">@auth.get("/login", response_class=HTMLResponse)
|
395
|
async def login_get(request: Request):
|
396
|
"""
|
397
|
return html template for login
|
398
|
"""
|
399
|
return templates.TemplateResponse("login.html", {"request": request})</code></pre>
|
400
|
</details>
|
401
|
</dd>
|
402
|
<dt id="sql_app.api.auth.logout"><code class="name flex">
|
403
|
<span>def <span class="ident">logout</span></span>(<span>Authorize: fastapi_jwt_auth.auth_jwt.AuthJWT = Depends(NoneType))</span>
|
404
|
</code></dt>
|
405
|
<dd>
|
406
|
<div class="desc"><p>Endpoint for deleting cookie token with acces role.</p></div>
|
407
|
<details class="source">
|
408
|
<summary>
|
409
|
<span>Expand source code</span>
|
410
|
</summary>
|
411
|
<pre><code class="python">@auth.get('/logout', response_class=HTMLResponse)
|
412
|
def logout(Authorize: AuthJWT = Depends()):
|
413
|
"""
|
414
|
Endpoint for deleting cookie token with acces role.
|
415
|
"""
|
416
|
Authorize.jwt_optional()
|
417
|
|
418
|
Authorize.unset_jwt_cookies()
|
419
|
return """
|
420
|
<html>
|
421
|
<head>
|
422
|
<title>Logout</title>
|
423
|
</head>
|
424
|
<body>
|
425
|
<h1>Logged Out</h1>
|
426
|
<form action="/logs-web" method="get">
|
427
|
<input type="submit" value="Back" />
|
428
|
</form>
|
429
|
</body>
|
430
|
</html>
|
431
|
"""</code></pre>
|
432
|
</details>
|
433
|
</dd>
|
434
|
<dt id="sql_app.api.auth.refresh"><code class="name flex">
|
435
|
<span>def <span class="ident">refresh</span></span>(<span>Authorize: fastapi_jwt_auth.auth_jwt.AuthJWT = Depends(NoneType))</span>
|
436
|
</code></dt>
|
437
|
<dd>
|
438
|
<div class="desc"><p>endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
|
439
|
unlimited.</p></div>
|
440
|
<details class="source">
|
441
|
<summary>
|
442
|
<span>Expand source code</span>
|
443
|
</summary>
|
444
|
<pre><code class="python">@auth.post('/refresh')
|
445
|
def refresh(Authorize: AuthJWT = Depends()):
|
446
|
"""
|
447
|
endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
|
448
|
unlimited.
|
449
|
"""
|
450
|
Authorize.jwt_refresh_token_required()
|
451
|
current_user = Authorize.get_jwt_subject()
|
452
|
new_access_token = Authorize.create_access_token(subject=current_user)
|
453
|
# Set the JWT cookies in the response
|
454
|
Authorize.set_access_cookies(new_access_token)
|
455
|
return {"msg": "The token has been refresh"}</code></pre>
|
456
|
</details>
|
457
|
</dd>
|
458
|
<dt id="sql_app.api.auth.signup"><code class="name flex">
|
459
|
<span>async def <span class="ident">signup</span></span>(<span>username: str = Form(Ellipsis), password: str = Form(Ellipsis), db: sqlalchemy.orm.session.Session = Depends(get_db))</span>
|
460
|
</code></dt>
|
461
|
<dd>
|
462
|
<div class="desc"><p>Endpoint called form signup template. Creates new user with role guest that can be changed by admin user</p></div>
|
463
|
<details class="source">
|
464
|
<summary>
|
465
|
<span>Expand source code</span>
|
466
|
</summary>
|
467
|
<pre><code class="python">@auth.post("/signup", response_class=HTMLResponse)
|
468
|
async def signup(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)):
|
469
|
"""
|
470
|
Endpoint called form signup template. Creates new user with role guest that can be changed by admin user
|
471
|
"""
|
472
|
users = crud.get_users(db, 0, 100)
|
473
|
users_names = []
|
474
|
for u in users:
|
475
|
users_names.append(u.username)
|
476
|
if username not in users_names:
|
477
|
new_user = crud.create_user(db, username, get_hash_password(password), "guest")
|
478
|
if new_user is None:
|
479
|
print("something went wrong")
|
480
|
return """
|
481
|
<html>
|
482
|
<head>
|
483
|
<title>Signup</title>
|
484
|
</head>
|
485
|
<body>
|
486
|
<h1>New user created. You can go back to previous page.</h1>
|
487
|
<form action="/logs-web" method="get">
|
488
|
<input type="submit" value="Home Page" />
|
489
|
</form>
|
490
|
</body>
|
491
|
</html>
|
492
|
"""
|
493
|
else:
|
494
|
return """
|
495
|
<html>
|
496
|
<head>
|
497
|
<title>Signup</title>
|
498
|
</head>
|
499
|
<body>
|
500
|
<h1>Username taken. Try to choose different username.</h1>
|
501
|
<form action="/logs-web" method="get">
|
502
|
<input type="submit" value="Home Page" />
|
503
|
</form>
|
504
|
</body>
|
505
|
</html>
|
506
|
"""</code></pre>
|
507
|
</details>
|
508
|
</dd>
|
509
|
<dt id="sql_app.api.auth.signup_get"><code class="name flex">
|
510
|
<span>async def <span class="ident">signup_get</span></span>(<span>request: starlette.requests.Request)</span>
|
511
|
</code></dt>
|
512
|
<dd>
|
513
|
<div class="desc"><p>return html template for signup</p></div>
|
514
|
<details class="source">
|
515
|
<summary>
|
516
|
<span>Expand source code</span>
|
517
|
</summary>
|
518
|
<pre><code class="python">@auth.get("/signup", response_class=HTMLResponse)
|
519
|
async def signup_get(request: Request):
|
520
|
"""
|
521
|
return html template for signup
|
522
|
"""
|
523
|
return templates.TemplateResponse("signup.html", {"request": request})</code></pre>
|
524
|
</details>
|
525
|
</dd>
|
526
|
<dt id="sql_app.api.auth.verify_password"><code class="name flex">
|
527
|
<span>def <span class="ident">verify_password</span></span>(<span>plain_password, hashed_password)</span>
|
528
|
</code></dt>
|
529
|
<dd>
|
530
|
<div class="desc"><p>Verifies plain text password with hashed password</p></div>
|
531
|
<details class="source">
|
532
|
<summary>
|
533
|
<span>Expand source code</span>
|
534
|
</summary>
|
535
|
<pre><code class="python">def verify_password(plain_password, hashed_password):
|
536
|
"""
|
537
|
Verifies plain text password with hashed password
|
538
|
"""
|
539
|
return pwd_context.verify(plain_password, hashed_password)</code></pre>
|
540
|
</details>
|
541
|
</dd>
|
542
|
</dl>
|
543
|
</section>
|
544
|
<section>
|
545
|
<h2 class="section-title" id="header-classes">Classes</h2>
|
546
|
<dl>
|
547
|
<dt id="sql_app.api.auth.Settings"><code class="flex name class">
|
548
|
<span>class <span class="ident">Settings</span></span>
|
549
|
<span>(</span><span>**data: Any)</span>
|
550
|
</code></dt>
|
551
|
<dd>
|
552
|
<div class="desc"><p>Create a new model by parsing and validating input data from keyword arguments.</p>
|
553
|
<p>Raises ValidationError if the input data cannot be parsed to form a valid model.</p></div>
|
554
|
<details class="source">
|
555
|
<summary>
|
556
|
<span>Expand source code</span>
|
557
|
</summary>
|
558
|
<pre><code class="python">class Settings(BaseModel):
|
559
|
authjwt_secret_key: str = "secret"
|
560
|
# Configure application to store and get JWT from cookies
|
561
|
authjwt_token_location: set = {"cookies"}
|
562
|
# Disable CSRF Protection for this example. default is True
|
563
|
authjwt_cookie_csrf_protect: bool = False</code></pre>
|
564
|
</details>
|
565
|
<h3>Ancestors</h3>
|
566
|
<ul class="hlist">
|
567
|
<li>pydantic.main.BaseModel</li>
|
568
|
<li>pydantic.utils.Representation</li>
|
569
|
</ul>
|
570
|
<h3>Class variables</h3>
|
571
|
<dl>
|
572
|
<dt id="sql_app.api.auth.Settings.authjwt_cookie_csrf_protect"><code class="name">var <span class="ident">authjwt_cookie_csrf_protect</span> : bool</code></dt>
|
573
|
<dd>
|
574
|
<div class="desc"></div>
|
575
|
</dd>
|
576
|
<dt id="sql_app.api.auth.Settings.authjwt_secret_key"><code class="name">var <span class="ident">authjwt_secret_key</span> : str</code></dt>
|
577
|
<dd>
|
578
|
<div class="desc"></div>
|
579
|
</dd>
|
580
|
<dt id="sql_app.api.auth.Settings.authjwt_token_location"><code class="name">var <span class="ident">authjwt_token_location</span> : set</code></dt>
|
581
|
<dd>
|
582
|
<div class="desc"></div>
|
583
|
</dd>
|
584
|
</dl>
|
585
|
</dd>
|
586
|
</dl>
|
587
|
</section>
|
588
|
</article>
|
589
|
<nav id="sidebar">
|
590
|
<h1>Index</h1>
|
591
|
<div class="toc">
|
592
|
<ul></ul>
|
593
|
</div>
|
594
|
<ul id="index">
|
595
|
<li><h3>Super-module</h3>
|
596
|
<ul>
|
597
|
<li><code><a title="sql_app.api" href="index.html">sql_app.api</a></code></li>
|
598
|
</ul>
|
599
|
</li>
|
600
|
<li><h3><a href="#header-functions">Functions</a></h3>
|
601
|
<ul class="two-column">
|
602
|
<li><code><a title="sql_app.api.auth.auth_user" href="#sql_app.api.auth.auth_user">auth_user</a></code></li>
|
603
|
<li><code><a title="sql_app.api.auth.get_db" href="#sql_app.api.auth.get_db">get_db</a></code></li>
|
604
|
<li><code><a title="sql_app.api.auth.get_hash_password" href="#sql_app.api.auth.get_hash_password">get_hash_password</a></code></li>
|
605
|
<li><code><a title="sql_app.api.auth.login" href="#sql_app.api.auth.login">login</a></code></li>
|
606
|
<li><code><a title="sql_app.api.auth.login_get" href="#sql_app.api.auth.login_get">login_get</a></code></li>
|
607
|
<li><code><a title="sql_app.api.auth.logout" href="#sql_app.api.auth.logout">logout</a></code></li>
|
608
|
<li><code><a title="sql_app.api.auth.refresh" href="#sql_app.api.auth.refresh">refresh</a></code></li>
|
609
|
<li><code><a title="sql_app.api.auth.signup" href="#sql_app.api.auth.signup">signup</a></code></li>
|
610
|
<li><code><a title="sql_app.api.auth.signup_get" href="#sql_app.api.auth.signup_get">signup_get</a></code></li>
|
611
|
<li><code><a title="sql_app.api.auth.verify_password" href="#sql_app.api.auth.verify_password">verify_password</a></code></li>
|
612
|
</ul>
|
613
|
</li>
|
614
|
<li><h3><a href="#header-classes">Classes</a></h3>
|
615
|
<ul>
|
616
|
<li>
|
617
|
<h4><code><a title="sql_app.api.auth.Settings" href="#sql_app.api.auth.Settings">Settings</a></code></h4>
|
618
|
<ul class="">
|
619
|
<li><code><a title="sql_app.api.auth.Settings.authjwt_cookie_csrf_protect" href="#sql_app.api.auth.Settings.authjwt_cookie_csrf_protect">authjwt_cookie_csrf_protect</a></code></li>
|
620
|
<li><code><a title="sql_app.api.auth.Settings.authjwt_secret_key" href="#sql_app.api.auth.Settings.authjwt_secret_key">authjwt_secret_key</a></code></li>
|
621
|
<li><code><a title="sql_app.api.auth.Settings.authjwt_token_location" href="#sql_app.api.auth.Settings.authjwt_token_location">authjwt_token_location</a></code></li>
|
622
|
</ul>
|
623
|
</li>
|
624
|
</ul>
|
625
|
</li>
|
626
|
</ul>
|
627
|
</nav>
|
628
|
</main>
|
629
|
<footer id="footer">
|
630
|
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
|
631
|
</footer>
|
632
|
</body>
|
633
|
</html>
|