summary refs log tree commit diff
path: root/nixos/tests/dex-oidc.nix
blob: 37275a97ef0fb62a0436596d39779847b3b1dc0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import ./make-test-python.nix ({ lib, ... }: {
  name = "dex-oidc";
  meta.maintainers = with lib.maintainers; [ Flakebi ];

  nodes.machine = { pkgs, ... }: {
    environment.systemPackages = with pkgs; [ jq ];
    services.dex = {
      enable = true;
      settings = {
        issuer = "http://127.0.0.1:8080/dex";
        storage = {
          type = "postgres";
          config.host = "/var/run/postgresql";
        };
        web.http = "127.0.0.1:8080";
        oauth2.skipApprovalScreen = true;
        staticClients = [
          {
            id = "oidcclient";
            name = "Client";
            redirectURIs = [ "https://example.com/callback" ];
            secretFile = "/etc/dex/oidcclient";
          }
        ];
        connectors = [
          {
            type = "mockPassword";
            id = "mock";
            name = "Example";
            config = {
              username = "admin";
              password = "password";
            };
          }
        ];
      };
    };

    # This should not be set from nix but through other means to not leak the secret.
    environment.etc."dex/oidcclient" = {
      mode = "0400";
      user = "dex";
      text = "oidcclientsecret";
    };

    services.postgresql = {
      enable = true;
      ensureDatabases =[ "dex" ];
      ensureUsers = [
        {
          name = "dex";
          ensurePermissions = { "DATABASE dex" = "ALL PRIVILEGES"; };
        }
      ];
    };
  };

  testScript = ''
    with subtest("Web server gets ready"):
        machine.wait_for_unit("dex.service")
        # Wait until server accepts connections
        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'")

    with subtest("Login"):
        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()
        print(f"Got state {state}")
        machine.succeed(f"curl -fs 'localhost:8080/dex/auth/mock/login?back=&state={state}' -d 'login=admin&password=password'")
        code = machine.succeed(f"curl -fs localhost:8080/dex/approval?req={state} | sed -n 's/.*code=\\(.*\\)&.*/\\1/p'").strip()
        print(f"Got approval code {code}")
        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()
        print(f"Got access token {bearer}")

    with subtest("Get userinfo"):
        assert '"sub"' in machine.succeed(
            f"curl -fs localhost:8080/dex/userinfo --oauth2-bearer {bearer}"
        )
  '';
})