Skip to content

Commit

Permalink
[fix] Only fetch clientIDs assigned to user
Browse files Browse the repository at this point in the history
  • Loading branch information
edulop committed Jun 11, 2020
1 parent abe5abe commit 9520c5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/aws_config_server/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Index(
return
}

clientIDs, err := okta.GetClientIDs(ctx, oktaClient)
clientIDs, err := okta.GetClientIDs(ctx, *email, oktaClient)
if err != nil {
logrus.Errorf("Unable to get list of ClientIDs for %s: %s", *email, err)
http.Error(w, fmt.Sprintf("%v:%s", 500, http.StatusText(500)), 500)
Expand Down
17 changes: 12 additions & 5 deletions pkg/okta/okta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package okta

import (
"context"
"fmt"
"net/url"

"github.com/okta/okta-sdk-golang/v2/okta"
Expand Down Expand Up @@ -31,22 +32,28 @@ func NewOktaClient(ctx context.Context, conf *OktaClientConfig) (*okta.Client, e
return client, errors.Wrap(err, "error creating Okta client")
}

func GetClientIDs(ctx context.Context, oktaClient AppResource) ([]ClientID, error) {
apps, err := paginateListApplications(ctx, oktaClient)
func GetClientIDs(ctx context.Context, userEmail string, oktaClient AppResource) ([]ClientID, error) {
apps, err := paginateListApplications(ctx, userEmail, oktaClient)
if err != nil {
return nil, err
}
return getClientIDsfromApplications(ctx, apps)
}

type AppResource interface {
ListApplications(context.Context, *query.Params) ([]okta.App, *okta.Response, error)
ListApplications(
ctx context.Context,
qp *query.Params,
) ([]okta.App, *okta.Response, error)
}

func paginateListApplications(ctx context.Context, client AppResource) ([]okta.App, error) {
var qp query.Params
func paginateListApplications(ctx context.Context, userEmail string, client AppResource) ([]okta.App, error) {
var apps []okta.App

qp := query.Params{
Filter: fmt.Sprintf("user.email+eq+\"%s\"", userEmail),
}

for {
currentApps, resp, err := client.ListApplications(ctx, &qp)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/okta/okta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestPaginateListApplications(t *testing.T) {
ctx := context.Background()
r := require.New(t)

appInterfaces, err := paginateListApplications(ctx, &oktaApplicationsWithNilResponse{})
appInterfaces, err := paginateListApplications(ctx, "okta user id", &oktaApplicationsWithNilResponse{})
r.NoError(err)
r.Len(appInterfaces, 2)
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestPaginateWithNext(t *testing.T) {
},
},
)
clientIDs, err := GetClientIDs(ctx, oktaApps)
clientIDs, err := GetClientIDs(ctx, "oktaUserID", oktaApps)
r.NoError(err)
r.Equal(clientIDs, []ClientID{"id1", "id2", "id3"})
}

0 comments on commit 9520c5f

Please sign in to comment.