Skip to content

Commit

Permalink
Add User & Groups to Userinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Meves committed Oct 19, 2020
1 parent 420a34f commit 1b23b73
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions oauthproxy.go
Original file line number Diff line number Diff line change
@@ -750,13 +750,19 @@ func (p *OAuthProxy) UserInfo(rw http.ResponseWriter, req *http.Request) {
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}

userInfo := struct {
Email string `json:"email"`
PreferredUsername string `json:"preferredUsername,omitempty"`
User string `json:"user"`
Email string `json:"email"`
Groups []string `json:"groups,omitempty"`
PreferredUsername string `json:"preferredUsername,omitempty"`
}{
User: session.User,
Email: session.Email,
Groups: session.Groups,
PreferredUsername: session.PreferredUsername,
}

rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
err = json.NewEncoder(rw).Encode(userInfo)
8 changes: 6 additions & 2 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
@@ -1267,14 +1267,18 @@ func TestUserInfoEndpointAccepted(t *testing.T) {
}

startSession := &sessions.SessionState{
Email: "john.doe@example.com", AccessToken: "my_access_token"}
User: "john.doe",
Email: "john.doe@example.com",
Groups: []string{"example", "groups"},
AccessToken: "my_access_token",
}
err = test.SaveSession(startSession)
assert.NoError(t, err)

test.proxy.ServeHTTP(test.rw, test.req)
assert.Equal(t, http.StatusOK, test.rw.Code)
bodyBytes, _ := ioutil.ReadAll(test.rw.Body)
assert.Equal(t, "{\"email\":\"john.doe@example.com\"}\n", string(bodyBytes))
assert.Equal(t, "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"]}\n", string(bodyBytes))
}

func TestUserInfoEndpointUnauthorizedOnNoCookieSetError(t *testing.T) {

0 comments on commit 1b23b73

Please sign in to comment.