From 19a555f4160d845fe1696433ee19e37c23393977 Mon Sep 17 00:00:00 2001 From: Simon Gottschlag Date: Mon, 18 Jan 2021 21:59:32 +0100 Subject: [PATCH 1/5] Change to RawStdEncoding to remove error --- auth/claims.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/claims.go b/auth/claims.go index 4a809735..12eafa44 100644 --- a/auth/claims.go +++ b/auth/claims.go @@ -30,7 +30,7 @@ func ParseClaims(token *oauth2.Token) (claims Claims, err error) { return } jwt := strings.Split(token.AccessToken, ".") - payload, err := base64.StdEncoding.DecodeString(jwt[1]) + payload, err := base64.RawStdEncoding.DecodeString(jwt[1]) if err != nil { return } From 40a606fc19c39662cc82ceba4ba735464b2e6278 Mon Sep 17 00:00:00 2001 From: Simon Gottschlag Date: Mon, 18 Jan 2021 21:59:53 +0100 Subject: [PATCH 2/5] fmt --- auth/claims.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/auth/claims.go b/auth/claims.go index 12eafa44..2bc2c455 100644 --- a/auth/claims.go +++ b/auth/claims.go @@ -3,8 +3,9 @@ package auth import ( "encoding/base64" "encoding/json" - "golang.org/x/oauth2" "strings" + + "golang.org/x/oauth2" ) // Claims is used to unmarshall the claims from a JWT issued by the Microsoft Identity Platform. From 1b96535f86871cfa7bdf26179f1d8e50884f22b0 Mon Sep 17 00:00:00 2001 From: Simon Gottschlag Date: Mon, 18 Jan 2021 22:58:26 +0100 Subject: [PATCH 3/5] Add GetMemberGroups for user --- clients/users.go | 28 +++++++++++++++++++++++ clients/users_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/clients/users.go b/clients/users.go index ad94735b..0a2f57a2 100644 --- a/clients/users.go +++ b/clients/users.go @@ -135,3 +135,31 @@ func (c *UsersClient) Delete(ctx context.Context, id string) (int, error) { } return status, nil } + +// GetMemberGroups returns a list of Groups the user is member of, optionally filtered using OData. +func (c *UsersClient) GetMemberGroups(ctx context.Context, id string, filter string) (*[]models.Group, int, error) { + params := url.Values{} + if filter != "" { + params.Add("$filter", filter) + } + resp, status, err := c.BaseClient.Get(ctx, base.GetHttpRequestInput{ + ValidStatusCodes: []int{http.StatusOK}, + Uri: base.Uri{ + Entity: fmt.Sprintf("/users/%s/transitiveMemberOf", id), + Params: params, + HasTenantId: true, + }, + }) + if err != nil { + return nil, status, err + } + defer resp.Body.Close() + respBody, _ := ioutil.ReadAll(resp.Body) + var data struct { + Groups []models.Group `json:"value"` + } + if err := json.Unmarshal(respBody, &data); err != nil { + return nil, status, err + } + return &data.Groups, status, nil +} diff --git a/clients/users_test.go b/clients/users_test.go index 82f35e27..5e5c2f3c 100644 --- a/clients/users_test.go +++ b/clients/users_test.go @@ -16,9 +16,10 @@ type UsersClientTest struct { } func TestUsersClient(t *testing.T) { + rs := internal.RandomString() c := UsersClientTest{ connection: internal.NewConnection(), - randomString: internal.RandomString(), + randomString: rs, } c.client = clients.NewUsersClient(c.connection.AuthConfig.TenantID) c.client.BaseClient.Authorizer = c.connection.Authorizer @@ -36,6 +37,38 @@ func TestUsersClient(t *testing.T) { user.DisplayName = internal.String(fmt.Sprintf("test-updated-user-%s", c.randomString)) testUsersClient_Update(t, c, *user) testUsersClient_List(t, c) + + g := GroupsClientTest{ + connection: internal.NewConnection(), + randomString: rs, + } + g.client = clients.NewGroupsClient(g.connection.AuthConfig.TenantID) + g.client.BaseClient.Authorizer = g.connection.Authorizer + + newGroupParent := models.Group{ + DisplayName: internal.String("Test Group Parent"), + MailEnabled: internal.Bool(false), + MailNickname: internal.String(fmt.Sprintf("test-group-parent-%s", c.randomString)), + SecurityEnabled: internal.Bool(true), + } + newGroupChild := models.Group{ + DisplayName: internal.String("Test Group Child"), + MailEnabled: internal.Bool(false), + MailNickname: internal.String(fmt.Sprintf("test-group-child-%s", c.randomString)), + SecurityEnabled: internal.Bool(true), + } + + groupParent := testGroupsClient_Create(t, g, newGroupParent) + groupChild := testGroupsClient_Create(t, g, newGroupChild) + groupParent.AppendMember(g.client.BaseClient.Endpoint, g.client.BaseClient.ApiVersion, *groupChild.ID) + testGroupsClient_AddMembers(t, g, groupParent) + groupChild.AppendMember(g.client.BaseClient.Endpoint, g.client.BaseClient.ApiVersion, *user.ID) + testGroupsClient_AddMembers(t, g, groupChild) + + testUsersClient_GetMemberGroups(t, c, *user.ID) + testGroupsClient_Delete(t, g, *groupParent.ID) + testGroupsClient_Delete(t, g, *groupChild.ID) + testUsersClient_Delete(t, c, *user.ID) } @@ -100,3 +133,20 @@ func testUsersClient_Delete(t *testing.T, c UsersClientTest, id string) { t.Fatalf("UsersClient.Delete(): invalid status: %d", status) } } + +func testUsersClient_GetMemberGroups(t *testing.T, c UsersClientTest, id string) (groups *[]models.Group) { + groups, _, err := c.client.GetMemberGroups(c.connection.Context, id, "") + if err != nil { + t.Fatalf("UsersClient.GetMemberGroups(): %v", err) + } + + if groups == nil { + t.Fatal("UsersClient.GetMemberGroups(): groups was nil") + } + + if len(*groups) != 2 { + t.Fatalf("UsersClient.GetMemberGroups(): expected groups length 1. was: %d", len(*groups)) + } + + return +} From a2f2e5564a67498b3a78091f17786095367e7673 Mon Sep 17 00:00:00 2001 From: Simon Gottschlag Date: Mon, 18 Jan 2021 23:07:49 +0100 Subject: [PATCH 4/5] fix error message --- clients/users_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/users_test.go b/clients/users_test.go index 5e5c2f3c..22ee6be7 100644 --- a/clients/users_test.go +++ b/clients/users_test.go @@ -145,7 +145,7 @@ func testUsersClient_GetMemberGroups(t *testing.T, c UsersClientTest, id string) } if len(*groups) != 2 { - t.Fatalf("UsersClient.GetMemberGroups(): expected groups length 1. was: %d", len(*groups)) + t.Fatalf("UsersClient.GetMemberGroups(): expected groups length 2. was: %d", len(*groups)) } return From 950bfd0d689b647b0a15887db5541fd42e8729a4 Mon Sep 17 00:00:00 2001 From: Simon Gottschlag Date: Tue, 19 Jan 2021 07:53:20 +0100 Subject: [PATCH 5/5] Change method name from GetMemberGroups to ListGroupMemberships --- clients/users.go | 4 ++-- clients/users_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/clients/users.go b/clients/users.go index 0a2f57a2..0adfb2c8 100644 --- a/clients/users.go +++ b/clients/users.go @@ -136,8 +136,8 @@ func (c *UsersClient) Delete(ctx context.Context, id string) (int, error) { return status, nil } -// GetMemberGroups returns a list of Groups the user is member of, optionally filtered using OData. -func (c *UsersClient) GetMemberGroups(ctx context.Context, id string, filter string) (*[]models.Group, int, error) { +// ListGroupMemberships returns a list of Groups the user is member of, optionally filtered using OData. +func (c *UsersClient) ListGroupMemberships(ctx context.Context, id string, filter string) (*[]models.Group, int, error) { params := url.Values{} if filter != "" { params.Add("$filter", filter) diff --git a/clients/users_test.go b/clients/users_test.go index 22ee6be7..e6fce341 100644 --- a/clients/users_test.go +++ b/clients/users_test.go @@ -65,7 +65,7 @@ func TestUsersClient(t *testing.T) { groupChild.AppendMember(g.client.BaseClient.Endpoint, g.client.BaseClient.ApiVersion, *user.ID) testGroupsClient_AddMembers(t, g, groupChild) - testUsersClient_GetMemberGroups(t, c, *user.ID) + testUsersClient_ListGroupMemberships(t, c, *user.ID) testGroupsClient_Delete(t, g, *groupParent.ID) testGroupsClient_Delete(t, g, *groupChild.ID) @@ -134,18 +134,18 @@ func testUsersClient_Delete(t *testing.T, c UsersClientTest, id string) { } } -func testUsersClient_GetMemberGroups(t *testing.T, c UsersClientTest, id string) (groups *[]models.Group) { - groups, _, err := c.client.GetMemberGroups(c.connection.Context, id, "") +func testUsersClient_ListGroupMemberships(t *testing.T, c UsersClientTest, id string) (groups *[]models.Group) { + groups, _, err := c.client.ListGroupMemberships(c.connection.Context, id, "") if err != nil { - t.Fatalf("UsersClient.GetMemberGroups(): %v", err) + t.Fatalf("UsersClient.ListGroupMemberships(): %v", err) } if groups == nil { - t.Fatal("UsersClient.GetMemberGroups(): groups was nil") + t.Fatal("UsersClient.ListGroupMemberships(): groups was nil") } if len(*groups) != 2 { - t.Fatalf("UsersClient.GetMemberGroups(): expected groups length 2. was: %d", len(*groups)) + t.Fatalf("UsersClient.ListGroupMemberships(): expected groups length 2. was: %d", len(*groups)) } return