| 1 |
package utils_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"net/http" |
| 6 |
"net/http/httptest" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/utils" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewUserAgentTransport(t *testing.T) { |
| 16 |
t.Parallel() |
| 17 |
|
| 18 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 |
_, _ = w.Write([]byte(r.UserAgent())) |
| 20 |
})) |
| 21 |
|
| 22 |
server.Client().Transport = utils.NewUserAgentTransport(nil) |
| 23 |
|
| 24 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, server.URL, nil) |
| 25 |
|
| 26 |
require.NoError(t, err) |
| 27 |
|
| 28 |
resp, err := server.Client().Do(req) |
| 29 |
require.NoError(t, err) |
| 30 |
|
| 31 |
t.Cleanup(func() { |
| 32 |
require.NoError(t, resp.Body.Close()) |
| 33 |
}) |
| 34 |
|
| 35 |
body, err := io.ReadAll(resp.Body) |
| 36 |
require.NoError(t, err) |
| 37 |
|
| 38 |
assert.Equal(t, "openvpn-auth-oauth2", string(body)) |
| 39 |
} |
| 40 |
|
| 41 |
func TestNewOAuth2HTTPClient(t *testing.T) { |
| 42 |
t.Parallel() |
| 43 |
|
| 44 |
client := utils.NewOAuth2HTTPClient(nil) |
| 45 |
|
| 46 |
require.NotNil(t, client.Transport) |
| 47 |
require.NotNil(t, client.CheckRedirect) |
| 48 |
assert.Equal(t, 30*time.Second, client.Timeout) |
| 49 |
} |
| 50 |
|
| 51 |
func TestCheckOAuth2ProviderRedirect(t *testing.T) { |
| 52 |
t.Parallel() |
| 53 |
|
| 54 |
for _, tc := range []struct { |
| 55 |
name string |
| 56 |
originalURL string |
| 57 |
redirectURL string |
| 58 |
expectedErr error |
| 59 |
}{ |
| 60 |
{ |
| 61 |
name: "same host", |
| 62 |
originalURL: "https://provider.example.com/token", |
| 63 |
redirectURL: "https://provider.example.com/token-v2", |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "http to https same host", |
| 67 |
originalURL: "http://provider.example.com/token", |
| 68 |
redirectURL: "https://provider.example.com/token", |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "cross host", |
| 72 |
originalURL: "https://provider.example.com/token", |
| 73 |
redirectURL: "https://metadata.internal/token", |
| 74 |
expectedErr: utils.ErrOAuth2ProviderRedirectHost, |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "subdomain", |
| 78 |
originalURL: "https://provider.example.com/token", |
| 79 |
redirectURL: "https://attacker.provider.example.com/token", |
| 80 |
expectedErr: utils.ErrOAuth2ProviderRedirectHost, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "https downgrade", |
| 84 |
originalURL: "https://provider.example.com/token", |
| 85 |
redirectURL: "http://provider.example.com/token", |
| 86 |
expectedErr: utils.ErrOAuth2ProviderRedirectDowngrade, |
| 87 |
}, |
| 88 |
} { |
| 89 |
t.Run(tc.name, func(t *testing.T) { |
| 90 |
t.Parallel() |
| 91 |
|
| 92 |
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, tc.redirectURL, nil) |
| 93 |
via := []*http.Request{httptest.NewRequestWithContext(t.Context(), http.MethodGet, tc.originalURL, nil)} |
| 94 |
|
| 95 |
err := utils.CheckOAuth2ProviderRedirect(req, via) |
| 96 |
|
| 97 |
if tc.expectedErr == nil { |
| 98 |
require.NoError(t, err) |
| 99 |
|
| 100 |
return |
| 101 |
} |
| 102 |
|
| 103 |
require.Error(t, err) |
| 104 |
assert.ErrorIs(t, err, tc.expectedErr) |
| 105 |
}) |
| 106 |
} |
| 107 |
} |