|
1 | 1 | package application |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
4 | 5 | "net/url" |
5 | 6 | "regexp" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 | "goauthentik.io/api/v3" |
| 11 | + "goauthentik.io/internal/constants" |
| 12 | + "goauthentik.io/internal/outpost/proxyv2/types" |
10 | 13 | ) |
11 | 14 |
|
12 | 15 | func urlMustParse(u string) *url.URL { |
@@ -48,3 +51,135 @@ func TestIsAllowlisted_Proxy_Domain(t *testing.T) { |
48 | 51 | assert.Equal(t, false, a.IsAllowlisted(urlMustParse("https://health.domain.tld/"))) |
49 | 52 | assert.Equal(t, true, a.IsAllowlisted(urlMustParse("https://health.domain.tld/ping/qq"))) |
50 | 53 | } |
| 54 | + |
| 55 | +func TestAdHeaders_Standard(t *testing.T) { |
| 56 | + a := newTestApplication() |
| 57 | + h := http.Header{} |
| 58 | + a.addHeaders(h, &types.Claims{ |
| 59 | + PreferredUsername: "foo", |
| 60 | + Groups: []string{"foo", "bar"}, |
| 61 | + Entitlements: []string{"bar", "quox"}, |
| 62 | + Email: "bar@authentik.company", |
| 63 | + Name: "foo", |
| 64 | + Sub: "bar", |
| 65 | + RawToken: "baz", |
| 66 | + }) |
| 67 | + assert.Equal(t, http.Header{ |
| 68 | + "X-Authentik-Email": []string{"bar@authentik.company"}, |
| 69 | + "X-Authentik-Entitlements": []string{"bar|quox"}, |
| 70 | + "X-Authentik-Groups": []string{"foo|bar"}, |
| 71 | + "X-Authentik-Jwt": []string{"baz"}, |
| 72 | + "X-Authentik-Meta-App": []string{""}, |
| 73 | + "X-Authentik-Meta-Jwks": []string{""}, |
| 74 | + "X-Authentik-Meta-Outpost": []string{""}, |
| 75 | + "X-Authentik-Meta-Provider": []string{a.proxyConfig.Name}, |
| 76 | + "X-Authentik-Meta-Version": []string{constants.UserAgentOutpost()}, |
| 77 | + "X-Authentik-Name": []string{"foo"}, |
| 78 | + "X-Authentik-Uid": []string{"bar"}, |
| 79 | + "X-Authentik-Username": []string{"foo"}, |
| 80 | + }, h) |
| 81 | +} |
| 82 | + |
| 83 | +func TestAdHeaders_BasicAuth(t *testing.T) { |
| 84 | + a := newTestApplication() |
| 85 | + a.proxyConfig.BasicAuthEnabled = api.PtrBool(true) |
| 86 | + a.proxyConfig.BasicAuthUserAttribute = api.PtrString("user") |
| 87 | + a.proxyConfig.BasicAuthPasswordAttribute = api.PtrString("pass") |
| 88 | + h := http.Header{} |
| 89 | + a.addHeaders(h, &types.Claims{ |
| 90 | + PreferredUsername: "foo", |
| 91 | + Groups: []string{"foo", "bar"}, |
| 92 | + Entitlements: []string{"bar", "quox"}, |
| 93 | + Email: "bar@authentik.company", |
| 94 | + Name: "foo", |
| 95 | + Sub: "bar", |
| 96 | + RawToken: "baz", |
| 97 | + Proxy: &types.ProxyClaims{ |
| 98 | + UserAttributes: map[string]any{ |
| 99 | + "user": "foo", |
| 100 | + "pass": "baz", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }) |
| 104 | + assert.Equal(t, http.Header{ |
| 105 | + "Authorization": []string{"Basic Zm9vOmJheg=="}, |
| 106 | + "X-Authentik-Email": []string{"bar@authentik.company"}, |
| 107 | + "X-Authentik-Entitlements": []string{"bar|quox"}, |
| 108 | + "X-Authentik-Groups": []string{"foo|bar"}, |
| 109 | + "X-Authentik-Jwt": []string{"baz"}, |
| 110 | + "X-Authentik-Meta-App": []string{""}, |
| 111 | + "X-Authentik-Meta-Jwks": []string{""}, |
| 112 | + "X-Authentik-Meta-Outpost": []string{""}, |
| 113 | + "X-Authentik-Meta-Provider": []string{a.proxyConfig.Name}, |
| 114 | + "X-Authentik-Meta-Version": []string{constants.UserAgentOutpost()}, |
| 115 | + "X-Authentik-Name": []string{"foo"}, |
| 116 | + "X-Authentik-Uid": []string{"bar"}, |
| 117 | + "X-Authentik-Username": []string{"foo"}, |
| 118 | + }, h) |
| 119 | +} |
| 120 | + |
| 121 | +func TestAdHeaders_Extra(t *testing.T) { |
| 122 | + a := newTestApplication() |
| 123 | + h := http.Header{} |
| 124 | + a.addHeaders(h, &types.Claims{ |
| 125 | + PreferredUsername: "foo", |
| 126 | + Groups: []string{"foo", "bar"}, |
| 127 | + Entitlements: []string{"bar", "quox"}, |
| 128 | + Email: "bar@authentik.company", |
| 129 | + Name: "foo", |
| 130 | + Sub: "bar", |
| 131 | + RawToken: "baz", |
| 132 | + Proxy: &types.ProxyClaims{ |
| 133 | + UserAttributes: map[string]any{ |
| 134 | + "additionalHeaders": map[string]any{ |
| 135 | + "foo": "bar", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }) |
| 140 | + assert.Equal(t, http.Header{ |
| 141 | + "Foo": []string{"bar"}, |
| 142 | + "X-Authentik-Email": []string{"bar@authentik.company"}, |
| 143 | + "X-Authentik-Entitlements": []string{"bar|quox"}, |
| 144 | + "X-Authentik-Groups": []string{"foo|bar"}, |
| 145 | + "X-Authentik-Jwt": []string{"baz"}, |
| 146 | + "X-Authentik-Meta-App": []string{""}, |
| 147 | + "X-Authentik-Meta-Jwks": []string{""}, |
| 148 | + "X-Authentik-Meta-Outpost": []string{""}, |
| 149 | + "X-Authentik-Meta-Provider": []string{a.proxyConfig.Name}, |
| 150 | + "X-Authentik-Meta-Version": []string{constants.UserAgentOutpost()}, |
| 151 | + "X-Authentik-Name": []string{"foo"}, |
| 152 | + "X-Authentik-Uid": []string{"bar"}, |
| 153 | + "X-Authentik-Username": []string{"foo"}, |
| 154 | + }, h) |
| 155 | +} |
| 156 | + |
| 157 | +func TestAdHeaders_UnderscoreInitial(t *testing.T) { |
| 158 | + a := newTestApplication() |
| 159 | + h := http.Header{} |
| 160 | + h.Set("X_AUTHENTIK_USERNAME", "another user") |
| 161 | + h.Set("X-Authentik_username", "another user") |
| 162 | + a.addHeaders(h, &types.Claims{ |
| 163 | + PreferredUsername: "foo", |
| 164 | + Groups: []string{"foo", "bar"}, |
| 165 | + Entitlements: []string{"bar", "quox"}, |
| 166 | + Email: "bar@authentik.company", |
| 167 | + Name: "foo", |
| 168 | + Sub: "bar", |
| 169 | + RawToken: "baz", |
| 170 | + }) |
| 171 | + assert.Equal(t, http.Header{ |
| 172 | + "X-Authentik-Email": []string{"bar@authentik.company"}, |
| 173 | + "X-Authentik-Entitlements": []string{"bar|quox"}, |
| 174 | + "X-Authentik-Groups": []string{"foo|bar"}, |
| 175 | + "X-Authentik-Jwt": []string{"baz"}, |
| 176 | + "X-Authentik-Meta-App": []string{""}, |
| 177 | + "X-Authentik-Meta-Jwks": []string{""}, |
| 178 | + "X-Authentik-Meta-Outpost": []string{""}, |
| 179 | + "X-Authentik-Meta-Provider": []string{a.proxyConfig.Name}, |
| 180 | + "X-Authentik-Meta-Version": []string{constants.UserAgentOutpost()}, |
| 181 | + "X-Authentik-Name": []string{"foo"}, |
| 182 | + "X-Authentik-Uid": []string{"bar"}, |
| 183 | + "X-Authentik-Username": []string{"foo"}, |
| 184 | + }, h) |
| 185 | +} |
0 commit comments