-
-
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.
Add custom claims and extra verifications
- Loading branch information
Showing
38 changed files
with
742 additions
and
200 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from sanic import Sanic | ||
from sanic.response import json | ||
from sanic_jwt import exceptions | ||
from sanic_jwt import Initialize | ||
from sanic_jwt import protected | ||
from sanic_jwt import Claim | ||
|
||
|
||
class User: | ||
|
||
def __init__(self, id, username, password): | ||
self.user_id = id | ||
self.username = username | ||
self.password = password | ||
|
||
def __repr__(self): | ||
return "User(id='{}')".format(self.user_id) | ||
|
||
def to_dict(self): | ||
return {"user_id": self.user_id, "username": self.username} | ||
|
||
|
||
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 | ||
|
||
|
||
class User2Claim(Claim): | ||
key = "user_id" | ||
|
||
def setup(self, payload, user): | ||
payload[self.key] = user.get("user_id") | ||
return payload | ||
|
||
def verify(self, value): | ||
return value == 2 | ||
|
||
|
||
custom_claims = [User2Claim] | ||
app = Sanic() | ||
sanicjwt = Initialize( | ||
app, authenticate=authenticate, custom_claims=custom_claims, debug=True | ||
) | ||
|
||
|
||
@app.route("/protected") | ||
@protected() | ||
async def protected(request): | ||
print(request.app.auth._custom_claims) | ||
return json({"protected": True}) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host="127.0.0.1", port=8888, auto_reload=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from sanic import Sanic | ||
from sanic.response import json | ||
from sanic_jwt import exceptions | ||
from sanic_jwt import Initialize | ||
from sanic_jwt import protected | ||
|
||
|
||
class User: | ||
|
||
def __init__(self, id, username, password): | ||
self.user_id = id | ||
self.username = username | ||
self.password = password | ||
|
||
def __repr__(self): | ||
return "User(id='{}')".format(self.user_id) | ||
|
||
def to_dict(self): | ||
return {"user_id": self.user_id, "username": self.username} | ||
|
||
|
||
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 | ||
|
||
|
||
def user2(payload): | ||
return payload.get("user_id") == 2 | ||
|
||
|
||
extra_verifications = [user2] | ||
|
||
app = Sanic() | ||
Initialize( | ||
app, authenticate=authenticate, extra_verifications=extra_verifications | ||
) | ||
|
||
|
||
@app.route("/protected") | ||
@protected() | ||
async def protected(request): | ||
return json({"protected": True}) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host="127.0.0.1", port=8888, auto_reload=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
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,25 @@ | ||
from sanic_jwt import exceptions | ||
|
||
|
||
class Claim: | ||
@classmethod | ||
def _register(cls, sanicjwt): | ||
required = ('key', 'setup', 'verify') | ||
instance = cls() | ||
if any(not hasattr(instance, x) for x in required): | ||
raise AttributeError | ||
sanicjwt.instance.auth._custom_claims.add(instance) | ||
|
||
def get_key(self): | ||
return self.key | ||
|
||
def _verify(self, payload): | ||
key = self.get_key() | ||
value = payload.get(key) | ||
valid_claim = self.verify(value) | ||
if not isinstance(valid_claim, bool): | ||
raise exceptions.InvalidCustomClaim() | ||
|
||
if valid_claim is False: | ||
message = "Invalid claim: {}".format(key) | ||
raise exceptions.InvalidCustomClaimError(message=message) |
Oops, something went wrong.