2020-09-25 18:39:49 +01:00
|
|
|
"""test flow with otp stages"""
|
|
|
|
from base64 import b32decode
|
|
|
|
from sys import platform
|
|
|
|
from time import sleep
|
|
|
|
from unittest.case import skipUnless
|
|
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
|
|
|
|
from django_otp.oath import TOTP
|
|
|
|
from django_otp.plugins.otp_static.models import StaticDevice, StaticToken
|
|
|
|
from django_otp.plugins.otp_totp.models import TOTPDevice
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
2020-09-28 17:17:07 +01:00
|
|
|
from selenium.webdriver.support import expected_conditions as ec
|
2020-09-25 18:39:49 +01:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
from authentik.flows.models import Flow, FlowStageBinding
|
2021-02-17 19:49:58 +00:00
|
|
|
from authentik.stages.authenticator_static.models import AuthenticatorStaticStage
|
|
|
|
from authentik.stages.authenticator_totp.models import AuthenticatorTOTPStage
|
|
|
|
from authentik.stages.authenticator_validate.models import AuthenticatorValidateStage
|
2020-11-19 13:25:53 +00:00
|
|
|
from tests.e2e.utils import USER, SeleniumTestCase, retry
|
2020-09-25 18:39:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(platform.startswith("linux"), "requires local docker")
|
2021-02-17 19:49:58 +00:00
|
|
|
class TestFlowsAuthenticator(SeleniumTestCase):
|
2020-09-25 18:39:49 +01:00
|
|
|
"""test flow with otp stages"""
|
|
|
|
|
2020-10-20 17:42:26 +01:00
|
|
|
@retry()
|
2021-02-17 19:49:58 +00:00
|
|
|
def test_totp_validate(self):
|
2020-09-25 18:39:49 +01:00
|
|
|
"""test flow with otp stages"""
|
|
|
|
sleep(1)
|
|
|
|
# Setup TOTP Device
|
|
|
|
user = USER()
|
|
|
|
device = TOTPDevice.objects.create(user=user, confirmed=True, digits=6)
|
|
|
|
|
|
|
|
flow: Flow = Flow.objects.get(slug="default-authentication-flow")
|
|
|
|
FlowStageBinding.objects.create(
|
2021-02-17 19:49:58 +00:00
|
|
|
target=flow, order=30, stage=AuthenticatorValidateStage.objects.create()
|
2020-09-25 18:39:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.driver.get(f"{self.live_server_url}/flows/{flow.slug}/")
|
2021-02-23 12:50:47 +00:00
|
|
|
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
identification_stage = self.get_shadow_root(
|
|
|
|
"ak-stage-identification", flow_executor
|
|
|
|
)
|
|
|
|
|
|
|
|
identification_stage.find_element(
|
|
|
|
By.CSS_SELECTOR, "input[name=uid_field]"
|
|
|
|
).click()
|
|
|
|
identification_stage.find_element(
|
|
|
|
By.CSS_SELECTOR, "input[name=uid_field]"
|
|
|
|
).send_keys(USER().username)
|
|
|
|
identification_stage.find_element(
|
|
|
|
By.CSS_SELECTOR, "input[name=uid_field]"
|
|
|
|
).send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
password_stage = self.get_shadow_root("ak-stage-password", flow_executor)
|
|
|
|
password_stage.find_element(By.CSS_SELECTOR, "input[name=password]").send_keys(
|
|
|
|
USER().username
|
|
|
|
)
|
|
|
|
password_stage.find_element(By.CSS_SELECTOR, "input[name=password]").send_keys(
|
|
|
|
Keys.ENTER
|
|
|
|
)
|
2020-09-25 18:39:49 +01:00
|
|
|
|
|
|
|
# Get expected token
|
|
|
|
totp = TOTP(device.bin_key, device.step, device.t0, device.digits, device.drift)
|
2021-02-23 12:50:47 +00:00
|
|
|
|
|
|
|
flow_executor = self.get_shadow_root("ak-flow-executor")
|
|
|
|
identification_stage = self.get_shadow_root(
|
|
|
|
"ak-stage-identification", flow_executor
|
|
|
|
)
|
|
|
|
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "input[name=code]").send_keys(
|
|
|
|
totp.token()
|
|
|
|
)
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "input[name=code]").send_keys(
|
|
|
|
Keys.ENTER
|
|
|
|
)
|
2020-12-05 21:08:42 +00:00
|
|
|
self.wait_for_url(self.shell_url("authentik_core:overview"))
|
2020-11-23 13:24:42 +00:00
|
|
|
self.assert_user(USER())
|
2020-09-25 18:39:49 +01:00
|
|
|
|
2020-10-20 17:42:26 +01:00
|
|
|
@retry()
|
2021-02-17 19:49:58 +00:00
|
|
|
def test_totp_setup(self):
|
2020-09-25 18:39:49 +01:00
|
|
|
"""test TOTP Setup stage"""
|
|
|
|
flow: Flow = Flow.objects.get(slug="default-authentication-flow")
|
|
|
|
|
|
|
|
self.driver.get(f"{self.live_server_url}/flows/{flow.slug}/")
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").click()
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER)
|
2020-12-05 21:08:42 +00:00
|
|
|
self.wait_for_url(self.shell_url("authentik_core:overview"))
|
2020-11-23 13:24:42 +00:00
|
|
|
self.assert_user(USER())
|
|
|
|
|
|
|
|
self.driver.get(
|
|
|
|
self.url(
|
2020-12-05 21:08:42 +00:00
|
|
|
"authentik_flows:configure",
|
2021-02-17 19:49:58 +00:00
|
|
|
stage_uuid=AuthenticatorTOTPStage.objects.first().stage_uuid,
|
2020-11-23 13:24:42 +00:00
|
|
|
)
|
2020-09-25 18:39:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Remember the current URL as we should end up back here
|
|
|
|
destination_url = self.driver.current_url
|
|
|
|
|
2020-09-28 17:17:07 +01:00
|
|
|
self.wait.until(ec.presence_of_element_located((By.ID, "qr")))
|
|
|
|
otp_uri = self.driver.find_element(By.ID, "qr").get_attribute("data-otpuri")
|
2020-09-25 18:39:49 +01:00
|
|
|
|
|
|
|
# Parse the OTP URI, extract the secret and get the next token
|
|
|
|
otp_args = urlparse(otp_uri)
|
|
|
|
self.assertEqual(otp_args.scheme, "otpauth")
|
|
|
|
otp_qs = parse_qs(otp_args.query)
|
|
|
|
secret_key = b32decode(otp_qs["secret"][0])
|
|
|
|
|
|
|
|
totp = TOTP(secret_key)
|
|
|
|
|
|
|
|
self.driver.find_element(By.ID, "id_code").send_keys(totp.token())
|
|
|
|
self.driver.find_element(By.ID, "id_code").send_keys(Keys.ENTER)
|
|
|
|
|
|
|
|
self.wait_for_url(destination_url)
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
self.assertTrue(TOTPDevice.objects.filter(user=USER(), confirmed=True).exists())
|
|
|
|
|
2020-10-20 17:42:26 +01:00
|
|
|
@retry()
|
2021-02-17 19:49:58 +00:00
|
|
|
def test_static_setup(self):
|
2020-09-25 18:39:49 +01:00
|
|
|
"""test Static OTP Setup stage"""
|
|
|
|
flow: Flow = Flow.objects.get(slug="default-authentication-flow")
|
|
|
|
|
|
|
|
self.driver.get(f"{self.live_server_url}/flows/{flow.slug}/")
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").click()
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_uid_field").send_keys(Keys.ENTER)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(USER().username)
|
|
|
|
self.driver.find_element(By.ID, "id_password").send_keys(Keys.ENTER)
|
2020-12-05 21:08:42 +00:00
|
|
|
self.wait_for_url(self.shell_url("authentik_core:overview"))
|
2020-11-23 13:24:42 +00:00
|
|
|
self.assert_user(USER())
|
|
|
|
|
|
|
|
self.driver.get(
|
|
|
|
self.url(
|
2020-12-05 21:08:42 +00:00
|
|
|
"authentik_flows:configure",
|
2021-02-17 19:49:58 +00:00
|
|
|
stage_uuid=AuthenticatorStaticStage.objects.first().stage_uuid,
|
2020-11-23 13:24:42 +00:00
|
|
|
)
|
2020-09-25 18:39:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Remember the current URL as we should end up back here
|
|
|
|
destination_url = self.driver.current_url
|
|
|
|
|
|
|
|
token = self.driver.find_element(
|
2020-12-05 21:08:42 +00:00
|
|
|
By.CSS_SELECTOR, ".ak-otp-tokens li:nth-child(1)"
|
2020-09-25 18:39:49 +01:00
|
|
|
).text
|
|
|
|
|
|
|
|
self.driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
|
|
|
|
|
|
|
|
self.wait_for_url(destination_url)
|
|
|
|
sleep(1)
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
StaticDevice.objects.filter(user=USER(), confirmed=True).exists()
|
|
|
|
)
|
|
|
|
device = StaticDevice.objects.filter(user=USER(), confirmed=True).first()
|
|
|
|
self.assertTrue(StaticToken.objects.filter(token=token, device=device).exists())
|