|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/auth0/go-auth0/management" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | + |
| 14 | + "github.com/auth0/terraform-provider-auth0/internal/config" |
| 15 | +) |
| 16 | + |
| 17 | +// NewClientsDataSource will return a new auth0_clients data source. |
| 18 | +func NewClientsDataSource() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + ReadContext: readClientsForDataSource, |
| 21 | + Description: "Data source to retrieve a list of Auth0 application clients with optional filtering.", |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name_filter": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Description: "Filter clients by name (partial matches supported).", |
| 27 | + }, |
| 28 | + "app_types": { |
| 29 | + Type: schema.TypeSet, |
| 30 | + Optional: true, |
| 31 | + Description: "Filter clients by application types.", |
| 32 | + Elem: &schema.Schema{ |
| 33 | + Type: schema.TypeString, |
| 34 | + ValidateFunc: validation.StringInSlice(ValidAppTypes, false), |
| 35 | + }, |
| 36 | + }, |
| 37 | + "is_first_party": { |
| 38 | + Type: schema.TypeBool, |
| 39 | + Optional: true, |
| 40 | + Description: "Filter clients by first party status.", |
| 41 | + }, |
| 42 | + "clients": { |
| 43 | + Type: schema.TypeList, |
| 44 | + Computed: true, |
| 45 | + Description: "List of clients matching the filter criteria.", |
| 46 | + Elem: &schema.Resource{ |
| 47 | + Schema: coreClientDataSourceSchema(), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func coreClientDataSourceSchema() map[string]*schema.Schema { |
| 55 | + clientSchema := dataSourceSchema() |
| 56 | + |
| 57 | + // Remove unused fields from the client schema. |
| 58 | + fieldsToRemove := []string{ |
| 59 | + "client_aliases", |
| 60 | + "logo_uri", |
| 61 | + "oidc_conformant", |
| 62 | + "oidc_backchannel_logout_urls", |
| 63 | + "organization_usage", |
| 64 | + "organization_require_behavior", |
| 65 | + "cross_origin_auth", |
| 66 | + "cross_origin_loc", |
| 67 | + "custom_login_page_on", |
| 68 | + "custom_login_page", |
| 69 | + "form_template", |
| 70 | + "require_pushed_authorization_requests", |
| 71 | + "mobile", |
| 72 | + "initiate_login_uri", |
| 73 | + "native_social_login", |
| 74 | + "refresh_token", |
| 75 | + "signing_keys", |
| 76 | + "encryption_key", |
| 77 | + "sso", |
| 78 | + "sso_disabled", |
| 79 | + "jwt_configuration", |
| 80 | + "addons", |
| 81 | + "default_organization", |
| 82 | + "compliance_level", |
| 83 | + "require_proof_of_possession", |
| 84 | + "token_endpoint_auth_method", |
| 85 | + "signed_request_object", |
| 86 | + "client_authentication_methods", |
| 87 | + } |
| 88 | + |
| 89 | + for _, field := range fieldsToRemove { |
| 90 | + delete(clientSchema, field) |
| 91 | + } |
| 92 | + |
| 93 | + return clientSchema |
| 94 | +} |
| 95 | + |
| 96 | +func readClientsForDataSource(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 97 | + api := meta.(*config.Config).GetAPI() |
| 98 | + |
| 99 | + nameFilter := data.Get("name_filter").(string) |
| 100 | + appTypesSet := data.Get("app_types").(*schema.Set) |
| 101 | + isFirstParty := data.Get("is_first_party").(bool) |
| 102 | + |
| 103 | + appTypes := make([]string, 0, appTypesSet.Len()) |
| 104 | + for _, v := range appTypesSet.List() { |
| 105 | + appTypes = append(appTypes, v.(string)) |
| 106 | + } |
| 107 | + |
| 108 | + var clients []*management.Client |
| 109 | + |
| 110 | + params := []management.RequestOption{ |
| 111 | + management.PerPage(100), |
| 112 | + } |
| 113 | + |
| 114 | + if len(appTypes) > 0 { |
| 115 | + params = append(params, management.Parameter("app_type", strings.Join(appTypes, ","))) |
| 116 | + } |
| 117 | + if isFirstParty { |
| 118 | + params = append(params, management.Parameter("is_first_party", "true")) |
| 119 | + } |
| 120 | + |
| 121 | + var page int |
| 122 | + for { |
| 123 | + // Add current page parameter. |
| 124 | + params = append(params, management.Page(page)) |
| 125 | + |
| 126 | + list, err := api.Client.List(ctx, params...) |
| 127 | + if err != nil { |
| 128 | + return diag.FromErr(err) |
| 129 | + } |
| 130 | + |
| 131 | + for _, client := range list.Clients { |
| 132 | + if nameFilter == "" || strings.Contains(client.GetName(), nameFilter) { |
| 133 | + clients = append(clients, client) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if !list.HasNext() { |
| 138 | + break |
| 139 | + } |
| 140 | + |
| 141 | + // Remove the page parameter and increment for next iteration. |
| 142 | + params = params[:len(params)-1] |
| 143 | + page++ |
| 144 | + } |
| 145 | + |
| 146 | + filterID := generateFilterID(nameFilter, appTypes, isFirstParty) |
| 147 | + data.SetId(filterID) |
| 148 | + |
| 149 | + if err := flattenClientList(data, clients); err != nil { |
| 150 | + return diag.FromErr(err) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func generateFilterID(nameFilter string, appTypes []string, isFirstParty bool) string { |
| 157 | + h := sha256.New() |
| 158 | + h.Write([]byte(fmt.Sprintf("%s-%v-%v", nameFilter, appTypes, isFirstParty))) |
| 159 | + return fmt.Sprintf("clients-%x", h.Sum(nil)) |
| 160 | +} |
0 commit comments