diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index 5575db07b3..e66dfc2e63 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -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 diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index 55aac117db..42fa7ff5ea 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -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{