Skip to content

Commit 2f976a1

Browse files
committed
Fix for missing client config
1 parent 1e43230 commit 2f976a1

File tree

3 files changed

+95
-12
lines changed

3 files changed

+95
-12
lines changed

components/iam/pkg/oidc/router.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func (oidcService *OIDCService) getStartHandler() http.HandlerFunc {
4444
log.Trace("at start handler")
4545

4646
ctx := r.Context()
47-
config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig)
47+
config, ok := ctx.Value(keyOIDCClientConfig{}).(*OIDCClientConfig)
4848
if !ok {
4949
http.Error(rw, "config not found", http.StatusInternalServerError)
5050
return
5151
}
5252

53-
startParams, err := oidcService.GetStartParams(&config)
53+
startParams, err := oidcService.GetStartParams(config)
5454
if err != nil {
5555
http.Error(rw, "failed to start auth flow", http.StatusInternalServerError)
5656
return
@@ -99,7 +99,7 @@ func (oidcService *OIDCService) getCallbackHandler() http.HandlerFunc {
9999
log.Trace("at callback handler")
100100

101101
ctx := r.Context()
102-
config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig)
102+
config, ok := ctx.Value(keyOIDCClientConfig{}).(*OIDCClientConfig)
103103
if !ok {
104104
http.Error(rw, "config not found", http.StatusInternalServerError)
105105
return
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package oidc
6+
7+
import (
8+
"io/ioutil"
9+
"net/http"
10+
"net/http/httptest"
11+
"testing"
12+
"time"
13+
14+
"github.com/go-chi/chi/v5"
15+
"github.com/sirupsen/logrus"
16+
"github.com/stretchr/testify/require"
17+
"golang.org/x/oauth2"
18+
19+
goidc "github.com/coreos/go-oidc/v3/oidc"
20+
21+
"github.com/gitpod-io/gitpod/common-go/log"
22+
)
23+
24+
func TestRoute_start(t *testing.T) {
25+
log.Log.Logger.SetLevel(logrus.TraceLevel)
26+
27+
idpUrl := newFakeIdP(t)
28+
baseUrl := newTestServer(t, idpUrl)
29+
30+
client := &http.Client{Timeout: 10 * time.Second}
31+
resp, err := client.Get(baseUrl + "/oidc/start?issuer=" + idpUrl)
32+
if err != nil {
33+
t.Error(err)
34+
t.FailNow()
35+
}
36+
defer resp.Body.Close()
37+
38+
body, err := ioutil.ReadAll(resp.Body)
39+
if err != nil {
40+
t.Error(err)
41+
t.FailNow()
42+
}
43+
t.Log(string(body))
44+
require.Equal(t, 200, resp.StatusCode)
45+
require.NotEqual(t, "config not found", string(body))
46+
}
47+
48+
func newTestServer(t *testing.T, issuer string) string {
49+
oidcService := NewOIDCService()
50+
oidcConfig := &goidc.Config{
51+
ClientID: "123",
52+
}
53+
oauth2Config := &oauth2.Config{
54+
ClientID: "123",
55+
ClientSecret: "secret",
56+
RedirectURL: "http://localhost/callback",
57+
Scopes: []string{goidc.ScopeOpenID, "profile", "email"},
58+
}
59+
clientConfig := &OIDCClientConfig{
60+
Issuer: issuer,
61+
ID: "R4ND0M1D",
62+
OAuth2Config: oauth2Config,
63+
OIDCConfig: oidcConfig,
64+
}
65+
err := oidcService.AddClientConfig(clientConfig)
66+
if err != nil {
67+
t.Error(err)
68+
t.FailNow()
69+
}
70+
71+
router := chi.NewRouter()
72+
router.Mount("/oidc", Router(oidcService))
73+
74+
ts := httptest.NewServer(router)
75+
url := ts.URL
76+
77+
return url
78+
}

components/iam/pkg/oidc/service_test.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func TestGetStartParams(t *testing.T) {
5757
}
5858

5959
func TestGetClientConfigFromRequest(t *testing.T) {
60-
issuer, err := setupFakeIdP(t)
61-
require.NoError(t, err)
60+
issuer := newFakeIdP(t)
6261

6362
testCases := []struct {
6463
Location string
@@ -83,7 +82,7 @@ func TestGetClientConfigFromRequest(t *testing.T) {
8382
}
8483

8584
service := NewOIDCService()
86-
err = service.AddClientConfig(&OIDCClientConfig{
85+
err := service.AddClientConfig(&OIDCClientConfig{
8786
ID: "google-1",
8887
Issuer: issuer,
8988
OIDCConfig: &oidc.Config{},
@@ -108,11 +107,10 @@ func TestGetClientConfigFromRequest(t *testing.T) {
108107
}
109108

110109
func TestAuthenticate_nonce_check(t *testing.T) {
111-
issuer, err := setupFakeIdP(t)
112-
require.NoError(t, err)
110+
issuer := newFakeIdP(t)
113111

114112
service := NewOIDCService()
115-
err = service.AddClientConfig(&OIDCClientConfig{
113+
err := service.AddClientConfig(&OIDCClientConfig{
116114
ID: "google-1",
117115
Issuer: issuer,
118116
OIDCConfig: &oidc.Config{
@@ -141,7 +139,7 @@ func TestAuthenticate_nonce_check(t *testing.T) {
141139
require.NotNil(t, result)
142140
}
143141

144-
func setupFakeIdP(t *testing.T) (string, error) {
142+
func newFakeIdP(t *testing.T) string {
145143
router := chi.NewRouter()
146144
ts := httptest.NewServer(router)
147145
url := ts.URL
@@ -156,6 +154,12 @@ func setupFakeIdP(t *testing.T) (string, error) {
156154
log.Fatal(err)
157155
}
158156
})
157+
router.Get("/o/oauth2/v2/auth", func(w http.ResponseWriter, r *http.Request) {
158+
_, err := w.Write([]byte(r.URL.RawQuery))
159+
if err != nil {
160+
log.Fatal(err)
161+
}
162+
})
159163
router.Get("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
160164
_, err := w.Write([]byte(fmt.Sprintf(`{
161165
"issuer": "%[1]s",
@@ -216,10 +220,11 @@ func setupFakeIdP(t *testing.T) (string, error) {
216220
]
217221
}`, url)))
218222
if err != nil {
219-
log.Fatal(err)
223+
t.Error((err))
224+
t.FailNow()
220225
}
221226
})
222227

223228
t.Cleanup(ts.Close)
224-
return url, nil
229+
return url
225230
}

0 commit comments

Comments
 (0)