2020-07-08 23:41:14 +01:00
|
|
|
"""test OAuth Source"""
|
|
|
|
from os.path import abspath
|
2020-09-11 22:21:11 +01:00
|
|
|
from sys import platform
|
2020-09-13 14:39:25 +01:00
|
|
|
from time import sleep
|
2020-09-11 22:21:11 +01:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from unittest.case import skipUnless
|
2020-07-08 23:41:14 +01:00
|
|
|
|
2020-09-11 22:54:20 +01:00
|
|
|
from django.test import override_settings
|
2020-09-09 23:14:59 +01:00
|
|
|
from docker.models.containers import Container
|
|
|
|
from docker.types import Healthcheck
|
2020-07-08 23:41:14 +01:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
from selenium.webdriver.support import expected_conditions as ec
|
2020-08-14 17:09:49 +01:00
|
|
|
from structlog import get_logger
|
2020-07-09 23:14:48 +01:00
|
|
|
from yaml import safe_dump
|
2020-07-08 23:41:14 +01:00
|
|
|
|
|
|
|
from e2e.utils import SeleniumTestCase
|
|
|
|
from passbook.flows.models import Flow
|
2020-09-26 17:17:53 +01:00
|
|
|
from passbook.providers.oauth2.generators import (
|
|
|
|
generate_client_id,
|
|
|
|
generate_client_secret,
|
|
|
|
)
|
2020-07-08 23:41:14 +01:00
|
|
|
from passbook.sources.oauth.models import OAuthSource
|
|
|
|
|
2020-07-09 22:37:13 +01:00
|
|
|
CONFIG_PATH = "/tmp/dex.yml"
|
2020-08-14 17:09:49 +01:00
|
|
|
LOGGER = get_logger()
|
2020-07-08 23:41:14 +01:00
|
|
|
|
|
|
|
|
2020-09-11 22:21:11 +01:00
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2020-09-26 16:44:05 +01:00
|
|
|
class TestSourceOAuth2(SeleniumTestCase):
|
2020-07-08 23:41:14 +01:00
|
|
|
"""test OAuth Source flow"""
|
|
|
|
|
|
|
|
container: Container
|
|
|
|
|
|
|
|
def setUp(self):
|
2020-07-09 13:59:25 +01:00
|
|
|
self.client_secret = generate_client_secret()
|
2020-09-11 22:21:11 +01:00
|
|
|
self.prepare_dex_config()
|
2020-07-10 15:49:25 +01:00
|
|
|
super().setUp()
|
2020-07-08 23:41:14 +01:00
|
|
|
|
|
|
|
def prepare_dex_config(self):
|
|
|
|
"""Since Dex does not document which environment
|
|
|
|
variables can be used to configure clients"""
|
2020-07-09 22:13:14 +01:00
|
|
|
config = {
|
|
|
|
"enablePasswordDB": True,
|
|
|
|
"issuer": "http://127.0.0.1:5556/dex",
|
|
|
|
"logger": {"level": "debug"},
|
|
|
|
"staticClients": [
|
|
|
|
{
|
|
|
|
"id": "example-app",
|
|
|
|
"name": "Example App",
|
|
|
|
"redirectURIs": [
|
|
|
|
self.url(
|
|
|
|
"passbook_sources_oauth:oauth-client-callback",
|
|
|
|
source_slug="dex",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
"secret": self.client_secret,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"staticPasswords": [
|
|
|
|
{
|
|
|
|
"email": "admin@example.com",
|
|
|
|
# hash for password
|
|
|
|
"hash": "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W",
|
|
|
|
"userID": "08a8684b-db88-4b73-90a9-3cd1661f5466",
|
|
|
|
"username": "admin",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"storage": {"config": {"file": "/tmp/dex.db"}, "type": "sqlite3"},
|
|
|
|
"web": {"http": "0.0.0.0:5556"},
|
|
|
|
}
|
2020-07-09 22:37:13 +01:00
|
|
|
with open(CONFIG_PATH, "w+") as _file:
|
2020-07-08 23:41:14 +01:00
|
|
|
safe_dump(config, _file)
|
|
|
|
|
2020-09-11 22:21:11 +01:00
|
|
|
def get_container_specs(self) -> Optional[Dict[str, Any]]:
|
|
|
|
return {
|
|
|
|
"image": "quay.io/dexidp/dex:v2.24.0",
|
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
|
|
|
"command": "serve /config.yml",
|
|
|
|
"healthcheck": Healthcheck(
|
2020-07-08 23:41:14 +01:00
|
|
|
test=["CMD", "wget", "--spider", "http://localhost:5556/dex/healthz"],
|
|
|
|
interval=5 * 100 * 1000000,
|
|
|
|
start_period=1 * 100 * 1000000,
|
|
|
|
),
|
2020-09-11 22:21:11 +01:00
|
|
|
"volumes": {abspath(CONFIG_PATH): {"bind": "/config.yml", "mode": "ro"}},
|
|
|
|
}
|
2020-07-08 23:41:14 +01:00
|
|
|
|
2020-07-09 22:13:14 +01:00
|
|
|
def create_objects(self):
|
|
|
|
"""Create required objects"""
|
2020-07-08 23:41:14 +01:00
|
|
|
# Bootstrap all needed objects
|
|
|
|
authentication_flow = Flow.objects.get(slug="default-source-authentication")
|
|
|
|
enrollment_flow = Flow.objects.get(slug="default-source-enrollment")
|
|
|
|
|
2020-09-26 18:08:37 +01:00
|
|
|
OAuthSource.objects.create( # nosec
|
2020-07-08 23:41:14 +01:00
|
|
|
name="dex",
|
|
|
|
slug="dex",
|
|
|
|
authentication_flow=authentication_flow,
|
|
|
|
enrollment_flow=enrollment_flow,
|
|
|
|
provider_type="openid-connect",
|
|
|
|
authorization_url="http://127.0.0.1:5556/dex/auth",
|
2020-09-26 18:08:37 +01:00
|
|
|
access_token_url="http://127.0.0.1:5556/dex/token",
|
2020-07-08 23:41:14 +01:00
|
|
|
profile_url="http://127.0.0.1:5556/dex/userinfo",
|
|
|
|
consumer_key="example-app",
|
2020-07-09 13:59:25 +01:00
|
|
|
consumer_secret=self.client_secret,
|
2020-07-08 23:41:14 +01:00
|
|
|
)
|
|
|
|
|
2020-07-09 22:13:14 +01:00
|
|
|
def test_oauth_enroll(self):
|
|
|
|
"""test OAuth Source With With OIDC"""
|
|
|
|
self.create_objects()
|
2020-07-08 23:41:14 +01:00
|
|
|
self.driver.get(self.live_server_url)
|
|
|
|
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located(
|
|
|
|
(By.CLASS_NAME, "pf-c-login__main-footer-links-item-link")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.driver.find_element(
|
|
|
|
By.CLASS_NAME, "pf-c-login__main-footer-links-item-link"
|
|
|
|
).click()
|
|
|
|
|
|
|
|
# Now we should be at the IDP, wait for the login field
|
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "login")))
|
|
|
|
self.driver.find_element(By.ID, "login").send_keys("admin@example.com")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys("password")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
# Wait until we're logged in
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]"))
|
|
|
|
)
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
2020-09-11 22:21:11 +01:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.NAME, "username")))
|
2020-07-08 23:41:14 +01:00
|
|
|
# At this point we've been redirected back
|
|
|
|
# and we're asked for the username
|
|
|
|
self.driver.find_element(By.NAME, "username").click()
|
|
|
|
self.driver.find_element(By.NAME, "username").send_keys("foo")
|
|
|
|
self.driver.find_element(By.NAME, "username").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
# Wait until we've loaded the user info page
|
2020-09-28 23:27:58 +01:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "user-settings")))
|
|
|
|
self.driver.get(self.url("passbook_core:user-settings"))
|
2020-07-08 23:41:14 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-09-30 18:34:22 +01:00
|
|
|
self.driver.find_element(By.ID, "user-settings").text,
|
|
|
|
"foo",
|
2020-07-08 23:41:14 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.ID, "id_username").get_attribute("value"), "foo"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2020-09-30 18:34:22 +01:00
|
|
|
self.driver.find_element(By.ID, "id_name").get_attribute("value"),
|
|
|
|
"admin",
|
2020-07-08 23:41:14 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.ID, "id_email").get_attribute("value"),
|
|
|
|
"admin@example.com",
|
|
|
|
)
|
2020-07-09 23:14:48 +01:00
|
|
|
|
2020-09-11 22:54:20 +01:00
|
|
|
@override_settings(SESSION_COOKIE_SAMESITE="strict")
|
|
|
|
def test_oauth_samesite_strict(self):
|
|
|
|
"""test OAuth Source With SameSite set to strict
|
|
|
|
(=will fail because session is not carried over)"""
|
|
|
|
self.create_objects()
|
|
|
|
self.driver.get(self.live_server_url)
|
|
|
|
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located(
|
|
|
|
(By.CLASS_NAME, "pf-c-login__main-footer-links-item-link")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.driver.find_element(
|
|
|
|
By.CLASS_NAME, "pf-c-login__main-footer-links-item-link"
|
|
|
|
).click()
|
|
|
|
|
|
|
|
# Now we should be at the IDP, wait for the login field
|
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "login")))
|
|
|
|
self.driver.find_element(By.ID, "login").send_keys("admin@example.com")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys("password")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
# Wait until we're logged in
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]"))
|
|
|
|
)
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
2020-09-13 19:47:17 +01:00
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located((By.CSS_SELECTOR, ".pf-c-alert__title"))
|
|
|
|
)
|
2020-09-11 22:54:20 +01:00
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, ".pf-c-alert__title").text,
|
|
|
|
"Authentication Failed.",
|
|
|
|
)
|
|
|
|
|
2020-07-09 23:14:48 +01:00
|
|
|
def test_oauth_enroll_auth(self):
|
|
|
|
"""test OAuth Source With With OIDC (enroll and authenticate again)"""
|
|
|
|
self.test_oauth_enroll()
|
|
|
|
# We're logged in at the end of this, log out and re-login
|
2020-09-28 17:17:07 +01:00
|
|
|
self.driver.find_element(By.ID, "logout").click()
|
2020-07-09 23:14:48 +01:00
|
|
|
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located(
|
|
|
|
(By.CLASS_NAME, "pf-c-login__main-footer-links-item-link")
|
|
|
|
)
|
|
|
|
)
|
2020-09-13 14:39:25 +01:00
|
|
|
sleep(1)
|
2020-07-09 23:14:48 +01:00
|
|
|
self.driver.find_element(
|
|
|
|
By.CLASS_NAME, "pf-c-login__main-footer-links-item-link"
|
|
|
|
).click()
|
2020-09-13 14:39:25 +01:00
|
|
|
sleep(1)
|
2020-07-09 23:14:48 +01:00
|
|
|
# Now we should be at the IDP, wait for the login field
|
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "login")))
|
|
|
|
self.driver.find_element(By.ID, "login").send_keys("admin@example.com")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys("password")
|
|
|
|
self.driver.find_element(By.ID, "password").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
# Wait until we're logged in
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]"))
|
|
|
|
)
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
2020-09-28 23:27:58 +01:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "user-settings")))
|
|
|
|
self.driver.get(self.url("passbook_core:user-settings"))
|
2020-07-09 23:14:48 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-09-30 18:34:22 +01:00
|
|
|
self.driver.find_element(By.ID, "user-settings").text,
|
|
|
|
"foo",
|
2020-07-09 23:14:48 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.ID, "id_username").get_attribute("value"), "foo"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2020-09-30 18:34:22 +01:00
|
|
|
self.driver.find_element(By.ID, "id_name").get_attribute("value"),
|
|
|
|
"admin",
|
2020-07-09 23:14:48 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.ID, "id_email").get_attribute("value"),
|
|
|
|
"admin@example.com",
|
|
|
|
)
|
2020-09-26 16:44:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
|
|
|
class TestSourceOAuth1(SeleniumTestCase):
|
2020-09-26 18:08:37 +01:00
|
|
|
"""Test OAuth1 Source"""
|
|
|
|
|
2020-09-26 16:44:05 +01:00
|
|
|
def setUp(self) -> None:
|
|
|
|
self.client_id = generate_client_id()
|
|
|
|
self.client_secret = generate_client_secret()
|
|
|
|
self.source_slug = "oauth1-test"
|
|
|
|
super().setUp()
|
|
|
|
|
|
|
|
def get_container_specs(self) -> Optional[Dict[str, Any]]:
|
|
|
|
return {
|
|
|
|
"image": "beryju/oauth1-test-server",
|
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
|
|
|
"environment": {
|
|
|
|
"OAUTH1_CLIENT_ID": self.client_id,
|
|
|
|
"OAUTH1_CLIENT_SECRET": self.client_secret,
|
|
|
|
"OAUTH1_REDIRECT_URI": (
|
|
|
|
self.url(
|
|
|
|
"passbook_sources_oauth:oauth-client-callback",
|
|
|
|
source_slug=self.source_slug,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def create_objects(self):
|
|
|
|
"""Create required objects"""
|
|
|
|
# Bootstrap all needed objects
|
|
|
|
authentication_flow = Flow.objects.get(slug="default-source-authentication")
|
|
|
|
enrollment_flow = Flow.objects.get(slug="default-source-enrollment")
|
|
|
|
|
2020-09-26 18:08:37 +01:00
|
|
|
OAuthSource.objects.create( # nosec
|
2020-09-26 16:44:05 +01:00
|
|
|
name="oauth1",
|
|
|
|
slug=self.source_slug,
|
|
|
|
authentication_flow=authentication_flow,
|
|
|
|
enrollment_flow=enrollment_flow,
|
|
|
|
provider_type="twitter",
|
|
|
|
request_token_url="http://localhost:5000/oauth/request_token",
|
|
|
|
access_token_url="http://localhost:5000/oauth/access_token",
|
|
|
|
authorization_url="http://localhost:5000/oauth/authorize",
|
|
|
|
profile_url="http://localhost:5000/api/me",
|
|
|
|
consumer_key=self.client_id,
|
|
|
|
consumer_secret=self.client_secret,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_oauth_enroll(self):
|
|
|
|
"""test OAuth Source With With OIDC"""
|
|
|
|
self.create_objects()
|
|
|
|
self.driver.get(self.live_server_url)
|
|
|
|
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located(
|
|
|
|
(By.CLASS_NAME, "pf-c-login__main-footer-links-item-link")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.driver.find_element(
|
|
|
|
By.CLASS_NAME, "pf-c-login__main-footer-links-item-link"
|
|
|
|
).click()
|
|
|
|
|
|
|
|
# Now we should be at the IDP, wait for the login field
|
|
|
|
self.wait.until(ec.presence_of_element_located((By.NAME, "username")))
|
|
|
|
self.driver.find_element(By.NAME, "username").send_keys("example-user")
|
|
|
|
self.driver.find_element(By.NAME, "username").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
# Wait until we're logged in
|
|
|
|
self.wait.until(
|
|
|
|
ec.presence_of_element_located((By.CSS_SELECTOR, "[name='confirm']"))
|
|
|
|
)
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "[name='confirm']").click()
|
|
|
|
|
|
|
|
# Wait until we've loaded the user info page
|
2020-10-19 15:44:46 +01:00
|
|
|
sleep(2)
|
2020-09-28 23:27:58 +01:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "user-settings")))
|
|
|
|
self.driver.get(self.url("passbook_core:user-settings"))
|
2020-09-26 16:44:05 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-09-30 18:34:22 +01:00
|
|
|
self.driver.find_element(By.ID, "user-settings").text,
|
|
|
|
"example-user",
|
2020-09-26 16:44:05 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2020-09-26 17:17:53 +01:00
|
|
|
self.driver.find_element(By.ID, "id_username").get_attribute("value"),
|
|
|
|
"example-user",
|
2020-09-26 16:44:05 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2020-09-26 17:17:53 +01:00
|
|
|
self.driver.find_element(By.ID, "id_name").get_attribute("value"),
|
|
|
|
"test name",
|
2020-09-26 16:44:05 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
self.driver.find_element(By.ID, "id_email").get_attribute("value"),
|
|
|
|
"foo@example.com",
|
|
|
|
)
|