Skip to content

Commit

Permalink
Fix client grant importing
Browse files Browse the repository at this point in the history
Signed-off-by: Brendan Devenney <[email protected]>
  • Loading branch information
devenney-form3 committed Aug 29, 2019
1 parent 7f54d91 commit 8a6653b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
30 changes: 30 additions & 0 deletions auth0/auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,36 @@ func (authClient *AuthClient) DeleteApiById(id string) error {
return nil
}

// GetClientGrantById retrieves a client grant based on ID.
//
// Note that this is significantly heavier than GetClientGrantByClientIdAndAudience due to the Auth0 API's lack of
// ID-based retrieval.
func (authClient *AuthClient) GetClientGrantById(id string) (*ClientGrant, error) {

_, body, errs := gorequest.New().
Get(authClient.config.apiUri+"client-grants").
Set("Authorization", authClient.config.getAuthenticationHeader()).
End()

if errs != nil {
return nil, fmt.Errorf("could parse client-grant response from auth0, error: %v", errs)
}

clientGrant := make([]ClientGrant, 0)
err := json.Unmarshal([]byte(body), &clientGrant)
if err != nil {
return nil, fmt.Errorf("could not parse auth0 get client-grant response, error: %v %s", err, body)
}

for _, grant := range clientGrant {
if grant.Id == id {
return &grant, nil
}
}

return nil, nil
}

// ClientGrant
func (authClient *AuthClient) GetClientGrantByClientIdAndAudience(clientId string, audience string) (*ClientGrant, error) {

Expand Down
9 changes: 8 additions & 1 deletion auth0/resource_auth0_client_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ func resourceAuth0ClientGrantCreate(d *schema.ResourceData, meta interface{}) er
}

func resourceAuth0ClientGrantRead(d *schema.ResourceData, meta interface{}) error {
var clientGrant *ClientGrant
var err error

auth0Client := meta.(*AuthClient)

clientId := readStringFromResource(d, "client_id")
audience := readStringFromResource(d, "audience")

clientGrant, err := auth0Client.GetClientGrantByClientIdAndAudience(clientId, audience)
if clientId != "" && audience != "" {
clientGrant, err = auth0Client.GetClientGrantByClientIdAndAudience(clientId, audience)
} else {
// This is necessary for ID-only import but it's significantly heavier than querying by client ID and audience
clientGrant, err = auth0Client.GetClientGrantById(d.Id())
}

if err != nil {
return fmt.Errorf("could not find auth0 client-grant: %v", err)
Expand Down

0 comments on commit 8a6653b

Please sign in to comment.