Skip to content

Commit

Permalink
trim whitespaces for login request
Browse files Browse the repository at this point in the history
  • Loading branch information
changchaishi committed Jan 23, 2025
1 parent b45e11c commit adea97e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"

"github.com/go-openapi/runtime"
Expand Down Expand Up @@ -124,11 +125,24 @@ func getConsoleCredentials(accessKey, secretKey string, client *http.Client) (*C
}, nil
}

// trimWhitespace removes any leading and trailing whitespace from the login request string field
func trimWhitespace(lr *models.LoginRequest) {
val := reflect.ValueOf(lr).Elem()

for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if field.Kind() == reflect.String {
field.SetString(strings.TrimSpace(field.String()))
}
}
}

// getLoginResponse performs login() and serializes it to the handler's output
func getLoginResponse(params authApi.LoginParams) (*models.LoginResponse, *CodedAPIError) {
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
lr := params.Body
trimWhitespace(lr)

clientIP := getClientIP(params.HTTPRequest)
client := GetConsoleHTTPClient(clientIP)
Expand Down
30 changes: 30 additions & 0 deletions integration/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ func TestLogout(t *testing.T) {
assert.Equal(response.StatusCode, 200)
}

func TestLoginExtraSpaces(t *testing.T) {
assert := assert.New(t)

client := &http.Client{
Timeout: 2 * time.Second,
}
requestData := map[string]string{
"accessKey": " minioadmin ",
"secretKey": "minioadmin",
}

requestDataJSON, _ := json.Marshal(requestData)

requestDataBody := bytes.NewReader(requestDataJSON)

request, err := http.NewRequest("POST", "http://localhost:9090/api/v1/login", requestDataBody)
if err != nil {
log.Println(err)
return
}

request.Header.Add("Content-Type", "application/json")

response, err := client.Do(request)

assert.Equal(204, response.StatusCode, "Login request should succeed")
assert.NotNil(response, "Login response is nil")
assert.Nil(err, "Login errored out")
}

func TestBadLogin(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit adea97e

Please sign in to comment.