Skip to content

Commit df7a4b4

Browse files
committed
fix: readmel, example
1 parent 9f48096 commit df7a4b4

5 files changed

Lines changed: 79 additions & 76 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ OPEN := $(shell command -v open 2> /dev/null)
3737
open:
3838
sleep 1
3939
ifdef OPEN
40-
open http://localhost:5000
40+
open http://localhost:8080
4141
endif
4242

4343
server: $(VIRTUAL_ENV)
44-
@poetry run uvicorn --reload --port 5000 example.app:app
44+
@poetry run uvicorn --reload --port 8080 example.app:app
4545

4646
.PHONY: example
4747
example:

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ AIOAuth Client -- OAuth support for Asyncio_ / Trio_ libraries.
2828
Requirements
2929
=============
3030

31-
- python >= 3.7
31+
- python >= 3.8
3232

3333
.. _installation:
3434

@@ -128,7 +128,7 @@ Run example with command: ::
128128

129129
make example
130130

131-
Open http://localhost:5000 in your browser.
131+
Open http://localhost:8080 in your browser.
132132

133133
.. _bugtracker:
134134

example/app.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111
1212
"""
1313

14-
from asgi_tools import App, ResponseRedirect
14+
1515
import html
1616
from pprint import pformat
1717

18-
from .config import CREDENTIALS
19-
from aioauth_client import ClientRegistry, OAuth1Client, GithubClient
18+
from asgi_tools import App, ResponseRedirect
19+
20+
from aioauth_client import ClientRegistry, GithubClient, OAuth1Client
2021

22+
from .config import CREDENTIALS
2123

2224
app = App(debug=True)
2325

2426

25-
@app.route('/')
26-
async def index(request):
27+
@app.route("/")
28+
async def index(_):
2729
return """
2830
<link rel="stylesheet"
2931
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
@@ -47,22 +49,24 @@ async def index(request):
4749
"""
4850

4951

50-
@app.route('/oauth/{provider}')
52+
@app.route("/oauth/{provider}")
5153
async def oauth(request):
52-
provider = request.path_params.get('provider')
54+
provider = request.path_params.get("provider")
5355
if provider not in CREDENTIALS:
54-
return 404, 'Unknown provider %s' % provider
56+
return 404, "Unknown provider %s" % provider
5557

5658
# Create OAuth1/2 client
57-
Client = ClientRegistry.clients[provider]
59+
client_cls = ClientRegistry.clients[provider]
5860
params = CREDENTIALS[provider]
59-
client = Client(**params)
60-
client.params['oauth_callback' if issubclass(Client, OAuth1Client) else 'redirect_uri'] = \
61-
str(request.url.with_query(''))
61+
client = client_cls(**params)
62+
client.params[
63+
"oauth_callback" if issubclass(client_cls, OAuth1Client) else "redirect_uri"
64+
] = str(
65+
request.url.with_query(""),
66+
)
6267

6368
# Check if is not redirect from provider
6469
if client.shared_key not in request.url.query:
65-
6670
# For oauth1 we need more work
6771
if isinstance(client, OAuth1Client):
6872
token, secret, _ = await client.get_request_token()
@@ -73,7 +77,7 @@ async def oauth(request):
7377
request.app.token = token
7478

7579
# Redirect client to provider
76-
return ResponseRedirect(client.get_authorize_url(access_type='offline'))
80+
return ResponseRedirect(client.get_authorize_url(access_type="offline"))
7781

7882
# For oauth1 we need more work
7983
if isinstance(client, OAuth1Client):
@@ -111,20 +115,17 @@ async def oauth(request):
111115
# Simple Github (OAuth2) example (not connected to app)
112116
async def github(request):
113117
github = GithubClient(
114-
client_id='b6281b6fe88fa4c313e6',
115-
client_secret='21ff23d9f1cad775daee6a38d230e1ee05b04f7c',
118+
client_id="b6281b6fe88fa4c313e6",
119+
client_secret="21ff23d9f1cad775daee6a38d230e1ee05b04f7c", # noqa:
116120
)
117-
if 'code' not in request.url.query:
118-
return ResponseRedirect(github.get_authorize_url(scope='user:email'))
121+
if "code" not in request.url.query:
122+
return ResponseRedirect(github.get_authorize_url(scope="user:email"))
119123

120124
# Get access token
121-
code = request.url.query['code']
125+
code = request.url.query["code"]
122126
token, _ = await github.get_access_token(code)
123127
assert token
124128

125129
# Get a resource `https://api.github.com/user`
126-
response = await github.request('GET', 'user')
130+
response = await github.request("GET", "user")
127131
return await response.read()
128-
129-
130-
# pylama:ignore=D

example/config.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
CREDENTIALS = {
2-
'twitter': {
3-
'consumer_key': 'oUXo1M7q1rlsPXm4ER3dWnMt8',
4-
'consumer_secret': 'YWzEvXZJO9PI6f9w2FtwUJenMvy9SPLrHOvnNkVkc5LdYjKKup',
2+
"twitter": {
3+
"consumer_key": "oUXo1M7q1rlsPXm4ER3dWnMt8",
4+
"consumer_secret": "YWzEvXZJO9PI6f9w2FtwUJenMvy9SPLrHOvnNkVkc5LdYjKKup",
55
},
6-
'github': {
7-
'client_id': 'b6281b6fe88fa4c313e6',
8-
'client_secret': '21ff23d9f1cad775daee6a38d230e1ee05b04f7c',
6+
"github": {
7+
"client_id": "b6281b6fe88fa4c313e6",
8+
"client_secret": "21ff23d9f1cad775daee6a38d230e1ee05b04f7c",
99
},
10-
'google': {
11-
'client_id': '150775235058-9fmas709maee5nn053knv1heov12sh4n.apps.googleusercontent.com', # noqa
12-
'client_secret': 'df3JwpfRf8RIBz-9avNW8Gx7',
13-
'scope': 'email profile',
10+
"google": {
11+
"client_id": "150775235058-9fmas709maee5nn053knv1heov12sh4n.apps.googleusercontent.com", # noqa:
12+
"client_secret": "df3JwpfRf8RIBz-9avNW8Gx7",
13+
"scope": "email profile",
1414
},
15-
'yandex': {
16-
'client_id': 'e19388a76a824b3385f38beec67f98f1',
17-
'client_secret': '1d2e6fdcc23b45849def6a34b43ac2d8',
15+
"yandex": {
16+
"client_id": "e19388a76a824b3385f38beec67f98f1",
17+
"client_secret": "1d2e6fdcc23b45849def6a34b43ac2d8",
1818
},
19-
'facebook': {
20-
'client_id': '384739235070641',
21-
'client_secret': '8e3374a4e1e91a2bd5b830a46208c15a',
22-
'scope': 'email'
19+
"facebook": {
20+
"client_id": "384739235070641",
21+
"client_secret": "8e3374a4e1e91a2bd5b830a46208c15a",
22+
"scope": "email",
2323
},
24-
'bitbucket': {
25-
'client_id': '4DKzbyW8JSbnkFyRS5',
26-
'client_secret': 'AvzZhtvRJhrEJMsGAMsPEuHTRWdMPX9z',
24+
"bitbucket": {
25+
"client_id": "4DKzbyW8JSbnkFyRS5",
26+
"client_secret": "AvzZhtvRJhrEJMsGAMsPEuHTRWdMPX9z",
2727
},
2828
}

example/google.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,50 @@
1414
1515
"""
1616

17-
from asgi_tools import App, ResponseRedirect
1817
from asgi_sessions import SessionMiddleware
18+
from asgi_tools import App, ResponseRedirect
19+
1920
from aioauth_client import GoogleClient
2021

2122
from .config import CREDENTIALS
2223

23-
2424
app = App()
25-
app.middleware(SessionMiddleware.setup(secret_key='aioauth-client'))
25+
app.middleware(SessionMiddleware.setup(secret_key="aioauth-client")) # noqa:
2626

2727

28-
class cfg:
29-
redirect_uri = 'http://localhost:5000/oauth/google' # define it in google api console
28+
class CFG:
29+
redirect_uri = "http://localhost:5000/oauth/google" # define it in google api console
3030

3131
# client id and secret from google api console
32-
client_id = CREDENTIALS['google']['client_id']
33-
client_secret = CREDENTIALS['google']['client_secret']
32+
client_id = CREDENTIALS["google"]["client_id"]
33+
client_secret = CREDENTIALS["google"]["client_secret"]
3434

3535
# secret_key for session encryption
3636
# key must be 32 url-safe base64-encoded bytes
37-
secret_key = b'abcdefghijklmnopqrstuvwxyz123456'
37+
secret_key = b"abcdefghijklmnopqrstuvwxyz123456"
3838

3939

40-
@app.route('/oauth/google')
40+
@app.route("/oauth/google")
4141
async def oauth(request):
4242
client = GoogleClient(
43-
client_id=cfg.client_id,
44-
client_secret=cfg.client_secret
43+
client_id=CFG.client_id,
44+
client_secret=CFG.client_secret,
4545
)
4646

47-
if 'code' not in request.url.query:
48-
return ResponseRedirect(client.get_authorize_url(
49-
scope='email profile',
50-
redirect_uri=cfg.redirect_uri
51-
))
47+
if "code" not in request.url.query:
48+
return ResponseRedirect(
49+
client.get_authorize_url(
50+
scope="email profile",
51+
redirect_uri=CFG.redirect_uri,
52+
),
53+
)
5254

5355
token, data = await client.get_access_token(
54-
request.url.query['code'],
55-
redirect_uri=cfg.redirect_uri
56+
request.url.query["code"],
57+
redirect_uri=CFG.redirect_uri,
5658
)
57-
request.session['token'] = token
58-
return ResponseRedirect('/')
59+
request.session["token"] = token
60+
return ResponseRedirect("/")
5961

6062

6163
def login_required(fn):
@@ -65,28 +67,28 @@ def login_required(fn):
6567
"""
6668

6769
async def wrapped(request, **kwargs):
68-
if 'token' not in request.session:
69-
return ResponseRedirect('/oauth/google')
70+
if "token" not in request.session:
71+
return ResponseRedirect("/oauth/google")
7072

7173
client = GoogleClient(
72-
client_id=cfg.client_id,
73-
client_secret=cfg.client_secret,
74-
access_token=request.session['token']
74+
client_id=CFG.client_id,
75+
client_secret=CFG.client_secret,
76+
access_token=request.session["token"],
7577
)
7678

7779
try:
7880
user, info = await client.user_info()
79-
except Exception:
80-
return ResponseRedirect(cfg.oauth_redirect_path)
81+
except Exception: # noqa:
82+
return ResponseRedirect(CFG.oauth_redirect_path)
8183

8284
return await fn(request, user, **kwargs)
8385

8486
return wrapped
8587

8688

87-
@app.route('/')
89+
@app.route("/")
8890
@login_required
89-
async def index(request, user):
91+
async def index(_, user):
9092
text = f"""
9193
<link rel="stylesheet"
9294
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />

0 commit comments

Comments
 (0)