Repository navigation
Expand file tree
/
Copy pathgoogle.py
More file actions
107 lines (81 loc) · 2.92 KB
/
Copy pathgoogle.py
File metadata and controls
107 lines (81 loc) · 2.92 KB
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
"""
enable Google+ API by visiting
https://console.developers.google.com/apis/api/plus.googleapis.com/overview
Requirements:
aioauth-client
asgi-tools
asgi-sessions
uvicorn
Run the example with uvicorn:
$ uvicorn --port 5000 example.google_example:app
"""
from asgi_sessions import SessionMiddleware
from asgi_tools import App, ResponseRedirect
from aioauth_client import GoogleClient
from .config import CREDENTIALS
app = App()
app.middleware(SessionMiddleware.setup(secret_key="aioauth-client")) # noqa:
class CFG:
redirect_uri = "http://localhost:5000/oauth/google" # define it in google api console
# client id and secret from google api console
client_id = CREDENTIALS["google"]["client_id"]
client_secret = CREDENTIALS["google"]["client_secret"]
# secret_key for session encryption
# key must be 32 url-safe base64-encoded bytes
secret_key = b"abcdefghijklmnopqrstuvwxyz123456"
@app.route("/oauth/google")
async def oauth(request):
client = GoogleClient(
client_id=CFG.client_id,
client_secret=CFG.client_secret,
)
if "code" not in request.url.query:
return ResponseRedirect(
client.get_authorize_url(
scope="email profile",
redirect_uri=CFG.redirect_uri,
),
)
token, data = await client.get_access_token(
request.url.query["code"],
redirect_uri=CFG.redirect_uri,
)
request.session["token"] = token
return ResponseRedirect("/")
def login_required(fn):
"""auth decorator
call function(request, user: <aioauth_client User object>)
"""
async def wrapped(request, **kwargs):
if "token" not in request.session:
return ResponseRedirect("/oauth/google")
client = GoogleClient(
client_id=CFG.client_id,
client_secret=CFG.client_secret,
access_token=request.session["token"],
)
try:
user, info = await client.user_info()
except Exception: # noqa:
return ResponseRedirect(CFG.redirect_uri)
return await fn(request, user, **kwargs)
return wrapped
@app.route("/")
@login_required
async def index(_, user):
text = f"""
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<div class="container">
<header class="navbar navbar-dark" style="background-color: #7952b3">
<h2 class="navbar-brand">AIOAuth Google Client Example</h2>
</header>
<table class="table mt-4">
<tr><td> ID </td><td> {user.id} </td></tr>
<tr><td>First, last name</td><td>{user.first_name}, {user.last_name}</td></tr>
<tr><td>Email</td><td> {user.email} </td></tr>
<tr><td>Picture</td><td> {user.picture} </td></tr>
</table>
<div>
"""
return text