Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oidc: add UserInfoEndpoint returning the discocvered URL #375

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ func (p *Provider) Endpoint() oauth2.Endpoint {
return oauth2.Endpoint{AuthURL: p.authURL, TokenURL: p.tokenURL}
}

// UserInfoEndpoint returns the OpenID Connect userinfo endpoint for the given
// provider.
func (p *Provider) UserInfoEndpoint() string {
return p.userInfoURL
}

// UserInfo represents the OpenID Connect userinfo claims.
type UserInfo struct {
Subject string `json:"sub"`
Expand Down
45 changes: 36 additions & 9 deletions oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,19 @@ func (ts *testServer) run(t *testing.T) string {
]
}`

var userInfoJSON string
if ts.userInfo != "" {
userInfoJSON = fmt.Sprintf(`"userinfo_endpoint": "%s/userinfo",`, server.URL)
}

wellKnown := fmt.Sprintf(`{
"issuer": "%[1]s",
"authorization_endpoint": "%[1]s/auth",
"token_endpoint": "%[1]s/token",
"jwks_uri": "%[1]s/keys",
"userinfo_endpoint": "%[1]s/userinfo",
%[2]s
"id_token_signing_alg_values_supported": ["RS256"]
}`, server.URL)
}`, server.URL, userInfoJSON)

newMux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, req *http.Request) {
_, err := io.WriteString(w, wellKnown)
Expand All @@ -383,13 +388,15 @@ func (ts *testServer) run(t *testing.T) string {
w.WriteHeader(500)
}
})
newMux.HandleFunc("/userinfo", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", ts.contentType)
_, err := io.WriteString(w, ts.userInfo)
if err != nil {
w.WriteHeader(500)
}
})
if ts.userInfo != "" {
newMux.HandleFunc("/userinfo", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", ts.contentType)
_, err := io.WriteString(w, ts.userInfo)
if err != nil {
w.WriteHeader(500)
}
})
}
t.Cleanup(server.Close)
return server.URL
}
Expand Down Expand Up @@ -489,6 +496,13 @@ func TestUserInfoEndpoint(t *testing.T) {
claims: []byte(userInfoJSONCognitoVariant),
},
},
{
name: "no userinfo endpoint",
server: testServer{
contentType: "application/json",
userInfo: "",
},
},
}

for _, test := range tests {
Expand All @@ -502,6 +516,19 @@ func TestUserInfoEndpoint(t *testing.T) {
t.Fatalf("Failed to initialize provider for test %v", err)
}

if test.server.userInfo == "" {
if provider.UserInfoEndpoint() != "" {
t.Errorf("expected UserInfoEndpoint to be empty, got %v", provider.UserInfoEndpoint())
}

// provider.UserInfo will error.
return
}

if provider.UserInfoEndpoint() != serverURL+"/userinfo" {
t.Errorf("expected UserInfoEndpoint to be %v , got %v", serverURL+"/userinfo", provider.UserInfoEndpoint())
}

fakeOauthToken := oauth2.Token{}
info, err := provider.UserInfo(ctx, oauth2.StaticTokenSource(&fakeOauthToken))
if err != nil {
Expand Down