-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
335 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +0,0 @@ | ||
from sanic import Sanic | ||
from sanic.response import json | ||
from sanic_jwt import exceptions | ||
from sanic_jwt import initialize | ||
from sanic_jwt.decorators import protected | ||
|
||
|
||
class User(object): | ||
def __init__(self, id, username, password): | ||
self.user_id = id | ||
self.username = username | ||
self.password = password | ||
|
||
def __str__(self): | ||
return "User(id='%s')" % self.id | ||
|
||
|
||
users = [ | ||
User(1, 'user1', 'abcxyz'), | ||
User(2, 'user2', 'abcxyz'), | ||
] | ||
|
||
username_table = {u.username: u for u in users} | ||
# userid_table = {u.user_id: u for u in users} | ||
|
||
|
||
async def authenticate(request, *args, **kwargs): | ||
username = request.json.get('username', None) | ||
password = request.json.get('password', None) | ||
|
||
if not username or not password: | ||
raise exceptions.AuthenticationFailed("Missing username or password.") | ||
|
||
user = username_table.get(username, None) | ||
if user is None: | ||
raise exceptions.AuthenticationFailed("User not found.") | ||
|
||
if password != user.password: | ||
raise exceptions.AuthenticationFailed("Password is incorrect.") | ||
|
||
return user | ||
|
||
app = Sanic() | ||
initialize( | ||
app, | ||
authenticate=authenticate, | ||
) | ||
|
||
|
||
@app.route("/") | ||
async def test(request): | ||
return json({"hello": "world"}) | ||
|
||
|
||
@app.route("/protected") | ||
@protected() | ||
async def protected(request): | ||
return json({"protected": True}) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from sanic import Sanic, response | ||
from sanic.views import HTTPMethodView | ||
from sanic.response import json | ||
from sanic_jwt import initialize | ||
|
||
|
||
class MagicLoginHandler(HTTPMethodView): | ||
async def options(self, request): | ||
return response.text('', status=204) | ||
|
||
async def post(self, request): | ||
# create a magic login token and email it to the user | ||
|
||
response = { | ||
'magic-token': '123456789' | ||
} | ||
return json(response) | ||
|
||
|
||
app = Sanic() | ||
initialize( | ||
app, | ||
authenticate=lambda: True, | ||
class_views=[ | ||
('/magic-login', MagicLoginHandler) # The path will be relative to the url prefix (which defaults to /auth) | ||
] | ||
) | ||
|
||
|
||
class TestEndpointsExtra(object): | ||
def dispatch(self, path, method): | ||
# header_token = '{} {}'.format(app.config.SANIC_JWT_AUTHORIZATION_HEADER_PREFIX, access_token) | ||
method = getattr(app.test_client, method) | ||
request, response = method(path) | ||
return request, response | ||
|
||
def get(self, path): | ||
return self.dispatch(path, 'get') | ||
|
||
def post(self, path): | ||
return self.dispatch(path, 'post') | ||
|
||
def options(self, path): | ||
return self.dispatch(path, 'options') | ||
|
||
def test_verify_token(self): | ||
_, response = self.options('/auth/magic-login') | ||
assert response.status == 204 | ||
|
||
_, response = self.post('/auth/magic-login') | ||
|
||
assert response.status == 200 | ||
assert response.json.get('magic-token') == '123456789' |
Oops, something went wrong.