Skip to content

Commit 58b7341

Browse files
committed
wip
1 parent b088b35 commit 58b7341

File tree

3 files changed

+113
-19
lines changed

3 files changed

+113
-19
lines changed

components/iam/pkg/oidc/router.go

+2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ func (oidcService *OIDCService) clientConfigMiddleware() func(http.Handler) http
8282

8383
config, err := oidcService.GetClientConfigFromRequest(r)
8484
if err != nil {
85+
log.Warn("client config not found: " + err.Error())
8586
http.Error(rw, "config not found", http.StatusNotFound)
87+
return
8688
}
8789

8890
ctx := context.WithValue(r.Context(), keyOIDCClientConfig{}, config)

components/iam/pkg/oidc/service.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/url"
1515

1616
"github.com/coreos/go-oidc/v3/oidc"
17+
"github.com/gitpod-io/gitpod/common-go/log"
1718
"github.com/go-chi/chi/v5"
1819
"golang.org/x/oauth2"
1920
)
@@ -106,6 +107,7 @@ func (service *OIDCService) GetClientConfigFromRequest(r *http.Request) (*OIDCCl
106107
if err != nil {
107108
return nil, errors.New("bad issuer param")
108109
}
110+
log.WithField("issuer", issuer).Trace("at GetClientConfigFromRequest")
109111

110112
for _, value := range service.configsById {
111113
if value.Issuer == issuer {

components/iam/pkg/oidc/service_test.go

+109-19
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ package oidc
66

77
import (
88
"context"
9+
"fmt"
10+
"log"
911
"net/http"
1012
"net/http/httptest"
13+
"net/url"
1114
"testing"
1215

1316
"github.com/coreos/go-oidc/v3/oidc"
17+
"github.com/go-chi/chi/v5"
18+
"github.com/go-chi/chi/v5/middleware"
1419
"github.com/stretchr/testify/require"
1520
"golang.org/x/oauth2"
1621
)
@@ -53,9 +58,9 @@ func TestGetStartParams(t *testing.T) {
5358
}
5459

5560
func TestGetClientConfigFromRequest(t *testing.T) {
56-
const (
57-
issuerG = "https://accounts.google.com"
58-
)
61+
issuer, err := setupFakeIdP(t)
62+
require.NoError(t, err)
63+
5964
testCases := []struct {
6065
Location string
6166
ExpectedError bool
@@ -67,7 +72,7 @@ func TestGetClientConfigFromRequest(t *testing.T) {
6772
ExpectedId: "",
6873
},
6974
{
70-
Location: "/start?issuer=https%3A%2F%2Faccounts.google.com",
75+
Location: "/start?issuer=" + url.QueryEscape(issuer),
7176
ExpectedError: false,
7277
ExpectedId: "google-1",
7378
},
@@ -79,9 +84,9 @@ func TestGetClientConfigFromRequest(t *testing.T) {
7984
}
8085

8186
service := NewOIDCService()
82-
err := service.AddClientConfig(&OIDCClientConfig{
87+
err = service.AddClientConfig(&OIDCClientConfig{
8388
ID: "google-1",
84-
Issuer: issuerG,
89+
Issuer: issuer,
8590
OIDCConfig: &oidc.Config{},
8691
OAuth2Config: &oauth2.Config{},
8792
})
@@ -103,34 +108,119 @@ func TestGetClientConfigFromRequest(t *testing.T) {
103108
}
104109
}
105110

106-
func TestAuthenticate(t *testing.T) {
107-
t.Skip() //
108-
const (
109-
issuerG = "https://accounts.google.com"
110-
)
111+
func TestAuthenticate_nonce_check(t *testing.T) {
112+
issuer, err := setupFakeIdP(t)
113+
require.NoError(t, err)
114+
111115
service := NewOIDCService()
112-
err := service.AddClientConfig(&OIDCClientConfig{
116+
err = service.AddClientConfig(&OIDCClientConfig{
113117
ID: "google-1",
114-
Issuer: issuerG,
118+
Issuer: issuer,
115119
OIDCConfig: &oidc.Config{
116-
SkipClientIDCheck: true,
120+
SkipClientIDCheck: true,
121+
SkipIssuerCheck: true,
122+
SkipExpiryCheck: true,
123+
InsecureSkipSignatureCheck: true,
117124
},
118125
OAuth2Config: &oauth2.Config{},
119126
})
120127
require.NoError(t, err, "failed to initialize test")
121128

122129
token := oauth2.Token{}
130+
rawIDToken := `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkN1bXF1YXQgRG9wdGlnIiwibm9uY2UiOiIxMTEiLCJpYXQiOjE1MTYyMzkwMjJ9.NfbRZns-Sefhw6MT4ULWMj_7bX0vScklaZA2ObCYkStYlo2SvNu5Be79-5Lwcy4GY95vY_dFvLIKrZjfqv_duURSKLUbtH8VxskhcrW4sPAK2R5lzz62a6d_OnVydjNJRZf754TQZILAzMm81tEDNAJSDQjaTFl7t8Bp0iYapNyyH9ZoBrGAPaZkXHYoq4lNH88gCZj5JMRIbrZbsvhOuR3CAzbAMplOmKIWxhFVnHdm6aq6HRjz0ra6Y7yh0R9jEF3vWl2w5D3aN4XESPNBbyB3CHKQ5TG0WkbgdUpv1wwzbPfz4aFHOt--qLy7ZK0TOrS-A7YLFFsJKtoPGRjAPA`
123131
extra := map[string]interface{}{
124-
"id_token": "foo123",
132+
"id_token": rawIDToken,
125133
}
126134

127-
ctx := context.Background()
128-
nonceCookieValue := "foobar123"
129-
result, err := service.Authenticate(ctx, &OAuth2Result{
135+
nonceCookieValue := "111"
136+
oauth2Result := &OAuth2Result{
130137
OAuth2Token: token.WithExtra(extra),
131-
}, issuerG, nonceCookieValue)
138+
}
139+
result, err := service.Authenticate(context.Background(), oauth2Result, issuer, nonceCookieValue)
132140

133141
require.NoError(t, err, "failed to authenticate")
134142
require.NotNil(t, result)
143+
}
144+
145+
func setupFakeIdP(t *testing.T) (string, error) {
146+
router := chi.NewRouter()
147+
ts := httptest.NewServer(router)
148+
url := ts.URL
149+
150+
router.Use(middleware.Logger)
151+
router.Get("/oauth2/v3/certs", func(w http.ResponseWriter, r *http.Request) {
152+
_, err := w.Write([]byte(`{
153+
"keys": [
154+
]
155+
}`))
156+
if err != nil {
157+
log.Fatal(err)
158+
}
159+
})
160+
router.Get("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
161+
_, err := w.Write([]byte(fmt.Sprintf(`{
162+
"issuer": "%[1]s",
163+
"authorization_endpoint": "%[1]s/o/oauth2/v2/auth",
164+
"device_authorization_endpoint": "%[1]s/device/code",
165+
"token_endpoint": "%[1]s/token",
166+
"userinfo_endpoint": "%[1]s/v1/userinfo",
167+
"revocation_endpoint": "%[1]s/revoke",
168+
"jwks_uri": "%[1]s/oauth2/v3/certs",
169+
"response_types_supported": [
170+
"code",
171+
"token",
172+
"id_token",
173+
"code token",
174+
"code id_token",
175+
"token id_token",
176+
"code token id_token",
177+
"none"
178+
],
179+
"subject_types_supported": [
180+
"public"
181+
],
182+
"id_token_signing_alg_values_supported": [
183+
"RS256"
184+
],
185+
"scopes_supported": [
186+
"openid",
187+
"email",
188+
"profile"
189+
],
190+
"token_endpoint_auth_methods_supported": [
191+
"client_secret_post",
192+
"client_secret_basic"
193+
],
194+
"claims_supported": [
195+
"aud",
196+
"email",
197+
"email_verified",
198+
"exp",
199+
"family_name",
200+
"given_name",
201+
"iat",
202+
"iss",
203+
"locale",
204+
"name",
205+
"picture",
206+
"sub"
207+
],
208+
"code_challenge_methods_supported": [
209+
"plain",
210+
"S256"
211+
],
212+
"grant_types_supported": [
213+
"authorization_code",
214+
"refresh_token",
215+
"urn:ietf:params:oauth:grant-type:device_code",
216+
"urn:ietf:params:oauth:grant-type:jwt-bearer"
217+
]
218+
}`, url)))
219+
if err != nil {
220+
log.Fatal(err)
221+
}
222+
})
135223

224+
t.Cleanup(ts.Close)
225+
return url, nil
136226
}

0 commit comments

Comments
 (0)