2016-01-25 20:52:24 +00:00
|
|
|
import os
|
2016-02-12 16:02:35 +00:00
|
|
|
import random
|
|
|
|
import string
|
2015-07-10 10:22:25 +00:00
|
|
|
try:
|
|
|
|
from urlparse import parse_qs, urlsplit
|
|
|
|
except ImportError:
|
|
|
|
from urllib.parse import parse_qs, urlsplit
|
2016-01-25 20:52:24 +00:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2015-02-18 18:07:22 +00:00
|
|
|
from oidc_provider.models import *
|
2015-02-11 18:37:51 +00:00
|
|
|
|
|
|
|
|
2016-02-12 16:02:35 +00:00
|
|
|
FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d'
|
|
|
|
FAKE_RANDOM_STRING = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(32))
|
2015-07-16 18:04:33 +00:00
|
|
|
|
2015-08-11 18:59:57 +00:00
|
|
|
|
2015-02-11 18:37:51 +00:00
|
|
|
def create_fake_user():
|
2015-06-23 19:32:12 +00:00
|
|
|
"""
|
|
|
|
Create a test user.
|
2015-02-11 18:37:51 +00:00
|
|
|
|
2015-06-23 19:32:12 +00:00
|
|
|
Return a User object.
|
|
|
|
"""
|
|
|
|
user = User()
|
|
|
|
user.username = 'johndoe'
|
|
|
|
user.email = 'johndoe@example.com'
|
|
|
|
user.set_password('1234')
|
2015-02-11 18:37:51 +00:00
|
|
|
|
2015-06-23 19:32:12 +00:00
|
|
|
user.save()
|
2015-02-11 18:37:51 +00:00
|
|
|
|
2015-06-23 19:32:12 +00:00
|
|
|
return user
|
2015-02-11 18:37:51 +00:00
|
|
|
|
2015-08-11 18:59:57 +00:00
|
|
|
|
2015-02-11 18:37:51 +00:00
|
|
|
def create_fake_client(response_type):
|
2015-06-23 19:32:12 +00:00
|
|
|
"""
|
|
|
|
Create a test client, response_type argument MUST be:
|
|
|
|
'code', 'id_token' or 'id_token token'.
|
|
|
|
|
|
|
|
Return a Client object.
|
|
|
|
"""
|
|
|
|
client = Client()
|
|
|
|
client.name = 'Some Client'
|
|
|
|
client.client_id = '123'
|
|
|
|
client.client_secret = '456'
|
|
|
|
client.response_type = response_type
|
|
|
|
client.redirect_uris = ['http://example.com/']
|
|
|
|
|
|
|
|
client.save()
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
2015-08-11 18:59:57 +00:00
|
|
|
|
2016-01-25 20:52:24 +00:00
|
|
|
def create_rsakey():
|
|
|
|
"""
|
|
|
|
Generate and save a sample RSA Key.
|
|
|
|
"""
|
|
|
|
fullpath = os.path.abspath(os.path.dirname(__file__)) + '/RSAKEY.pem'
|
|
|
|
|
|
|
|
with open(fullpath, 'r') as f:
|
|
|
|
key = f.read()
|
|
|
|
RSAKey(key=key).save()
|
|
|
|
|
|
|
|
|
2015-06-23 19:32:12 +00:00
|
|
|
def is_code_valid(url, user, client):
|
|
|
|
"""
|
|
|
|
Check if the code inside the url is valid.
|
|
|
|
"""
|
|
|
|
try:
|
2015-07-10 10:22:25 +00:00
|
|
|
parsed = urlsplit(url)
|
|
|
|
params = parse_qs(parsed.query)
|
|
|
|
code = params['code'][0]
|
2015-06-23 19:32:12 +00:00
|
|
|
code = Code.objects.get(code=code)
|
|
|
|
is_code_ok = (code.client == client) and \
|
|
|
|
(code.user == user)
|
|
|
|
except:
|
|
|
|
is_code_ok = False
|
|
|
|
|
|
|
|
return is_code_ok
|
2015-08-11 18:59:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FakeUserInfo(object):
|
2016-01-12 18:17:22 +00:00
|
|
|
"""
|
|
|
|
Fake class for setting OIDC_USERINFO.
|
|
|
|
"""
|
2015-08-11 18:59:57 +00:00
|
|
|
|
|
|
|
given_name = 'John'
|
|
|
|
family_name = 'Doe'
|
|
|
|
nickname = 'johndoe'
|
|
|
|
website = 'http://johndoe.com'
|
|
|
|
|
|
|
|
phone_number = '+49-89-636-48018'
|
|
|
|
phone_number_verified = True
|
|
|
|
|
|
|
|
address_street_address = 'Evergreen 742'
|
|
|
|
address_locality = 'Glendive'
|
|
|
|
address_region = 'Montana'
|
|
|
|
address_country = 'United States'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_by_user(cls, user):
|
|
|
|
return cls()
|
2016-01-12 18:17:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fake_sub_generator(user):
|
|
|
|
"""
|
|
|
|
Fake function for setting OIDC_IDTOKEN_SUB_GENERATOR.
|
|
|
|
"""
|
|
|
|
return user.email
|
2016-02-12 16:02:35 +00:00
|
|
|
|
|
|
|
|
2016-02-17 22:28:08 +00:00
|
|
|
def fake_idtoken_processing_hook(id_token, user):
|
2016-02-12 16:02:35 +00:00
|
|
|
"""
|
2016-02-12 17:51:43 +00:00
|
|
|
Fake function for inserting some keys into token. Testing OIDC_IDTOKEN_PROCESSING_HOOK.
|
2016-02-12 16:02:35 +00:00
|
|
|
"""
|
2016-02-12 17:51:43 +00:00
|
|
|
id_token['test_idtoken_processing_hook'] = FAKE_RANDOM_STRING
|
2016-02-17 22:28:08 +00:00
|
|
|
id_token['test_idtoken_processing_hook_user_email'] = user.email
|
2016-02-12 17:51:43 +00:00
|
|
|
return id_token
|