2020-07-08 22:41:14 +00:00
|
|
|
"""test OAuth Source"""
|
2022-08-17 21:00:47 +00:00
|
|
|
from pathlib import Path
|
2020-09-11 21:21:11 +00:00
|
|
|
from sys import platform
|
2020-09-13 13:39:25 +00:00
|
|
|
from time import sleep
|
2021-02-18 12:41:03 +00:00
|
|
|
from typing import Any, Optional
|
2020-09-11 21:21:11 +00:00
|
|
|
from unittest.case import skipUnless
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2020-09-09 22:14:59 +00:00
|
|
|
from docker.models.containers import Container
|
|
|
|
from docker.types import Healthcheck
|
2020-07-08 22:41:14 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
from selenium.webdriver.support import expected_conditions as ec
|
2021-02-27 23:27:18 +00:00
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
2020-07-09 22:14:48 +00:00
|
|
|
from yaml import safe_dump
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2022-08-02 22:05:49 +00:00
|
|
|
from authentik.blueprints.tests import apply_blueprint
|
2021-03-30 09:05:31 +00:00
|
|
|
from authentik.core.models import User
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.flows.models import Flow
|
2021-08-23 18:27:38 +00:00
|
|
|
from authentik.lib.generators import generate_id, generate_key
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.sources.oauth.models import OAuthSource
|
2022-08-29 19:20:58 +00:00
|
|
|
from authentik.sources.oauth.types.registry import SourceType, registry
|
2022-05-17 22:03:02 +00:00
|
|
|
from authentik.sources.oauth.views.callback import OAuthCallback
|
2021-05-25 15:35:24 +00:00
|
|
|
from authentik.stages.identification.models import IdentificationStage
|
2022-08-01 21:05:58 +00:00
|
|
|
from tests.e2e.utils import SeleniumTestCase, retry
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2020-12-07 10:21:07 +00:00
|
|
|
CONFIG_PATH = "/tmp/dex.yml" # nosec
|
2020-07-08 22:41:14 +00:00
|
|
|
|
|
|
|
|
2022-09-06 22:23:25 +00:00
|
|
|
class OAuth1Callback(OAuthCallback):
|
2022-05-17 22:03:02 +00:00
|
|
|
"""OAuth1 Callback with custom getters"""
|
|
|
|
|
|
|
|
def get_user_id(self, info: dict[str, str]) -> str:
|
|
|
|
return info.get("id")
|
|
|
|
|
|
|
|
def get_user_enroll_context(
|
|
|
|
self,
|
|
|
|
info: dict[str, Any],
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"username": info.get("screen_name"),
|
|
|
|
"email": info.get("email"),
|
|
|
|
"name": info.get("name"),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 19:20:58 +00:00
|
|
|
@registry.register()
|
2021-04-20 16:48:01 +00:00
|
|
|
class OAUth1Type(SourceType):
|
2022-05-17 22:03:02 +00:00
|
|
|
"""OAuth1 Type definition"""
|
2021-04-20 16:48:01 +00:00
|
|
|
|
2022-09-06 22:23:25 +00:00
|
|
|
callback_view = OAuth1Callback
|
2022-05-17 22:03:02 +00:00
|
|
|
name = "OAuth1"
|
|
|
|
slug = "oauth1"
|
2021-04-20 16:48:01 +00:00
|
|
|
|
|
|
|
request_token_url = "http://localhost:5000/oauth/request_token" # nosec
|
|
|
|
access_token_url = "http://localhost:5000/oauth/access_token" # nosec
|
|
|
|
authorization_url = "http://localhost:5000/oauth/authorize"
|
|
|
|
profile_url = "http://localhost:5000/api/me"
|
|
|
|
urls_customizable = False
|
|
|
|
|
|
|
|
|
2020-09-11 21:21:11 +00:00
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2020-09-26 15:44:05 +00:00
|
|
|
class TestSourceOAuth2(SeleniumTestCase):
|
2020-07-08 22:41:14 +00:00
|
|
|
"""test OAuth Source flow"""
|
|
|
|
|
|
|
|
container: Container
|
|
|
|
|
|
|
|
def setUp(self):
|
2021-08-23 18:27:38 +00:00
|
|
|
self.client_secret = generate_key()
|
2020-09-11 21:21:11 +00:00
|
|
|
self.prepare_dex_config()
|
2020-07-10 14:49:25 +00:00
|
|
|
super().setUp()
|
2020-07-08 22:41:14 +00:00
|
|
|
|
|
|
|
def prepare_dex_config(self):
|
|
|
|
"""Since Dex does not document which environment
|
|
|
|
variables can be used to configure clients"""
|
2020-07-09 21:13:14 +00: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(
|
2020-12-05 21:08:42 +00:00
|
|
|
"authentik_sources_oauth:oauth-client-callback",
|
2020-07-09 21:13:14 +00:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
],
|
2020-12-07 10:21:07 +00:00
|
|
|
"storage": {"config": {"file": "/tmp/dex.db"}, "type": "sqlite3"}, # nosec
|
2020-07-09 21:13:14 +00:00
|
|
|
"web": {"http": "0.0.0.0:5556"},
|
|
|
|
}
|
2021-08-23 08:10:31 +00:00
|
|
|
with open(CONFIG_PATH, "w+", encoding="utf8") as _file:
|
2020-07-08 22:41:14 +00:00
|
|
|
safe_dump(config, _file)
|
|
|
|
|
2021-02-18 12:41:03 +00:00
|
|
|
def get_container_specs(self) -> Optional[dict[str, Any]]:
|
2020-09-11 21:21:11 +00:00
|
|
|
return {
|
2022-09-04 15:58:30 +00:00
|
|
|
"image": "ghcr.io/dexidp/dex:v2.28.1",
|
2020-09-11 21:21:11 +00:00
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
2021-03-22 21:27:57 +00:00
|
|
|
"command": "dex serve /config.yml",
|
2020-09-11 21:21:11 +00:00
|
|
|
"healthcheck": Healthcheck(
|
2020-07-08 22:41:14 +00:00
|
|
|
test=["CMD", "wget", "--spider", "http://localhost:5556/dex/healthz"],
|
2023-12-19 17:29:44 +00:00
|
|
|
interval=5 * 1_000 * 1_000_000,
|
|
|
|
start_period=1 * 1_000 * 1_000_000,
|
2020-07-08 22:41:14 +00:00
|
|
|
),
|
2022-08-17 21:00:47 +00:00
|
|
|
"volumes": {str(Path(CONFIG_PATH).absolute()): {"bind": "/config.yml", "mode": "ro"}},
|
2020-09-11 21:21:11 +00:00
|
|
|
}
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2020-07-09 21:13:14 +00:00
|
|
|
def create_objects(self):
|
|
|
|
"""Create required objects"""
|
2020-07-08 22:41:14 +00:00
|
|
|
# Bootstrap all needed objects
|
|
|
|
authentication_flow = Flow.objects.get(slug="default-source-authentication")
|
|
|
|
enrollment_flow = Flow.objects.get(slug="default-source-enrollment")
|
|
|
|
|
2021-05-25 15:35:24 +00:00
|
|
|
source = OAuthSource.objects.create( # nosec
|
2020-07-08 22:41:14 +00:00
|
|
|
name="dex",
|
|
|
|
slug="dex",
|
|
|
|
authentication_flow=authentication_flow,
|
|
|
|
enrollment_flow=enrollment_flow,
|
2021-08-21 16:36:06 +00:00
|
|
|
provider_type="openidconnect",
|
2020-07-08 22:41:14 +00:00
|
|
|
authorization_url="http://127.0.0.1:5556/dex/auth",
|
2020-09-26 17:08:37 +00:00
|
|
|
access_token_url="http://127.0.0.1:5556/dex/token",
|
2020-07-08 22:41:14 +00:00
|
|
|
profile_url="http://127.0.0.1:5556/dex/userinfo",
|
|
|
|
consumer_key="example-app",
|
2020-07-09 12:59:25 +00:00
|
|
|
consumer_secret=self.client_secret,
|
2020-07-08 22:41:14 +00:00
|
|
|
)
|
2021-05-25 15:35:24 +00:00
|
|
|
ident_stage = IdentificationStage.objects.first()
|
|
|
|
ident_stage.sources.set([source])
|
|
|
|
ident_stage.save()
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-authentication-flow.yaml",
|
|
|
|
"default/flow-default-invalidation-flow.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-source-authentication.yaml",
|
|
|
|
"default/flow-default-source-enrollment.yaml",
|
|
|
|
"default/flow-default-source-pre-authentication.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
2020-07-09 21:13:14 +00:00
|
|
|
def test_oauth_enroll(self):
|
|
|
|
"""test OAuth Source With With OIDC"""
|
|
|
|
self.create_objects()
|
2020-07-08 22:41:14 +00:00
|
|
|
self.driver.get(self.live_server_url)
|
|
|
|
|
2021-02-27 23:27:18 +00:00
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
2021-08-03 15:45:16 +00:00
|
|
|
identification_stage = self.get_shadow_root("ak-stage-identification", flow_executor)
|
2021-02-27 23:27:18 +00:00
|
|
|
wait = WebDriverWait(identification_stage, self.wait_timeout)
|
|
|
|
|
|
|
|
wait.until(
|
2020-07-08 22:41:14 +00:00
|
|
|
ec.presence_of_element_located(
|
2021-05-03 19:40:57 +00:00
|
|
|
(By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button")
|
2020-07-08 22:41:14 +00:00
|
|
|
)
|
|
|
|
)
|
2021-02-27 23:27:18 +00:00
|
|
|
identification_stage.find_element(
|
2021-05-03 19:40:57 +00:00
|
|
|
By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button"
|
2020-07-08 22:41:14 +00:00
|
|
|
).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
|
2021-08-03 15:45:16 +00:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]")))
|
2020-07-08 22:41:14 +00:00
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
|
|
|
# At this point we've been redirected back
|
|
|
|
# and we're asked for the username
|
2021-02-27 23:27:18 +00:00
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
2021-02-27 23:30:33 +00:00
|
|
|
prompt_stage = self.get_shadow_root("ak-stage-prompt", flow_executor)
|
2021-02-27 23:27:18 +00:00
|
|
|
|
2021-02-27 23:30:33 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=username]").click()
|
2021-08-03 15:45:16 +00:00
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=username]").send_keys("foo")
|
|
|
|
prompt_stage.find_element(By.CSS_SELECTOR, "input[name=username]").send_keys(Keys.ENTER)
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2020-11-23 13:24:42 +00:00
|
|
|
# Wait until we've logged in
|
2021-09-16 15:30:16 +00:00
|
|
|
self.wait_for_url(self.if_user_url("/library"))
|
2021-09-16 20:48:34 +00:00
|
|
|
self.driver.get(self.if_user_url("/settings"))
|
2020-07-08 22:41:14 +00:00
|
|
|
|
2021-03-30 09:05:31 +00:00
|
|
|
self.assert_user(User(username="foo", name="admin", email="admin@example.com"))
|
2020-07-09 22:14:48 +00:00
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2020-07-09 22:14:48 +00: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-12-05 21:08:42 +00:00
|
|
|
self.driver.get(self.url("authentik_flows:default-invalidation"))
|
2021-02-27 23:27:18 +00:00
|
|
|
sleep(1)
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
2021-08-03 15:45:16 +00:00
|
|
|
identification_stage = self.get_shadow_root("ak-stage-identification", flow_executor)
|
2021-02-27 23:27:18 +00:00
|
|
|
wait = WebDriverWait(identification_stage, self.wait_timeout)
|
2020-07-09 22:14:48 +00:00
|
|
|
|
2021-02-27 23:27:18 +00:00
|
|
|
wait.until(
|
2020-07-09 22:14:48 +00:00
|
|
|
ec.presence_of_element_located(
|
2021-05-03 19:40:57 +00:00
|
|
|
(By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button")
|
2020-07-09 22:14:48 +00:00
|
|
|
)
|
|
|
|
)
|
2021-02-27 23:27:18 +00:00
|
|
|
identification_stage.find_element(
|
2021-05-03 19:40:57 +00:00
|
|
|
By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button"
|
2020-07-09 22:14:48 +00:00
|
|
|
).click()
|
2021-02-27 23:27:18 +00:00
|
|
|
|
2020-07-09 22:14:48 +00: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
|
2021-08-03 15:45:16 +00:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "button[type=submit]")))
|
2020-07-09 22:14:48 +00:00
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
2020-11-23 13:24:42 +00:00
|
|
|
# Wait until we've logged in
|
2021-09-16 15:30:16 +00:00
|
|
|
self.wait_for_url(self.if_user_url("/library"))
|
2021-09-16 20:48:34 +00:00
|
|
|
self.driver.get(self.if_user_url("/settings"))
|
2020-07-09 22:14:48 +00:00
|
|
|
|
2021-03-30 09:05:31 +00:00
|
|
|
self.assert_user(User(username="foo", name="admin", email="admin@example.com"))
|
2020-09-26 15:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
|
|
|
class TestSourceOAuth1(SeleniumTestCase):
|
2020-09-26 17:08:37 +00:00
|
|
|
"""Test OAuth1 Source"""
|
|
|
|
|
2020-09-26 15:44:05 +00:00
|
|
|
def setUp(self) -> None:
|
2021-08-23 18:27:38 +00:00
|
|
|
self.client_id = generate_id()
|
|
|
|
self.client_secret = generate_key()
|
2020-09-26 15:44:05 +00:00
|
|
|
self.source_slug = "oauth1-test"
|
|
|
|
super().setUp()
|
|
|
|
|
2021-02-18 12:41:03 +00:00
|
|
|
def get_container_specs(self) -> Optional[dict[str, Any]]:
|
2020-09-26 15:44:05 +00:00
|
|
|
return {
|
2023-05-24 09:03:26 +00:00
|
|
|
"image": "ghcr.io/beryju/oauth1-test-server:v1.1",
|
2020-09-26 15:44:05 +00:00
|
|
|
"detach": True,
|
|
|
|
"network_mode": "host",
|
|
|
|
"auto_remove": True,
|
|
|
|
"environment": {
|
|
|
|
"OAUTH1_CLIENT_ID": self.client_id,
|
|
|
|
"OAUTH1_CLIENT_SECRET": self.client_secret,
|
2023-02-01 10:31:32 +00:00
|
|
|
"OAUTH1_REDIRECT_URI": self.url(
|
|
|
|
"authentik_sources_oauth:oauth-client-callback",
|
|
|
|
source_slug=self.source_slug,
|
2020-09-26 15:44:05 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-05-25 15:35:24 +00:00
|
|
|
source = OAuthSource.objects.create( # nosec
|
2020-09-26 15:44:05 +00:00
|
|
|
name="oauth1",
|
|
|
|
slug=self.source_slug,
|
|
|
|
authentication_flow=authentication_flow,
|
|
|
|
enrollment_flow=enrollment_flow,
|
2022-05-17 22:03:02 +00:00
|
|
|
provider_type="oauth1",
|
2020-09-26 15:44:05 +00:00
|
|
|
consumer_key=self.client_id,
|
|
|
|
consumer_secret=self.client_secret,
|
|
|
|
)
|
2021-05-25 15:35:24 +00:00
|
|
|
ident_stage = IdentificationStage.objects.first()
|
|
|
|
ident_stage.sources.set([source])
|
|
|
|
ident_stage.save()
|
2020-09-26 15:44:05 +00:00
|
|
|
|
2020-10-20 16:42:26 +00:00
|
|
|
@retry()
|
2022-08-01 21:05:58 +00:00
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-authentication-flow.yaml",
|
|
|
|
"default/flow-default-invalidation-flow.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
|
|
|
@apply_blueprint(
|
2023-01-24 11:23:22 +00:00
|
|
|
"default/flow-default-source-authentication.yaml",
|
|
|
|
"default/flow-default-source-enrollment.yaml",
|
|
|
|
"default/flow-default-source-pre-authentication.yaml",
|
2022-08-01 21:05:58 +00:00
|
|
|
)
|
2020-09-26 15:44:05 +00:00
|
|
|
def test_oauth_enroll(self):
|
|
|
|
"""test OAuth Source With With OIDC"""
|
|
|
|
self.create_objects()
|
|
|
|
self.driver.get(self.live_server_url)
|
|
|
|
|
2021-02-27 23:27:18 +00:00
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
2021-08-03 15:45:16 +00:00
|
|
|
identification_stage = self.get_shadow_root("ak-stage-identification", flow_executor)
|
2021-02-27 23:27:18 +00:00
|
|
|
wait = WebDriverWait(identification_stage, self.wait_timeout)
|
|
|
|
|
|
|
|
wait.until(
|
2020-09-26 15:44:05 +00:00
|
|
|
ec.presence_of_element_located(
|
2021-05-03 19:40:57 +00:00
|
|
|
(By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button")
|
2020-09-26 15:44:05 +00:00
|
|
|
)
|
|
|
|
)
|
2021-02-27 23:27:18 +00:00
|
|
|
identification_stage.find_element(
|
2021-05-03 19:40:57 +00:00
|
|
|
By.CSS_SELECTOR, ".pf-c-login__main-footer-links-item > button"
|
2020-09-26 15:44:05 +00:00
|
|
|
).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)
|
2021-05-03 19:40:57 +00:00
|
|
|
sleep(2)
|
2020-09-26 15:44:05 +00:00
|
|
|
|
|
|
|
# Wait until we're logged in
|
2021-08-03 15:45:16 +00:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "[name='confirm']")))
|
2020-09-26 15:44:05 +00:00
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "[name='confirm']").click()
|
|
|
|
|
|
|
|
# Wait until we've loaded the user info page
|
2020-10-19 14:44:46 +00:00
|
|
|
sleep(2)
|
2020-11-23 13:24:42 +00:00
|
|
|
# Wait until we've logged in
|
2021-09-16 15:30:16 +00:00
|
|
|
self.wait_for_url(self.if_user_url("/library"))
|
2021-09-16 20:48:34 +00:00
|
|
|
self.driver.get(self.if_user_url("/settings"))
|
2020-09-26 15:44:05 +00:00
|
|
|
|
2021-08-03 15:45:16 +00:00
|
|
|
self.assert_user(User(username="example-user", name="test name", email="foo@example.com"))
|