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 public error type for missing user info endpoint #374

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const (
)

var (
errNoAtHash = errors.New("id token did not have an access token hash")
errInvalidAtHash = errors.New("access token hash does not match value in ID token")
errNoAtHash = errors.New("id token did not have an access token hash")
errInvalidAtHash = errors.New("access token hash does not match value in ID token")
ErrUserInfoNotSupported = errors.New("oidc: user info endpoint is not supported by this provider")
)

type contextKey int
Expand Down Expand Up @@ -306,7 +307,7 @@ func (u *UserInfo) Claims(v interface{}) error {
// UserInfo uses the token source to query the provider's user info endpoint.
func (p *Provider) UserInfo(ctx context.Context, tokenSource oauth2.TokenSource) (*UserInfo, error) {
if p.userInfoURL == "" {
return nil, errors.New("oidc: user info endpoint is not supported by this provider")
return nil, ErrUserInfoNotSupported
}

req, err := http.NewRequest("GET", p.userInfoURL, nil)
Expand Down
42 changes: 31 additions & 11 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 All @@ -415,6 +422,7 @@ func TestUserInfoEndpoint(t *testing.T) {
name string
server testServer
wantUserInfo UserInfo
wantError error
}{
{
name: "basic json userinfo",
Expand Down Expand Up @@ -489,6 +497,14 @@ func TestUserInfoEndpoint(t *testing.T) {
claims: []byte(userInfoJSONCognitoVariant),
},
},
{
name: "no userinfo endpoint",
server: testServer{
contentType: "application/json",
userInfo: "",
},
wantError: ErrUserInfoNotSupported,
},
}

for _, test := range tests {
Expand All @@ -504,8 +520,12 @@ func TestUserInfoEndpoint(t *testing.T) {

fakeOauthToken := oauth2.Token{}
info, err := provider.UserInfo(ctx, oauth2.StaticTokenSource(&fakeOauthToken))
if err != nil {
t.Fatalf("failed to get userinfo %v", err)
if err != test.wantError {
t.Fatalf("expected UserInfo err %v got %v", test.wantError, err)
}

if test.wantError != nil {
return
}

if info.Email != test.wantUserInfo.Email {
Expand Down