-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix create_id_token with extra scope claims + add ruff as formatter.
- Loading branch information
1 parent
98b9810
commit b744992
Showing
8 changed files
with
418 additions
and
357 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
{ | ||
"[python]": { | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.sortImports": "explicit" | ||
} | ||
}, | ||
"python.formatting.provider": "black", | ||
"editor.formatOnSave": true, | ||
"black-formatter.args": [ | ||
"--line-length=100", | ||
"--preview", | ||
], | ||
"isort.args": [ | ||
"--profile", | ||
"black" | ||
], | ||
"source.fixAll": "explicit", | ||
"source.organizeImports": "explicit" | ||
}, | ||
"editor.defaultFormatter": "charliermarsh.ruff" | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -5,25 +5,27 @@ | |
from django.contrib.auth.backends import ModelBackend | ||
|
||
try: | ||
from urlparse import parse_qs, urlsplit | ||
from urlparse import parse_qs | ||
from urlparse import urlsplit | ||
except ImportError: | ||
from urllib.parse import parse_qs, urlsplit | ||
from urllib.parse import parse_qs | ||
from urllib.parse import urlsplit | ||
|
||
from django.utils import timezone | ||
from django.contrib.auth.models import User | ||
from django.utils import timezone | ||
|
||
from oidc_provider.models import ( | ||
Client, | ||
Code, | ||
Token, | ||
ResponseType) | ||
|
||
from oidc_provider.lib.claims import ScopeClaims | ||
from oidc_provider.models import Client | ||
from oidc_provider.models import Code | ||
from oidc_provider.models import ResponseType | ||
from oidc_provider.models import Token | ||
|
||
FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d' | ||
FAKE_RANDOM_STRING = ''.join( | ||
random.choice(string.ascii_uppercase + string.digits) for _ in range(32)) | ||
FAKE_CODE_CHALLENGE = 'YlYXEqXuRm-Xgi2BOUiK50JW1KsGTX6F1TDnZSC8VTg' | ||
FAKE_CODE_VERIFIER = 'SmxGa0XueyNh5bDgTcSrqzAh2_FmXEqU8kDT6CuXicw' | ||
FAKE_NONCE = "cb584e44c43ed6bd0bc2d9c7e242837d" | ||
FAKE_RANDOM_STRING = "".join( | ||
random.choice(string.ascii_uppercase + string.digits) for _ in range(32) | ||
) | ||
FAKE_CODE_CHALLENGE = "YlYXEqXuRm-Xgi2BOUiK50JW1KsGTX6F1TDnZSC8VTg" | ||
FAKE_CODE_VERIFIER = "SmxGa0XueyNh5bDgTcSrqzAh2_FmXEqU8kDT6CuXicw" | ||
|
||
|
||
def create_fake_user(): | ||
|
@@ -33,11 +35,11 @@ def create_fake_user(): | |
Return a User object. | ||
""" | ||
user = User() | ||
user.username = 'johndoe' | ||
user.email = '[email protected]' | ||
user.first_name = 'John' | ||
user.last_name = 'Doe' | ||
user.set_password('1234') | ||
user.username = "johndoe" | ||
user.email = "[email protected]" | ||
user.first_name = "John" | ||
user.last_name = "Doe" | ||
user.set_password("1234") | ||
|
||
user.save() | ||
|
||
|
@@ -52,20 +54,20 @@ def create_fake_client(response_type, is_public=False, require_consent=True): | |
Return a Client object. | ||
""" | ||
client = Client() | ||
client.name = 'Some Client' | ||
client.name = "Some Client" | ||
client.client_id = str(random.randint(1, 999999)).zfill(6) | ||
if is_public: | ||
client.client_type = 'public' | ||
client.client_secret = '' | ||
client.client_type = "public" | ||
client.client_secret = "" | ||
else: | ||
client.client_secret = str(random.randint(1, 999999)).zfill(6) | ||
client.redirect_uris = ['http://example.com/'] | ||
client.redirect_uris = ["http://example.com/"] | ||
client.require_consent = require_consent | ||
client.scope = ['openid', 'email'] | ||
client.scope = ["openid", "email"] | ||
client.save() | ||
|
||
# check if response_type is a string in a python 2 and 3 compatible way | ||
if isinstance(response_type, ("".__class__, u"".__class__)): | ||
if isinstance(response_type, ("".__class__, "".__class__)): | ||
response_type = (response_type,) | ||
for value in response_type: | ||
client.response_types.add(ResponseType.objects.get(value=value)) | ||
|
@@ -90,7 +92,7 @@ def is_code_valid(url, user, client): | |
try: | ||
parsed = urlsplit(url) | ||
params = parse_qs(parsed.query or parsed.fragment) | ||
code = params['code'][0] | ||
code = params["code"][0] | ||
code = Code.objects.get(code=code) | ||
is_code_ok = (code.client == client) and (code.user == user) | ||
except Exception: | ||
|
@@ -103,15 +105,28 @@ def userinfo(claims, user): | |
""" | ||
Fake function for setting OIDC_USERINFO. | ||
""" | ||
claims['given_name'] = 'John' | ||
claims['family_name'] = 'Doe' | ||
claims['name'] = '{0} {1}'.format(claims['given_name'], claims['family_name']) | ||
claims['email'] = user.email | ||
claims['email_verified'] = True | ||
claims['address']['country'] = 'Argentina' | ||
claims["given_name"] = "John" | ||
claims["family_name"] = "Doe" | ||
claims["name"] = "{0} {1}".format(claims["given_name"], claims["family_name"]) | ||
claims["email"] = user.email | ||
claims["email_verified"] = True | ||
claims["address"]["country"] = "Argentina" | ||
return claims | ||
|
||
|
||
class FakeScopeClaims(ScopeClaims): | ||
info_pizza = ( | ||
"Pizza", | ||
"Some description for the scope.", | ||
) | ||
|
||
def scope_pizza(self): | ||
dic = { | ||
"pizza": "Margherita", | ||
} | ||
return dic | ||
|
||
|
||
def fake_sub_generator(user): | ||
""" | ||
Fake function for setting OIDC_IDTOKEN_SUB_GENERATOR. | ||
|
@@ -123,8 +138,8 @@ def fake_idtoken_processing_hook(id_token, user, **kwargs): | |
""" | ||
Fake function for inserting some keys into token. Testing OIDC_IDTOKEN_PROCESSING_HOOK. | ||
""" | ||
id_token['test_idtoken_processing_hook'] = FAKE_RANDOM_STRING | ||
id_token['test_idtoken_processing_hook_user_email'] = user.email | ||
id_token["test_idtoken_processing_hook"] = FAKE_RANDOM_STRING | ||
id_token["test_idtoken_processing_hook_user_email"] = user.email | ||
return id_token | ||
|
||
|
||
|
@@ -133,32 +148,31 @@ def fake_idtoken_processing_hook2(id_token, user, **kwargs): | |
Fake function for inserting some keys into token. | ||
Testing OIDC_IDTOKEN_PROCESSING_HOOK - tuple or list as param | ||
""" | ||
id_token['test_idtoken_processing_hook2'] = FAKE_RANDOM_STRING | ||
id_token['test_idtoken_processing_hook_user_email2'] = user.email | ||
id_token["test_idtoken_processing_hook2"] = FAKE_RANDOM_STRING | ||
id_token["test_idtoken_processing_hook_user_email2"] = user.email | ||
return id_token | ||
|
||
|
||
def fake_idtoken_processing_hook3(id_token, user, token, **kwargs): | ||
""" | ||
Fake function for checking scope is passed to processing hook. | ||
""" | ||
id_token['scope_of_token_passed_to_processing_hook'] = token.scope | ||
id_token["scope_of_token_passed_to_processing_hook"] = token.scope | ||
return id_token | ||
|
||
|
||
def fake_idtoken_processing_hook4(id_token, user, **kwargs): | ||
""" | ||
Fake function for checking kwargs passed to processing hook. | ||
""" | ||
id_token['kwargs_passed_to_processing_hook'] = { | ||
key: repr(value) | ||
for (key, value) in kwargs.items() | ||
id_token["kwargs_passed_to_processing_hook"] = { | ||
key: repr(value) for (key, value) in kwargs.items() | ||
} | ||
return id_token | ||
|
||
|
||
def fake_introspection_processing_hook(response_dict, client, id_token): | ||
response_dict['test_introspection_processing_hook'] = FAKE_RANDOM_STRING | ||
response_dict["test_introspection_processing_hook"] = FAKE_RANDOM_STRING | ||
return response_dict | ||
|
||
|
||
|
Oops, something went wrong.