forked from hanchon-live/tutorial-fastapi-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (94 loc) · 3.97 KB
/
main.py
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
import uvicorn
from fastapi import Depends
from fastapi import FastAPI
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.responses import JSONResponse
from apps.api import api_app
from apps.auth import auth_app
from apps.db import add_blacklist_token
from apps.db import init_blacklist_file
from apps.jwt import CREDENTIALS_EXCEPTION
from apps.jwt import get_current_user_token
app = FastAPI()
app.mount('/auth', auth_app)
app.mount('/api', api_app)
@app.get('/')
async def root():
return HTMLResponse('<body><a href="/auth/login">Log In</a></body>')
@app.get('/logout')
def logout(token: str = Depends(get_current_user_token)):
if add_blacklist_token(token):
return JSONResponse({'result': True})
raise CREDENTIALS_EXCEPTION
# bc128a56441dcf055d055bdda4cfbbafb35a5fcd
@app.get('/token')
async def token(request: Request):
return HTMLResponse('''
<script>
function send(){
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === 4) {
console.log(req.response);
if (req.response["result"] === true) {
window.localStorage.setItem('jwt', req.response["access_token"]);
window.localStorage.setItem('refresh', req.response["refresh_token"]);
}
}
}
req.withCredentials = true;
req.responseType = 'json';
req.open("get", "/auth/token?"+window.location.search.substr(1), true);
req.send("");
}
</script>
<button onClick="send()">Get FastAPI JWT Token</button>
<button onClick='fetch("http://127.0.0.1:7000/api/").then(
(r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Unprotected API
</button>
<button onClick='fetch("http://127.0.0.1:7000/api/protected").then(
(r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Protected API without JWT
</button>
<button onClick='fetch("http://127.0.0.1:7000/api/protected",{
headers:{
"Authorization": "Bearer " + window.localStorage.getItem("jwt")
},
}).then((r)=>r.json()).then((msg)=>{console.log(msg)});'>
Call Protected API wit JWT
</button>
<button onClick='fetch("http://127.0.0.1:7000/logout",{
headers:{
"Authorization": "Bearer " + window.localStorage.getItem("jwt")
},
}).then((r)=>r.json()).then((msg)=>{
console.log(msg);
if (msg["result"] === true) {
window.localStorage.removeItem("jwt");
}
});'>
Logout
</button>
<button onClick='fetch("http://127.0.0.1:7000/auth/refresh",{
method: "POST",
headers:{
"Authorization": "Bearer " + window.localStorage.getItem("jwt")
},
body:JSON.stringify({
grant_type:\"refresh_token\",
refresh_token:window.localStorage.getItem(\"refresh\")
})
}).then((r)=>r.json()).then((msg)=>{
console.log(msg);
if (msg["result"] === true) {
window.localStorage.setItem("jwt", msg["access_token"]);
}
});'>
Refresh
</button>
''')
if __name__ == '__main__':
init_blacklist_file()
uvicorn.run(app, port=7000)