mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 10:07:34 -05:00
fix(graph): return the identities of the identity backend on /me
Without $expand=memberOf, GET /me built the user from the CS3 user of the request context. That user doesn't know the identities the user signed in with, so identities[].issuerAssignedId contained the internal user ID instead of the subject assigned by the identity provider, which the identity backend stores when the user is provisioned. Only requests with $expand=memberOf read the user from the identity backend and returned the correct value. GET /me now always reads the user from the identity backend. The group memberships are still only looked up when $expand=memberOf asks for them, so a plain /me costs one more user lookup in the identity backend. This changes the response of a plain GET /me in two cases: - users for whom the identity backend has no stored identity, e.g. users created through the API rather than signed in through an identity provider, get no identities at all, instead of one that wrongly carried the internal user ID - when the identity backend doesn't know the user, /me answers with its error (e.g. 404 instead of 200 with a model built from the request context) A failing identity backend is now logged at error level, a user it doesn't know at debug level. PERFORMANCE: every plain GET /me now runs one search in the identity backend (LDAP), where it used to need none. Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
3999ed0de8
commit
acb16b538a
2 files changed
+53
-17
No files matched your search
@@ -42,7 +42,6 @@ import (
|
||||
// GetMe implements the Service interface.
|
||||
func (g Graph) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
logger := g.logger.SubloggerWithRequestID(r.Context())
|
||||
logger.Debug().Msg("calling get user in /me")
|
||||
sanitizedPath := strings.TrimPrefix(r.URL.Path, "/graph/v1.0/")
|
||||
|
||||
odataReq, err := godata.ParseRequest(r.Context(), sanitizedPath, r.URL.Query())
|
||||
@@ -66,22 +65,23 @@ func (g Graph) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var me *libregraph.User
|
||||
// We can just return the user from context unless we need to expand the group memberships
|
||||
if !slices.Contains(exp, "memberOf") {
|
||||
me = identity.CreateUserModelFromCS3(u)
|
||||
} else {
|
||||
var err error
|
||||
logger.Debug().Msg("calling get user on backend")
|
||||
me, err = g.identityBackend.GetUser(r.Context(), u.GetId().GetOpaqueId(), odataReq)
|
||||
if err != nil {
|
||||
logger.Debug().Err(err).Interface("user", u).Msg("could not get user from backend")
|
||||
errorcode.RenderError(w, r, err)
|
||||
return
|
||||
}
|
||||
if me.MemberOf == nil {
|
||||
me.MemberOf = []libregraph.Group{}
|
||||
}
|
||||
// The user is always read from the identity backend: the CS3 user of the request
|
||||
// context doesn't know the identities the user signed in with, and a model built
|
||||
// from it would report the internal user ID as identities[].issuerAssignedId
|
||||
// instead of the subject assigned by the identity provider. The group memberships
|
||||
// are only looked up when $expand=memberOf asks for them.
|
||||
me, err := g.identityBackend.GetUser(r.Context(), u.GetId().GetOpaqueId(), odataReq)
|
||||
if err != nil {
|
||||
// a user the backend doesn't know is logged at debug level, a failing backend as error
|
||||
errorEvent(&logger, err).Str(_logKeyUserID, u.GetId().GetOpaqueId()).Msg("could not get user from backend")
|
||||
errorcode.RenderError(w, r, err)
|
||||
return
|
||||
}
|
||||
// An expanded relation without entries is returned as an empty array rather than
|
||||
// left out, so that clients that asked for it can rely on the attribute being there.
|
||||
// The JSON encoding leaves out a nil slice, but keeps an empty one.
|
||||
if slices.Contains(exp, "memberOf") && me.MemberOf == nil {
|
||||
me.MemberOf = []libregraph.Group{}
|
||||
}
|
||||
|
||||
// expand appRoleAssignments if requested
|
||||
|
||||
@@ -40,6 +40,7 @@ import (
|
||||
"github.com/opencloud-eu/opencloud/services/graph/mocks"
|
||||
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
|
||||
"github.com/opencloud-eu/opencloud/services/graph/pkg/config/defaults"
|
||||
"github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode"
|
||||
identitymocks "github.com/opencloud-eu/opencloud/services/graph/pkg/identity/mocks"
|
||||
service "github.com/opencloud-eu/opencloud/services/graph/pkg/service/v0"
|
||||
)
|
||||
@@ -131,6 +132,14 @@ var _ = Describe("Users", func() {
|
||||
})
|
||||
|
||||
It("gets the information", func() {
|
||||
user := &libregraph.User{
|
||||
Id: libregraph.PtrString("user"),
|
||||
Identities: []libregraph.ObjectIdentity{{
|
||||
Issuer: libregraph.PtrString("https://idp.example.org"),
|
||||
IssuerAssignedId: libregraph.PtrString("subject-of-the-idp"),
|
||||
}},
|
||||
}
|
||||
identityBackend.On("GetUser", mock.Anything, "user", mock.Anything).Return(user, nil)
|
||||
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(&settings.GetValueResponse{
|
||||
Value: &settingsmsg.ValueWithIdentifier{
|
||||
@@ -155,6 +164,32 @@ var _ = Describe("Users", func() {
|
||||
svc.GetMe(rr, r)
|
||||
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
|
||||
data, err := io.ReadAll(rr.Body)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
responseUser := &libregraph.User{}
|
||||
err = json.Unmarshal(data, &responseUser)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// the identities come from the identity backend, not from the user ID
|
||||
Expect(responseUser.GetIdentities()).To(HaveLen(1))
|
||||
Expect(responseUser.GetIdentities()[0].GetIssuer()).To(Equal("https://idp.example.org"))
|
||||
Expect(responseUser.GetIdentities()[0].GetIssuerAssignedId()).To(Equal("subject-of-the-idp"))
|
||||
// without $expand=memberOf, the group memberships are left out
|
||||
Expect(responseUser.HasMemberOf()).To(BeFalse())
|
||||
})
|
||||
|
||||
It("renders the error of the identity backend", func() {
|
||||
identityBackend.On("GetUser", mock.Anything, "user", mock.Anything).
|
||||
Return(nil, errorcode.New(errorcode.ItemNotFound, "user not found"))
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/me", nil)
|
||||
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
||||
svc.GetMe(rr, r)
|
||||
|
||||
Expect(rr.Code).To(Equal(http.StatusNotFound))
|
||||
valueService.AssertNotCalled(GinkgoT(), "GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything)
|
||||
})
|
||||
|
||||
It("expands the memberOf", func() {
|
||||
@@ -209,6 +244,7 @@ var _ = Describe("Users", func() {
|
||||
RoleId: "some-appRole-ID",
|
||||
},
|
||||
}
|
||||
identityBackend.On("GetUser", mock.Anything, "user", mock.Anything).Return(&libregraph.User{Id: libregraph.PtrString("user")}, nil)
|
||||
roleService.On("ListRoleAssignments", mock.Anything, mock.Anything, mock.Anything).Return(&settings.ListRoleAssignmentsResponse{Assignments: assignments}, nil)
|
||||
valueService.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(&settings.GetValueResponse{
|
||||
|
||||
Reference in new issue
Block a user