Projekt

Obecné

Profil

Stáhnout (29.3 KB) Statistiky
| Větev: | Tag: | Revize:
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=&#34;../templates/auth&#34;)
42

    
43
pwd_context = CryptContext(schemes=[&#34;bcrypt&#34;], deprecated=&#34;auto&#34;)
44

    
45
# prefix used for all endpoints in this file
46
auth = APIRouter(prefix=&#34;&#34;)
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 = &#34;secret&#34;
60
    # Configure application to store and get JWT from cookies
61
    authjwt_token_location: set = {&#34;cookies&#34;}
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
    &#34;admin&#34;: {
74
        &#34;username&#34;: &#34;admin&#34;,
75
        &#34;password&#34;: &#34;admin&#34;
76
    }
77
}
78

    
79

    
80
def verify_password(plain_password, hashed_password):
81
    &#34;&#34;&#34;
82
    Verifies plain text password with hashed password
83
    &#34;&#34;&#34;
84
    return pwd_context.verify(plain_password, hashed_password)
85

    
86

    
87
def get_hash_password(password):
88
    &#34;&#34;&#34;
89
    Returns hashed password
90
    &#34;&#34;&#34;
91
    return pwd_context.hash(password)
92

    
93

    
94
def auth_user(db, username: str, password: str):
95
    &#34;&#34;&#34;
96
    Determines if given password belongs to user with given username
97
    &#34;&#34;&#34;
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(&#34;/signup&#34;, response_class=HTMLResponse)
107
async def signup_get(request: Request):
108
    &#34;&#34;&#34;
109
    return html template for signup
110
    &#34;&#34;&#34;
111
    return templates.TemplateResponse(&#34;signup.html&#34;, {&#34;request&#34;: request})
112

    
113

    
114
@auth.post(&#34;/signup&#34;, response_class=HTMLResponse)
115
async def signup(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)):
116
    &#34;&#34;&#34;
117
    Endpoint called form signup template. Creates new user with role guest that can be changed by admin user
118
    &#34;&#34;&#34;
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), &#34;guest&#34;)
125
        if new_user is None:
126
            print(&#34;something went wrong&#34;)
127
        return &#34;&#34;&#34;
128
            &lt;html&gt;
129
                &lt;head&gt;
130
                    &lt;title&gt;Signup&lt;/title&gt;
131
                &lt;/head&gt;
132
                &lt;body&gt;
133
                    &lt;h1&gt;New user created. You can go back to previous page.&lt;/h1&gt;
134
                    &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
135
                        &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
136
                    &lt;/form&gt;
137
                &lt;/body&gt;
138
            &lt;/html&gt;
139
            &#34;&#34;&#34;
140
    else:
141
        return &#34;&#34;&#34;
142
                    &lt;html&gt;
143
                        &lt;head&gt;
144
                            &lt;title&gt;Signup&lt;/title&gt;
145
                        &lt;/head&gt;
146
                        &lt;body&gt;
147
                            &lt;h1&gt;Username taken. Try to choose different username.&lt;/h1&gt;
148
                            &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
149
                                &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
150
                            &lt;/form&gt;
151
                        &lt;/body&gt;
152
                    &lt;/html&gt;
153
                    &#34;&#34;&#34;
154

    
155
@auth.get(&#34;/login&#34;, response_class=HTMLResponse)
156
async def login_get(request: Request):
157
    &#34;&#34;&#34;
158
    return html template for login
159
    &#34;&#34;&#34;
160
    return templates.TemplateResponse(&#34;login.html&#34;, {&#34;request&#34;: request})
161

    
162

    
163
@auth.post(&#34;/login&#34;, response_class=HTMLResponse)
164
async def login(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db),
165
                Authorize: AuthJWT = Depends()):
166
    &#34;&#34;&#34;
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
    &#34;&#34;&#34;
170
    user = auth_user(db, username, password)
171
    if user != None:
172
        if user.role == &#34;admin&#34;:
173
            access_token = Authorize.create_access_token(subject=&#34;admin&#34;, expires_time=False)
174
            refresh_token = Authorize.create_refresh_token(subject=&#34;admin&#34;, expires_time=False)
175
        else:
176
            access_token = Authorize.create_access_token(subject=&#34;guest&#34;, expires_time=False)
177
            refresh_token = Authorize.create_refresh_token(subject=&#34;guest&#34;, expires_time=False)
178
    else:
179
        usr = fake_users_db.get(username)
180
        if usr != None:
181
            if usr[&#34;username&#34;] == username and usr[&#34;password&#34;] == password:
182
                access_token = Authorize.create_access_token(subject=&#34;admin&#34;, expires_time=False)
183
                refresh_token = Authorize.create_refresh_token(subject=&#34;admin&#34;, expires_time=False)
184
        else:
185
            return &#34;&#34;&#34;
186
                &lt;html&gt;
187
                    &lt;head&gt;
188
                        &lt;title&gt;Login&lt;/title&gt;
189
                    &lt;/head&gt;
190
                    &lt;body&gt;
191
                        &lt;h1&gt;Wrong Username or Password&lt;/h1&gt;
192
                        &lt;form action=&#34;/login&#34; method=&#34;get&#34;&gt;
193
                            &lt;input type=&#34;submit&#34; value=&#34;Log again&#34; /&gt;
194
                        &lt;/form&gt;
195
                        &lt;form action=&#34;/login&#34; method=&#34;get&#34;&gt;
196
                            &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
197
                        &lt;/form&gt;
198
                    &lt;/body&gt;
199
                &lt;/html&gt;
200
                &#34;&#34;&#34;
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 &#34;&#34;&#34;
206
    &lt;html&gt;
207
        &lt;head&gt;
208
            &lt;title&gt;Login&lt;/title&gt;
209
        &lt;/head&gt;
210
        &lt;body&gt;
211
            &lt;h1&gt;Now you are logged in, you can continue to previous page.&lt;/h1&gt;
212
            &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
213
                &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
214
            &lt;/form&gt;
215
        &lt;/body&gt;
216
    &lt;/html&gt;
217
    &#34;&#34;&#34;
218

    
219

    
220
@auth.post(&#39;/refresh&#39;)
221
def refresh(Authorize: AuthJWT = Depends()):
222
    &#34;&#34;&#34;
223
    endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
224
    unlimited.
225
    &#34;&#34;&#34;
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 {&#34;msg&#34;: &#34;The token has been refresh&#34;}
232

    
233

    
234
@auth.get(&#39;/logout&#39;, response_class=HTMLResponse)
235
def logout(Authorize: AuthJWT = Depends()):
236
    &#34;&#34;&#34;
237
    Endpoint for deleting cookie token with acces role.
238
    &#34;&#34;&#34;
239
    Authorize.jwt_optional()
240

    
241
    Authorize.unset_jwt_cookies()
242
    return &#34;&#34;&#34;
243
        &lt;html&gt;
244
            &lt;head&gt;
245
                &lt;title&gt;Logout&lt;/title&gt;
246
            &lt;/head&gt;
247
            &lt;body&gt;
248
                &lt;h1&gt;Logged Out&lt;/h1&gt;
249
                &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
250
                    &lt;input type=&#34;submit&#34; value=&#34;Back&#34; /&gt;
251
                &lt;/form&gt;
252
            &lt;/body&gt;
253
        &lt;/html&gt;
254
        &#34;&#34;&#34;</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
    &#34;&#34;&#34;
275
    Determines if given password belongs to user with given username
276
    &#34;&#34;&#34;
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
    &#34;&#34;&#34;
313
    Returns hashed password
314
    &#34;&#34;&#34;
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(&#34;/login&#34;, response_class=HTMLResponse)
329
async def login(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db),
330
                Authorize: AuthJWT = Depends()):
331
    &#34;&#34;&#34;
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
    &#34;&#34;&#34;
335
    user = auth_user(db, username, password)
336
    if user != None:
337
        if user.role == &#34;admin&#34;:
338
            access_token = Authorize.create_access_token(subject=&#34;admin&#34;, expires_time=False)
339
            refresh_token = Authorize.create_refresh_token(subject=&#34;admin&#34;, expires_time=False)
340
        else:
341
            access_token = Authorize.create_access_token(subject=&#34;guest&#34;, expires_time=False)
342
            refresh_token = Authorize.create_refresh_token(subject=&#34;guest&#34;, expires_time=False)
343
    else:
344
        usr = fake_users_db.get(username)
345
        if usr != None:
346
            if usr[&#34;username&#34;] == username and usr[&#34;password&#34;] == password:
347
                access_token = Authorize.create_access_token(subject=&#34;admin&#34;, expires_time=False)
348
                refresh_token = Authorize.create_refresh_token(subject=&#34;admin&#34;, expires_time=False)
349
        else:
350
            return &#34;&#34;&#34;
351
                &lt;html&gt;
352
                    &lt;head&gt;
353
                        &lt;title&gt;Login&lt;/title&gt;
354
                    &lt;/head&gt;
355
                    &lt;body&gt;
356
                        &lt;h1&gt;Wrong Username or Password&lt;/h1&gt;
357
                        &lt;form action=&#34;/login&#34; method=&#34;get&#34;&gt;
358
                            &lt;input type=&#34;submit&#34; value=&#34;Log again&#34; /&gt;
359
                        &lt;/form&gt;
360
                        &lt;form action=&#34;/login&#34; method=&#34;get&#34;&gt;
361
                            &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
362
                        &lt;/form&gt;
363
                    &lt;/body&gt;
364
                &lt;/html&gt;
365
                &#34;&#34;&#34;
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 &#34;&#34;&#34;
371
    &lt;html&gt;
372
        &lt;head&gt;
373
            &lt;title&gt;Login&lt;/title&gt;
374
        &lt;/head&gt;
375
        &lt;body&gt;
376
            &lt;h1&gt;Now you are logged in, you can continue to previous page.&lt;/h1&gt;
377
            &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
378
                &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
379
            &lt;/form&gt;
380
        &lt;/body&gt;
381
    &lt;/html&gt;
382
    &#34;&#34;&#34;</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(&#34;/login&#34;, response_class=HTMLResponse)
395
async def login_get(request: Request):
396
    &#34;&#34;&#34;
397
    return html template for login
398
    &#34;&#34;&#34;
399
    return templates.TemplateResponse(&#34;login.html&#34;, {&#34;request&#34;: 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(&#39;/logout&#39;, response_class=HTMLResponse)
412
def logout(Authorize: AuthJWT = Depends()):
413
    &#34;&#34;&#34;
414
    Endpoint for deleting cookie token with acces role.
415
    &#34;&#34;&#34;
416
    Authorize.jwt_optional()
417

    
418
    Authorize.unset_jwt_cookies()
419
    return &#34;&#34;&#34;
420
        &lt;html&gt;
421
            &lt;head&gt;
422
                &lt;title&gt;Logout&lt;/title&gt;
423
            &lt;/head&gt;
424
            &lt;body&gt;
425
                &lt;h1&gt;Logged Out&lt;/h1&gt;
426
                &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
427
                    &lt;input type=&#34;submit&#34; value=&#34;Back&#34; /&gt;
428
                &lt;/form&gt;
429
            &lt;/body&gt;
430
        &lt;/html&gt;
431
        &#34;&#34;&#34;</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(&#39;/refresh&#39;)
445
def refresh(Authorize: AuthJWT = Depends()):
446
    &#34;&#34;&#34;
447
    endpoint for refreshing browser token. Not used at the moment since lifetime of given tokens are
448
    unlimited.
449
    &#34;&#34;&#34;
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 {&#34;msg&#34;: &#34;The token has been refresh&#34;}</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(&#34;/signup&#34;, response_class=HTMLResponse)
468
async def signup(username: str = Form(...), password: str = Form(...), db: Session = Depends(get_db)):
469
    &#34;&#34;&#34;
470
    Endpoint called form signup template. Creates new user with role guest that can be changed by admin user
471
    &#34;&#34;&#34;
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), &#34;guest&#34;)
478
        if new_user is None:
479
            print(&#34;something went wrong&#34;)
480
        return &#34;&#34;&#34;
481
            &lt;html&gt;
482
                &lt;head&gt;
483
                    &lt;title&gt;Signup&lt;/title&gt;
484
                &lt;/head&gt;
485
                &lt;body&gt;
486
                    &lt;h1&gt;New user created. You can go back to previous page.&lt;/h1&gt;
487
                    &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
488
                        &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
489
                    &lt;/form&gt;
490
                &lt;/body&gt;
491
            &lt;/html&gt;
492
            &#34;&#34;&#34;
493
    else:
494
        return &#34;&#34;&#34;
495
                    &lt;html&gt;
496
                        &lt;head&gt;
497
                            &lt;title&gt;Signup&lt;/title&gt;
498
                        &lt;/head&gt;
499
                        &lt;body&gt;
500
                            &lt;h1&gt;Username taken. Try to choose different username.&lt;/h1&gt;
501
                            &lt;form action=&#34;/logs-web&#34; method=&#34;get&#34;&gt;
502
                                &lt;input type=&#34;submit&#34; value=&#34;Home Page&#34; /&gt;
503
                            &lt;/form&gt;
504
                        &lt;/body&gt;
505
                    &lt;/html&gt;
506
                    &#34;&#34;&#34;</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(&#34;/signup&#34;, response_class=HTMLResponse)
519
async def signup_get(request: Request):
520
    &#34;&#34;&#34;
521
    return html template for signup
522
    &#34;&#34;&#34;
523
    return templates.TemplateResponse(&#34;signup.html&#34;, {&#34;request&#34;: 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
    &#34;&#34;&#34;
537
    Verifies plain text password with hashed password
538
    &#34;&#34;&#34;
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 = &#34;secret&#34;
560
    # Configure application to store and get JWT from cookies
561
    authjwt_token_location: set = {&#34;cookies&#34;}
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>
(1-1/16)