From 71d17239875224bae910f2bc511415a24c0c0f36 Mon Sep 17 00:00:00 2001 From: Filip Strozik Date: Tue, 27 Feb 2024 14:20:09 +0100 Subject: [PATCH] minor improvements --- internal/btp/xsuaa.go | 31 +++++++++++++++++-------------- internal/btp/xsuaa_test.go | 7 ++++--- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/internal/btp/xsuaa.go b/internal/btp/xsuaa.go index f1183e308..f9111713b 100644 --- a/internal/btp/xsuaa.go +++ b/internal/btp/xsuaa.go @@ -8,7 +8,9 @@ import ( "strings" ) -type errorResponse struct { +const authorizationEndpoint = "oauth/token" + +type xsuaaErrorResponse struct { Error string `json:"error"` ErrorDescription string `json:"error_description"` } @@ -27,7 +29,7 @@ func GetOAuthToken(credentials *CISCredentials) (*XSUAAToken, error) { request, err := http.NewRequest( http.MethodPost, - fmt.Sprintf("%s/oauth/token", credentials.UAA.URL), + fmt.Sprintf("%s/%s", credentials.UAA.URL, authorizationEndpoint), strings.NewReader(urlBody.Encode()), ) if err != nil { @@ -41,33 +43,34 @@ func GetOAuthToken(credentials *CISCredentials) (*XSUAAToken, error) { credentials.UAA.ClientSecret, ) - resp, err := http.DefaultClient.Do(request) + response, err := http.DefaultClient.Do(request) if err != nil { return nil, fmt.Errorf("failed to get token from server: %s", err.Error()) } - defer resp.Body.Close() - if resp.StatusCode != 200 { - return nil, decodeAuthErrorResponse(resp) + defer response.Body.Close() + + if response.StatusCode != 200 { + return nil, decodeAuthErrorResponse(response) } - return decodeAuthSuccessResponse(resp) + return decodeAuthSuccessResponse(response) } -func decodeAuthSuccessResponse(resp *http.Response) (*XSUAAToken, error) { +func decodeAuthSuccessResponse(response *http.Response) (*XSUAAToken, error) { token := XSUAAToken{} - err := json.NewDecoder(resp.Body).Decode(&token) + err := json.NewDecoder(response.Body).Decode(&token) if err != nil { - return nil, fmt.Errorf("failed to decode response: %s", err.Error()) + return nil, fmt.Errorf("failed to decode response with Status '%s': %s", response.Status, err.Error()) } return &token, nil } -func decodeAuthErrorResponse(resp *http.Response) error { - errorData := errorResponse{} - err := json.NewDecoder(resp.Body).Decode(&errorData) +func decodeAuthErrorResponse(response *http.Response) error { + errorData := xsuaaErrorResponse{} + err := json.NewDecoder(response.Body).Decode(&errorData) if err != nil { return fmt.Errorf("failed to decode error response: %s", err.Error()) } - return fmt.Errorf("error response: %s: %s", errorData.Error, errorData.ErrorDescription) + return fmt.Errorf("error response: %s: %s", response.Status, errorData.ErrorDescription) } diff --git a/internal/btp/xsuaa_test.go b/internal/btp/xsuaa_test.go index 3ad65d745..70cdbafeb 100644 --- a/internal/btp/xsuaa_test.go +++ b/internal/btp/xsuaa_test.go @@ -3,6 +3,7 @@ package btp import ( "encoding/json" "errors" + "fmt" "net/http" "net/http/httptest" "testing" @@ -73,7 +74,7 @@ func TestGetOAuthToken(t *testing.T) { }, }, want: nil, - expectedErr: errors.New("error response: error: description"), + expectedErr: errors.New("error response: 401 Unauthorized: description"), }, } for _, tt := range tests { @@ -91,7 +92,7 @@ func TestGetOAuthToken(t *testing.T) { func fixAuthenticationHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/oauth/token" { + if r.URL.Path != fmt.Sprintf("/%s", authorizationEndpoint) { w.WriteHeader(404) return } @@ -113,7 +114,7 @@ func fixAuthenticationHandler(t *testing.T) func(http.ResponseWriter, *http.Requ func fixAuthenticationErrorHandler(t *testing.T) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, _ *http.Request) { - data := errorResponse{ + data := xsuaaErrorResponse{ Error: "error", ErrorDescription: "description", }