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

Improve the XSUAA.go file #1970

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 17 additions & 14 deletions internal/btp/xsuaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 {
Expand All @@ -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)
}
7 changes: 4 additions & 3 deletions internal/btp/xsuaa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package btp
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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",
}
Expand Down
Loading