Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --orgs #71

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/auth0/pkg/app/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type FetchCmd struct {
UserPID string `name:"user-pid" env:"AUTH0_USER_PID" help:"auth0 user PID of the user you want to read" optional:""`
UserEmail string `name:"user-email" env:"AUTH0_USER_EMAIL" help:"auth0 user email of the user you want to read" optional:""`
Roles bool `name:"roles" env:"AUTH0_ROLES" default:"false" negatable:"" help:"include roles"`
Orgs bool `name:"orgs" env:"AUTH0_ORGS" default:"false" negatable:"" help:"include organizations"`
RateLimit bool `name:"rate-limit" default:"true" help:"enable http client rate limiter" negatable:""`
SAML bool `name:"saml" default:"false" help:"use specialized SAML data loader"`
SAML bool `name:"saml" default:"false" help:"use SAML data loader"`
}

func (f *FetchCmd) Run(ctx *cc.CommonCtx) error {
Expand All @@ -35,7 +36,7 @@ func (f *FetchCmd) Run(ctx *cc.CommonCtx) error {
if err != nil {
return err
}
fetcher = fetcher.WithUserPID(f.UserPID).WithEmail(f.UserEmail).WithRoles(f.Roles).WithSAML(f.SAML)
fetcher = fetcher.WithUserPID(f.UserPID).WithEmail(f.UserEmail).WithRoles(f.Roles).WithOrgs(f.Orgs).WithSAML(f.SAML)

return fetcher.Fetch(ctx.Context, os.Stdout, os.Stderr)
}
69 changes: 68 additions & 1 deletion plugins/auth0/pkg/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Fetcher struct {
UserEmail string
ConnectionName string
Roles bool
Orgs bool
SAML bool
client *auth0client.Auth0Client
}
Expand All @@ -46,6 +47,11 @@ func (f *Fetcher) WithRoles(roles bool) *Fetcher {
return f
}

func (f *Fetcher) WithOrgs(orgs bool) *Fetcher {
f.Orgs = orgs
return f
}

func (f *Fetcher) WithSAML(saml bool) *Fetcher {
f.SAML = saml
return f
Expand Down Expand Up @@ -97,6 +103,16 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer
obj["roles"] = roles
}
}
if f.Orgs {
orgs, err := f.getOrgs(ctx, *user.ID)
if err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
common.SetExitCode(1)
} else {
obj["orgs"] = orgs
}
}

err = writer.Write(obj)
if err != nil {
return err
Expand Down Expand Up @@ -135,14 +151,14 @@ func (f *Fetcher) getUsers(ctx context.Context, opts []management.RequestOption)
return users, false, nil
} else {
// List all users

if !f.SAML {
userList, err := f.client.Mgmt.User.List(ctx, opts...)
if err != nil {
return nil, false, err
}
return userList.Users, userList.HasNext(), nil
} else {
// Use special SAML user list, to avoid known unmarshal errors, see notes below.
ul := &UserList{}
if err := ListUsers(ctx, f.client.Mgmt, &ul, opts...); err != nil {
return nil, false, err
Expand Down Expand Up @@ -189,6 +205,57 @@ func (f *Fetcher) getRoles(ctx context.Context, uID string) ([]map[string]interf
return results, nil
}

func (f *Fetcher) getOrgs(ctx context.Context, uID string) ([]map[string]interface{}, error) {
page := 0
finished := false

var results []map[string]interface{}

for {
if finished {
break
}

reqOpts := management.Page(page)
orgs, err := f.client.Mgmt.User.Organizations(ctx, uID, reqOpts)
if err != nil {
return nil, err
}
for _, org := range orgs.Organizations {
res, err := json.Marshal(org)
if err != nil {
return nil, err
}
var obj map[string]interface{}
err = json.Unmarshal(res, &obj)
if err != nil {
return nil, err
}
results = append(results, obj)
}
if !orgs.HasNext() {
finished = true
}

page++
}

return results, nil
}

// Specialized SAML user list function
//
// The Auth0 golang SDK does not properly handle the unmarshal of the returned payload into a management.UserList.
//
// The returned payload contains:
// "email":"[email protected]",
// "emailVerified":"true",
// "email_verified":"[email protected]"
//
// Which results in an unmarshal error when calling `func (m *UserManager) List(ctx context.Context, opts ...RequestOption) (ul *UserList, err error)`
// resulting in an error `strconv.ParseBool: parsing "[email protected]": invalid syntax`
//
// The implementation below works around the issues by using custom JSON marshaling to map the values into the management.User instances.
type User struct {
management.User
}
Expand Down
Loading