Skip to content

Commit fe3e794

Browse files
author
Joe Gasewicz
authored
Bug fixes for 0.2.0 release Fixes #225 (#226)
* Big fixes for 0.2.0 release Fixes #225 * Bug fixes for 0.2.0 release Fixes #225 * Bug fixes for 0.2.0 release Fixes #225 * Bug fixes for 0.2.0 release Fixes #225
1 parent 538a497 commit fe3e794

File tree

3 files changed

+105
-37
lines changed

3 files changed

+105
-37
lines changed

CHANGELOG.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
### Changed
44

5+
**Releases 0.2.0** - 2021-07-21
6+
- 🎁 Remove testing logic from library's `Routing` & `Entity` classes [Issue #219](https://github.com/joegasewicz/flask-jwt-router/issues/219)
7+
- 🎁 Testing mixin for Google class [Issue #220](https://github.com/joegasewicz/flask-jwt-router/issues/220)
8+
- 🪲 Bug fixes for 0.2.0 release Fixes [Issue #225](https://github.com/joegasewicz/flask-jwt-router/issues/225)
9+
510
**Releases 0.1.9** - 2021-03-16
6-
- 🎁 Allow for multiple tablenames for Google OAuth [Issue #206](https://github.com/joegasewicz/flask-jwt-router/issues/206)
11+
- 🎁 Allow for multiple tablenames for Google OAuth [Issue #206](https://github.com/joegasewicz/flask-jwt-router/issues/206)
712

813
**Releases 0.1.8** - 2021-03-16
9-
- 📝 Outdated docstring - nice easy fix for a beginner developer! [Issue #192](https://github.com/joegasewicz/flask-jwt-router/issues/192)
10-
- 🎁 Bump cryptography from 3.3.1 to 3.3.2 #197
11-
- 🎁 Multiple redirect URI's for Google OAuth #203 [Issue #203](https://github.com/joegasewicz/flask-jwt-router/issues/203)
14+
- 📝 Outdated docstring - nice easy fix for a beginner developer! [Issue #192](https://github.com/joegasewicz/flask-jwt-router/issues/192)
15+
- 🎁 Bump cryptography from 3.3.1 to 3.3.2 #197
16+
- 🎁 Multiple redirect URI's for Google OAuth #203 [Issue #203](https://github.com/joegasewicz/flask-jwt-router/issues/203)
1217

1318
**Releases 0.1.7** - 2021-01-27
1419
- 🎁 Test utils for OAuth users should handle multiple user states. [Issue #188](https://github.com/joegasewicz/flask-jwt-router/pull/188)
@@ -40,24 +45,22 @@
4045

4146
**Release 0.0.27** - 2020-09-14
4247

43-
- 🪲 Secret Key now must be set [Issue #137](https://github.com/joegasewicz/flask-jwt-router/issues/137)
44-
- 🪲 Fixes ModuleNotFoundError: No module named 'dateutil' [Issue #138](https://github.com/joegasewicz/flask-jwt-router/issues/138)
45-
- 🎁 Removes strategy design pattern from project [Issue #141](https://github.com/joegasewicz/flask-jwt-router/issues/141)
46-
- 🎁 Renamed extensions to Config [Issue #143](https://github.com/joegasewicz/flask-jwt-router/issues/143)
47-
- 🎁 Added token expire duration option [Issue #44](https://github.com/joegasewicz/flask-jwt-router/issues/44)
48+
- 🪲 Secret Key now must be set [Issue #137](https://github.com/joegasewicz/flask-jwt-router/issues/137)
49+
- 🪲 Fixes ModuleNotFoundError: No module named 'dateutil' [Issue #138](https://github.com/joegasewicz/flask-jwt-router/issues/138)
50+
- 🎁 Removes strategy design pattern from project [Issue #141](https://github.com/joegasewicz/flask-jwt-router/issues/141)
51+
- 🎁 Renamed extensions to Config [Issue #143](https://github.com/joegasewicz/flask-jwt-router/issues/143)
52+
- 🎁 Added token expire duration option [Issue #44](https://github.com/joegasewicz/flask-jwt-router/issues/44)
4853

4954
**Release 0.0.26** - 2019-12-11
5055

51-
- 🪲 Preflight OPTIONS bug fix [Issue #125](https://github.com/joegasewicz/Flask-JWT-Router/issues/125)
56+
- 🪲 Preflight OPTIONS bug fix [Issue #125](https://github.com/joegasewicz/Flask-JWT-Router/issues/125)
5257

5358
**Release 0.0.25** - 2019-12-10
5459

55-
- 🎁 Replaced the the `entity_type` kwarg to `table_name` in the public method `register_entity` [Issue #111](https://github.com/joegasewicz/Flask-JWT-Router/issues/111)
56-
- 🎁 Renamed the `update_entity` public method to be called `update_token` [Issue #114](https://github.com/joegasewicz/Flask-JWT-Router/issues/114)
57-
- 🎁 Renamed the `register_entity` public method to be called `create_token` [Issue #113](https://github.com/joegasewicz/Flask-JWT-Router/issues/113)
58-
- 🎁 Add Models to JWTRoutes class & init_app method [Issue #119](https://github.com/joegasewicz/Flask-JWT-Router/issues/119)
60+
- 🎁 Replaced the the `entity_type` kwarg to `table_name` in the public method `register_entity` [Issue #111](https://github.com/joegasewicz/Flask-JWT-Router/issues/111)
61+
- 🎁 Renamed the `update_entity` public method to be called `update_token` [Issue #114](https://github.com/joegasewicz/Flask-JWT-Router/issues/114)
62+
- 🎁 Renamed the `register_entity` public method to be called `create_token` [Issue #113](https://github.com/joegasewicz/Flask-JWT-Router/issues/113)
63+
- 🎁 Add Models to JWTRoutes class & init_app method [Issue #119](https://github.com/joegasewicz/Flask-JWT-Router/issues/119)
5964

6065
## Unreleased
61-
**Releases 0.2.0** - 2021-07-21
62-
- 🎁 Remove testing logic from library's `Routing` & `Entity` classes [Issue #219](https://github.com/joegasewicz/flask-jwt-router/issues/219)
63-
- 🎁 Testing mixin for Google class [Issue #220](https://github.com/joegasewicz/flask-jwt-router/issues/220)
66+

README.md

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,21 @@ Read the docs: [Flask-JWT-Router](https://flask-jwt-router.readthedocs.io/en/lat
1515
Flask JWT Router is a Python library that adds authorised routes to a Flask app. Both basic & Google's OAuth2.0 authentication
1616
is supported.
1717

18+
## ![Google-Cloud](assets/Google-Cloud1.png) Google's OAuth2.0 supported
1819

19-
## ![Google-Cloud](assets/Google-Cloud1.png) Google's OAuth2.0 supported
2020

21-
### Google OAuth 2.0 Quick Start
22-
23-
Read the detailed instructions here: [Flask-JWT-Router](https://flask-jwt-router.readthedocs.io/en/latest/google.html)
21+
## Quik Start
2422
```python
25-
oauth_options = {
26-
"client_id": "<CLIENT_ID>",
27-
"client_secret": "<CLIENT_SECRET>",
28-
"redirect_uri": "http://localhost:3000",
29-
"tablename": "users",
30-
"email_field": "email",
31-
"expires_in": 3600,
32-
}
23+
from flask_jwt_router import JwtRoutes
3324

34-
jwt_routes.init_app(app, google_oauth=oauth_options)
25+
jwt_routes = JwtRoutes()
26+
jwt_routes.init_app(
27+
app,
28+
entity_models=[MyModel],
29+
)
3530
```
3631

32+
3733
Now your front end needs a token. Create an endpoint &
3834
return a new access token from the clients header *code*.
3935
For Example::
@@ -262,6 +258,30 @@ If you require calling a resource without passing headers, then you can use the
262258
url = "http://example.com/cars?auth=my_token"
263259
```
264260

261+
### Google OAuth 2.0 Quick Start
262+
263+
Read the detailed instructions here: [Flask-JWT-Router](https://flask-jwt-router.readthedocs.io/en/latest/google.html)
264+
```python
265+
from flask_jwt_router import Google, JwtRoutes
266+
267+
oauth_options = {
268+
"client_id": "<CLIENT_ID>",
269+
"client_secret": "<CLIENT_SECRET>",
270+
"redirect_uri": "http://localhost:3000",
271+
"tablename": "users",
272+
"email_field": "email",
273+
"expires_in": 3600,
274+
}
275+
276+
jwt_routes = JwtRoutes()
277+
jwt_routes.init_app(
278+
app,
279+
google_oauth=oauth_options,
280+
strategies=[Google],
281+
entity_models=[MyModel],
282+
)
283+
```
284+
265285
## Google OAuth 2.0 with ReactJS
266286
Flask-JWT-Router supports auth Google's OAuth 2.0 Single Sign On strategy if you are using React only.
267287
(We will provide Google's OAuth 2.0 Single Sign On strategy for server to server as soon as possible!).
@@ -271,15 +291,17 @@ Create a login route for Google's OAuth 2.0
271291
```python
272292
@app.route("/api/v1/google_login", methods=["POST"])
273293
def google_login():
274-
data = jwt_routes.google.oauth_login(request)
294+
google = jwt_routes.get_strategy("Google")
295+
data = google.oauth_login(request)
275296
return data, 200
276297
```
277298

278299
If your app requires multiple redirect uri's then
279300
you can use the `redirect_uri` kwarg to assign a uri for the current
280301
request handler. For example:
281302
```python
282-
data = jwt_routes.google.oauth_login(request, redirect="http://another_redirect.com")
303+
google = jwt_routes.get_strategy("Google")
304+
data = google.oauth_login(request, redirect="http://another_redirect.com")
283305
```
284306

285307
We have created a ReactJS library specifically for Flask-JWT-Router - [react-google-oauth2.0](https://github.com/joegasewicz/react-google-oauth2.0)
@@ -294,6 +316,39 @@ Testing OAuth2.0 in a Flask app is non-trivial, especially if you rely on Flask-
294316
to append your user onto Flask's global context (or `g`). Therefore we have provided a
295317
utility method that returns a headers Dict that you can then use in your test view handler
296318
request. This example is using the Pytest library:
319+
```python
320+
from flask_jwt_router import (
321+
BaseJwtRoutes,
322+
JwtRoutes,
323+
Google,
324+
GoogleTestUtil,
325+
TestRoutingMixin,
326+
)
327+
328+
class TestJwtRoutes(TestRoutingMixin, BaseJwtRoutes):
329+
pass
330+
331+
332+
if not Config.E2E_TEST:
333+
jwt_routes = JwtRoutes()
334+
else:
335+
jwt_routes = TestJwtRoutes()
336+
337+
if not config.E2E_TEST:
338+
jwt_routes.init_app(
339+
app,
340+
google_oauth=oauth_options,
341+
strategies=[Google],
342+
entity_models=[MyModel],
343+
)
344+
else:
345+
jwt_routes.init_app(
346+
app,
347+
google_oauth=oauth_options,
348+
strategies=[GoogleTestUtil],
349+
entity_models=[MyModel],
350+
)
351+
```
297352

298353
```python
299354
@pytest.fixture()
@@ -302,7 +357,8 @@ request. This example is using the Pytest library:
302357

303358

304359
def test_blogs(client):
305-
user_headers = jwt_routes.google.create_test_headers(email="[email protected]")
360+
google = jwt_routes.get_strategy("GoogleTestUtil")
361+
user_headers = google.create_test_headers(email="[email protected]")
306362
rv = client.get("/blogs", headers=user_headers)
307363
```
308364

@@ -311,7 +367,8 @@ For example:
311367

312368
```python
313369
# user is an instantiated SqlAlchemy object
314-
user_headers = jwt_routes.google.create_test_headers(email="[email protected]", entity=user)
370+
google = jwt_routes.get_strategy("GoogleTestUtil")
371+
user_headers = google.create_test_headers(email="[email protected]", entity=user)
315372
# user_headers: { "X-Auth-Token": "Bearer <GOOGLE_OAUTH2_TEST>" }
316373
```
317374
If you require more than one request to a Flask view handler in a single unit test, then set
@@ -320,7 +377,8 @@ entities within a single unit test method or function then you must pass in your
320377
For example:
321378
```python
322379
my_entity = User(email="[email protected]") # If you're testing against a real db, make sure this is an entry in the db
323-
_ = jwt_routes.google.create_test_headers(email="[email protected]", scope="application", entity=my_entity)
380+
google = jwt_routes.get_strategy("GoogleTestUtil")
381+
_ = google.create_test_headers(email="[email protected]", scope="application", entity=my_entity)
324382

325383
```
326384

flask_jwt_router/_routing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,21 @@ def handle_token(self):
184184
:return None:
185185
"""
186186
strategy: Optional[BaseOAuth] = None
187+
oauth_key: str = ""
187188
try:
188189
# TODO update docs
189190
resource_headers = request.headers.get("X-Auth-Resource")
190191
oauth_headers = request.headers.get("X-Auth-Token")
192+
if oauth_headers:
193+
# TODO When we have more than one strategy then `oauth_key` should derive the key dynamically
194+
oauth_key = "X-Auth-Token"
191195
if request.args.get("auth"):
192196
token = request.args.get("auth")
193197
# Strategies --------------------------------------------------------- #
194198

195199
elif oauth_headers is not None and len(self.strategy_dict.keys()):
196200
for s in self.strategy_dict.values():
197-
if s.header_name == oauth_headers:
201+
if s.header_name == oauth_key:
198202
strategy = s
199203
if not strategy:
200204
abort(401)
@@ -261,6 +265,9 @@ def handle_token(self):
261265

262266

263267
class _TestMixin(Routing):
268+
"""
269+
TODO https://github.com/joegasewicz/flask-jwt-router/issues/224 - Router test mixin should not duplicate any code from the Router class #224
270+
"""
264271

265272
strategies: List[TestBaseOAuth]
266273

@@ -289,7 +296,7 @@ def handle_token(self):
289296
abort(401)
290297
try:
291298
if not strategy.test_metadata:
292-
raise Exception("You didn't create your test headers with create_test_headers()")
299+
strategy.create_test_headers(email=token)
293300
email, entity = strategy.update_test_metadata(token)
294301

295302
self.entity.oauth_entity_key = self.config.oauth_entity

0 commit comments

Comments
 (0)