1 import ./make-test-python.nix ({ lib, ... }: {
3 meta.maintainers = with lib.maintainers; [ Flakebi ];
5 nodes.machine = { pkgs, ... }: {
6 environment.systemPackages = with pkgs; [ jq ];
10 issuer = "http://127.0.0.1:8080/dex";
13 config.host = "/var/run/postgresql";
15 web.http = "127.0.0.1:8080";
16 oauth2.skipApprovalScreen = true;
21 redirectURIs = [ "https://example.com/callback" ];
22 secretFile = "/etc/dex/oidcclient";
27 type = "mockPassword";
32 password = "password";
39 # This should not be set from nix but through other means to not leak the secret.
40 environment.etc."dex/oidcclient" = {
43 text = "oidcclientsecret";
46 services.postgresql = {
48 ensureDatabases =[ "dex" ];
52 ensureDBOwnership = true;
59 with subtest("Web server gets ready"):
60 machine.wait_for_unit("dex.service", timeout=120)
61 # Wait until server accepts connections
62 machine.wait_until_succeeds("curl -fs 'localhost:8080/dex/auth/mock?client_id=oidcclient&response_type=code&redirect_uri=https://example.com/callback&scope=openid'", timeout=120)
64 with subtest("Login"):
65 state = machine.succeed("curl -fs 'localhost:8080/dex/auth/mock?client_id=oidcclient&response_type=code&redirect_uri=https://example.com/callback&scope=openid' | sed -n 's/.*state=\\(.*\\)\">.*/\\1/p'").strip()
66 print(f"Got state {state}")
67 # Login request returns 303 with redirect_url that has code as query parameter:
68 # https://example.com/callback?code=kibsamwdupuy2iwqnlbqei3u6&state=
69 code = machine.succeed(f"curl -fs 'localhost:8080/dex/auth/mock/login?back=&state={state}' -d 'login=admin&password=password' -w '%{{redirect_url}}' | sed -n 's/.*code=\\(.*\\)&.*/\\1/p'")
70 print(f"Got approval code {code}")
71 bearer = machine.succeed(f"curl -fs localhost:8080/dex/token -u oidcclient:oidcclientsecret -d 'grant_type=authorization_code&redirect_uri=https://example.com/callback&code={code}' | jq .access_token -r").strip()
72 print(f"Got access token {bearer}")
74 with subtest("Get userinfo"):
75 assert '"sub"' in machine.succeed(
76 f"curl -fs localhost:8080/dex/userinfo --oauth2-bearer {bearer}"