Debian Codegraph @

internal/oauth2/provider_test.go

raw
1 package oauth2_test
2
3 import (
4 "context"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9
10 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config"
11 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
12 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2"
13 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/generic"
14 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/github"
15 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/google"
16 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testlogger"
17 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
20 "golang.org/x/net/nettest"
21 )
22
23 func TestNewProvider(t *testing.T) {
24 t.Parallel()
25
26 clientListener, err := nettest.NewLocalListener("tcp")
27 require.NoError(t, err)
28
29 conf := config.Config{}
30 _, resourceServerURL, clientCredentials := testsuite.New(&conf).SetupOIDCServer(t, clientListener, nil)
31
32 tests := []struct {
33 name string
34 conf config.Config
35 err string
36 }{
37 {
38 "default",
39 config.Config{
40 HTTP: config.HTTP{BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}},
41 OAuth2: config.OAuth2{
42 Issuer: resourceServerURL,
43 Provider: generic.Name,
44 Client: clientCredentials,
45 Endpoints: config.OAuth2Endpoints{},
46 },
47 },
48 "",
49 },
50 {
51 "with custom discovery",
52 config.Config{
53 HTTP: config.HTTP{BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}},
54 OAuth2: config.OAuth2{
55 Issuer: resourceServerURL,
56 Provider: generic.Name,
57 Client: clientCredentials,
58 Endpoints: config.OAuth2Endpoints{
59 Discovery: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/.well-known/openid-configuration"}},
60 },
61 },
62 },
63 "",
64 },
65 {
66 "with invalid custom discovery",
67 config.Config{
68 HTTP: config.HTTP{BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}},
69 OAuth2: config.OAuth2{
70 Issuer: resourceServerURL,
71 Provider: generic.Name,
72 Client: clientCredentials,
73 Endpoints: config.OAuth2Endpoints{
74 Discovery: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/.well-known/openid-config"}},
75 },
76 },
77 },
78 "error OIDC provider: OpenID Provider Configuration Discovery has failed\nhttp status not ok: 404 Not Found 404 page not found",
79 },
80 {
81 "with custom endpoints",
82 config.Config{
83 HTTP: config.HTTP{BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}},
84 OAuth2: config.OAuth2{
85 Issuer: resourceServerURL,
86 Provider: generic.Name,
87 Client: clientCredentials,
88 Endpoints: config.OAuth2Endpoints{
89 Discovery: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/.well-known/openid-configuration"}},
90 Auth: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/authorize"}},
91 Token: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/token"}},
92 },
93 },
94 },
95 "",
96 },
97 {
98 name: "with missing custom endpoints",
99 conf: config.Config{
100 HTTP: config.HTTP{
101 BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}},
102 },
103 OAuth2: config.OAuth2{
104 Issuer: resourceServerURL,
105 Provider: generic.Name,
106 Client: clientCredentials,
107 Endpoints: config.OAuth2Endpoints{
108 Discovery: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/.well-known/openid-configuration"}},
109 Auth: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/authorize"}},
110 },
111 },
112 },
113 err: "error fetch configuration for provider generic: both oauth2.endpoints.tokenUrl and oauth2.endpoints.authUrl are required",
114 },
115 {
116 name: "with pkce",
117 conf: config.Config{
118 HTTP: config.HTTP{BaseURL: types.URL{URL: &url.URL{Scheme: "http", Host: "localhost"}}},
119 OAuth2: config.OAuth2{
120 Issuer: resourceServerURL,
121 Provider: generic.Name,
122 PKCE: true,
123 Client: clientCredentials,
124 Endpoints: config.OAuth2Endpoints{
125 Discovery: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/.well-known/openid-configuration"}},
126 Auth: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/authorize"}},
127 Token: types.URL{URL: &url.URL{Scheme: resourceServerURL.Scheme, Host: resourceServerURL.Host, Path: "/token"}},
128 },
129 },
130 },
131 },
132 }
133 for _, tc := range tests {
134 t.Run(tc.name, func(t *testing.T) {
135 t.Parallel()
136
137 ctx, cancel := context.WithCancel(t.Context())
138 t.Cleanup(cancel)
139
140 logger := testlogger.New()
141
142 var (
143 err error
144 provider oauth2.Provider
145 )
146
147 switch tc.conf.OAuth2.Provider {
148 case generic.Name:
149 provider, err = generic.NewProvider(ctx, &tc.conf, http.DefaultClient)
150 case github.Name:
151 provider, err = github.NewProvider(ctx, &tc.conf, http.DefaultClient)
152 case google.Name:
153 provider, err = google.NewProvider(ctx, &tc.conf, http.DefaultClient)
154 default:
155 t.Fatal("unknown oauth2 provider: " + tc.conf.OAuth2.Provider)
156 }
157
158 require.NoError(t, err)
159
160 oAuth2Client, err := oauth2.New(
161 ctx,
162 logger.Logger(),
163 &tc.conf,
164 http.DefaultClient,
165 testsuite.NewFakeStorage(),
166 testsuite.NewCipher(t),
167 provider,
168 testsuite.NewFakeOpenVPNClient(),
169 )
170 if tc.err != "" {
171 require.Error(t, err)
172 assert.Equal(t, tc.err, strings.TrimSpace(err.Error()))
173
174 return
175 }
176
177 assert.Equal(t, oAuth2Client.OAuthConfig().ClientID, tc.conf.OAuth2.Client.ID)
178 assert.Equal(t, oAuth2Client.OAuthConfig().ClientSecret, tc.conf.OAuth2.Client.Secret.String())
179
180 if !tc.conf.OAuth2.Endpoints.Auth.IsEmpty() {
181 assert.Equal(t, oAuth2Client.OAuthConfig().Endpoint.AuthURL, tc.conf.OAuth2.Endpoints.Auth.String())
182 } else {
183 assert.NotEmpty(t, oAuth2Client.OAuthConfig().Endpoint.AuthURL)
184 }
185
186 if !tc.conf.OAuth2.Endpoints.Token.IsEmpty() {
187 assert.Equal(t, oAuth2Client.OAuthConfig().Endpoint.TokenURL, tc.conf.OAuth2.Endpoints.Token.String())
188 } else {
189 assert.NotEmpty(t, oAuth2Client.OAuthConfig().Endpoint.TokenURL)
190 }
191 })
192 }
193 }