Skip to content

Commit

Permalink
feat: adiciona basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomatavelli committed Sep 21, 2024
1 parent 0305dfe commit 09b42e1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
17 changes: 12 additions & 5 deletions millennium.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"net/http"
"net/url"
"slices"
"strings"
"time"

Expand All @@ -25,6 +26,7 @@ type AuthType string
// Authentication types available for Millennium
const (
NTLM AuthType = "NTLM"
Basic AuthType = "BASIC"
Session AuthType = "SESSION"
)

Expand Down Expand Up @@ -157,9 +159,7 @@ func (m *Millennium) Login(username string, password string, authType AuthType)
m.Client.HTTPClient.Transport = ntlmssp.Negotiator{
RoundTripper: &http.Transport{},
}
}

if authType == Session {
} else if authType == Session {
var responseLogin ResponseLogin
m.headers.Set("WTS-Authorization", fmt.Sprintf("%s/%s", strings.ToUpper(m.credentials.Username), strings.ToUpper(m.credentials.Password)))
if err := m.Post("login", []byte{}, &responseLogin); err != nil {
Expand Down Expand Up @@ -216,6 +216,7 @@ func (m *Millennium) Request(r RequestMethod) (err error) {
requestBody := bodyReader

req, err := retryablehttp.NewRequestWithContext(m.Context, requestMethod, requestURL, requestBody)

if err != nil {
return fmt.Errorf("unable to start new request to Millennium: %w", err)
}
Expand All @@ -224,8 +225,8 @@ func (m *Millennium) Request(r RequestMethod) (err error) {
req.Header = m.headers
}

// If authType is NTLM, set basic auth on request
if m.credentials.AuthType == NTLM {
// If authType is NTLM or Basic, set basic auth on request
if m.credentials.AuthType == NTLM || m.credentials.AuthType == Basic {
req.SetBasicAuth(m.credentials.Username, m.credentials.Password)
}

Expand All @@ -243,6 +244,12 @@ func (m *Millennium) sendRequest(request *retryablehttp.Request, response interf
return fmt.Errorf("unable to send request: %w", err)
}

if !slices.Contains([]int{http.StatusOK, http.StatusNoContent, http.StatusCreated, http.StatusTemporaryRedirect, http.StatusPermanentRedirect}, res.StatusCode) {
defer res.Body.Close()

return fmt.Errorf("unable to send request: %s", res.Status)
}

return m.getResponse(res, &response)
}

Expand Down
44 changes: 44 additions & 0 deletions millennium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ func (s *mockHTTPServer) Start() *httptest.Server {
Body: s.jsonError("Query error", http.StatusInternalServerError),
})
})
mux.HandleFunc("/api/test.basicauth", func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "correct_user" || password != "correct_password" {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "", http.StatusUnauthorized)
return
}

s.writeOutput(&writeOutputParams{
Writer: w,
Request: r,
StatusCode: 200,
Body: []byte(`{"odata.count": 1,"value":[{"number":1,"string":"test","bool":true}]}`),
})
})

s.testServer = httptest.NewServer(mux)
return s.testServer
Expand Down Expand Up @@ -298,6 +313,35 @@ func TestNTLM(t *testing.T) {
}
}

func TestBasicAuth(t *testing.T) {
client := NewTestClient(t)
err := client.Login("test", "test", Basic)
if err != nil {
t.Error(err)
}

var _r interface{}

_, err = client.Get("test.basicauth", url.Values{}, &_r)
if err == nil {
t.Error("Expected error")
}

err = client.Login("correct_user", "correct_password", Basic)
if err != nil {
t.Error(err)
}

x, err := client.Get("test.basicauth", url.Values{}, &_r)
if err != nil {
t.Fatal(err)
}

if x == 0 {
t.Error("Zero records returned")
}
}

func TestRequest(t *testing.T) {
client := NewTestClient(t)

Expand Down

0 comments on commit 09b42e1

Please sign in to comment.