Skip to content

Commit

Permalink
Ensure we catch and return bad status codes
Browse files Browse the repository at this point in the history
Signed-off-by: Brendan Devenney <[email protected]>
  • Loading branch information
devenney-form3 committed Sep 5, 2019
1 parent 387bdea commit 6affce0
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions auth0/auth0_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"

"github.com/parnurzeal/gorequest"
)

Expand Down Expand Up @@ -112,7 +113,11 @@ func (config *Config) getAuthenticationHeader() string {
// User
func (authClient *AuthClient) GetUserById(id string) (*User, error) {

_, body, errs := gorequest.New().Get(authClient.config.apiUri+"users/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Get(authClient.config.apiUri+"users/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could parse user response from auth0, error: %v", errs)
Expand All @@ -133,7 +138,11 @@ func (authClient *AuthClient) GetUserById(id string) (*User, error) {

func (authClient *AuthClient) CreateUser(userRequest *UserRequest) (*User, error) {

_, body, errs := gorequest.New().Post(authClient.config.apiUri+"users").Send(userRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Post(authClient.config.apiUri+"users").Send(userRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could create user in auth0, error: %v", errs)
Expand All @@ -154,13 +163,17 @@ func (authClient *AuthClient) CreateUser(userRequest *UserRequest) (*User, error

func (authClient *AuthClient) UpdateUserById(id string, userRequest *UserRequest) (*User, error) {

_, body, errs := gorequest.New().
resp, body, errs := gorequest.New().
Patch(authClient.config.apiUri+"users/"+id).
Set("Authorization", authClient.config.getAuthenticationHeader()).
Set("Content-Type", "application/json").
Send(userRequest).
End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could not update auth0 user, error: %v", errs)
}
Expand Down Expand Up @@ -191,7 +204,11 @@ func (authClient *AuthClient) DeleteUserById(id string) error {
// Client
func (authClient *AuthClient) GetClientById(id string) (*Client, error) {

_, body, errs := gorequest.New().Get(authClient.config.apiUri+"clients/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Get(authClient.config.apiUri+"clients/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could parse client response from auth0, error: %v", errs)
Expand All @@ -212,7 +229,11 @@ func (authClient *AuthClient) GetClientById(id string) (*Client, error) {

func (authClient *AuthClient) CreateClient(clientRequest *ClientRequest) (*Client, error) {

_, body, errs := gorequest.New().Post(authClient.config.apiUri+"clients").Send(clientRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Post(authClient.config.apiUri+"clients").Send(clientRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could create client in auth0, error: %v", errs)
Expand All @@ -232,7 +253,12 @@ func (authClient *AuthClient) CreateClient(clientRequest *ClientRequest) (*Clien
}

func (authClient *AuthClient) UpdateClientById(id string, clientRequest *ClientRequest) (*Client, error) {
_, body, errs := gorequest.New().Patch(authClient.config.apiUri+"clients/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Patch(authClient.config.apiUri+"clients/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could not update auth0 client, error: %v", errs)
}
Expand Down Expand Up @@ -262,7 +288,11 @@ func (authClient *AuthClient) DeleteClientById(id string) error {
// Api
func (authClient *AuthClient) GetApiById(id string) (*Api, error) {

_, body, errs := gorequest.New().Get(authClient.config.apiUri+"resource-servers/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Get(authClient.config.apiUri+"resource-servers/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could parse api response from auth0, error: %v", errs)
Expand All @@ -283,7 +313,11 @@ func (authClient *AuthClient) GetApiById(id string) (*Api, error) {

func (authClient *AuthClient) CreateApi(apiRequest *ApiRequest) (*Api, error) {

_, body, errs := gorequest.New().Post(authClient.config.apiUri+"resource-servers").Send(apiRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Post(authClient.config.apiUri+"resource-servers").Send(apiRequest).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could create api in auth0, error: %v", errs)
Expand All @@ -304,7 +338,12 @@ func (authClient *AuthClient) CreateApi(apiRequest *ApiRequest) (*Api, error) {

func (authClient *AuthClient) UpdateApiById(id string, apiRequest *ApiRequest) (*Api, error) {

_, body, errs := gorequest.New().Patch(authClient.config.apiUri+"resource-servers/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Patch(authClient.config.apiUri+"resource-servers/"+id).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could not update auth0 api, error: %v", errs)
}
Expand Down Expand Up @@ -340,11 +379,15 @@ func (authClient *AuthClient) GetClientGrantByClientIdAndAudience(clientId strin
"audience": audience,
}

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

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could parse client-grant response from auth0, error: %v", errs)
}
Expand All @@ -368,7 +411,11 @@ func (authClient *AuthClient) CreateClientGrant(clientGrantRequest *ClientGrantR
return nil, fmt.Errorf("failed to marshal client grant request: %v", err)
}

_, body, errs := gorequest.New().Post(authClient.config.apiUri+"client-grants").SendString(string(reqJSON)).Set("Authorization", authClient.config.getAuthenticationHeader()).End()
resp, body, errs := gorequest.New().Post(authClient.config.apiUri+"client-grants").SendString(string(reqJSON)).Set("Authorization", authClient.config.getAuthenticationHeader()).End()

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

if errs != nil {
return nil, fmt.Errorf("could create client-grant in auth0, error: %v", errs)
Expand All @@ -389,7 +436,12 @@ func (authClient *AuthClient) CreateClientGrant(clientGrantRequest *ClientGrantR

func (authClient *AuthClient) UpdateClientGrantById(id string, clientGrantRequest *ClientGrantRequest) (*ClientGrant, error) {

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

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("bad status code (%d): %s", resp.StatusCode, body)
}

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

0 comments on commit 6affce0

Please sign in to comment.