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

Ensure we catch and return bad status codes #14

Merged
merged 3 commits into from
Sep 5, 2019
Merged
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
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 && resp.StatusCode != 404 {
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 && resp.StatusCode != 404 {
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 && resp.StatusCode != 404 {
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 && resp.StatusCode != 404 {
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