Nerzal / gocloak

golang keycloak client
Apache License 2.0
1.01k stars 275 forks source link

UpdateUser & UpdateClient doesn't work at all. #444

Closed justmike1 closed 8 months ago

justmike1 commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

Cant use the UpdateUser & UpdateClient methods.

To Reproduce Steps to reproduce the behavior:

func createOrUpdateSupersetUser(ctx context.Context, client *gocloak.GoCloak, token *gocloak.JWT) error {
    supersetUserConfig := gocloak.User{
        Username:  gocloak.StringP(supersetUsername),
        Enabled:   gocloak.BoolP(true),
        FirstName: gocloak.StringP("Superset"),
        LastName:  gocloak.StringP("Admin"),
        Email:     gocloak.StringP(supersetUsername),
        Credentials: &[]gocloak.CredentialRepresentation{
            {
                Type:      gocloak.StringP("password"),
                Value:     gocloak.StringP(supersetPassword),
                Temporary: gocloak.BoolP(false),
            },
        },
    }
    supersetUser, err := client.GetUsers(ctx, token.AccessToken, velotixRealm, gocloak.GetUsersParams{
        Username: gocloak.StringP(supersetUsername),
    })
    if err != nil {
        return err
    }
    if supersetUser != nil && len(supersetUser) > 0 {
        log.Info("Updating superset user...")
        err = client.UpdateUser(ctx, token.AccessToken, velotixRealm, supersetUserConfig)
        if err != nil {
            return err
        }
    } else {
        log.Info("Creating superset user...")
        _, err = client.CreateUser(ctx, token.AccessToken, velotixRealm, supersetUserConfig)
        if err != nil {
            return err
        }
    }

    return err
}
const (
    supersetUsername = "test@user"
    supersetPassword = "test"
)
    err = createOrUpdateSupersetUser(ctx, client, token)
    if err != nil {
        log.Panic(err)
    }

Expected behavior A clear and concise description of what you expected to happen.

200 status code response of updated user.

Screenshots If applicable, add screenshots to help explain your problem.

image

Additional context Add any other context about the problem here.

gocloak version: 13.8 keycloak version: 22.0.1-2

burner-account commented 12 months ago

Well, this cannot work like you seem to want to:

        err = client.UpdateUser(ctx, token.AccessToken, velotixRealm, supersetUserConfig)
        if err != nil {
            return err
        }

Your supersetUserConfig lacks any kind of identifying property (subject ID). Do something like:

  obj = <client>.fetch user by subject ID
  obj.property = your update value

  err = <client>.UpdateUser( ..., obj)

Attention: This is a PUT not a PATCH. It will not be enough to transmit the update-delta - you need to send it all.

justmike1 commented 8 months ago

worked, thank you!