Skip to content

Commit

Permalink
Code refactoring after #1029
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 28, 2024
1 parent 801bb2d commit 3bc5274
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions pkg/tapo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ func (c *Client) newConn() (net.Conn, error) {

query := u.Query()

deviceId := query.Get("deviceId")
if deviceId != "" {
q := req.URL.Query()
q.Set("deviceId", deviceId)
req.URL.RawQuery = q.Encode()
if deviceId := query.Get("deviceId"); deviceId != "" {
req.URL.RawQuery = "deviceId=" + deviceId
}

req.URL.User = u.User
Expand Down Expand Up @@ -285,16 +282,14 @@ func dial(req *http.Request) (net.Conn, *http.Response, error) {
if err != nil {
return nil, nil, err
}
_ = res.Body.Close() // ignore response body

auth := res.Header.Get("WWW-Authenticate")

if res.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(auth, "Digest") {
return nil, nil, err
}

// Read the entire request body to prevent issues later - H200 seems to send HTTP ERROR 401 as body response.
_, _ = io.Copy(io.Discard, res.Body)

if password == "" {
// support cloud password in place of username
if strings.Contains(auth, `encrypt_type="3"`) {
Expand All @@ -308,32 +303,21 @@ func dial(req *http.Request) (net.Conn, *http.Response, error) {
realm := tcp.Between(auth, `realm="`, `"`)
nonce := tcp.Between(auth, `nonce="`, `"`)
qop := tcp.Between(auth, `qop="`, `"`)
// opaque is optional
op := ""
if strings.Contains(auth, `opaque="`) {
op = tcp.Between(auth, `opaque="`, `"`)
}
uri := req.URL.RequestURI()
ha1 := tcp.HexMD5(username, realm, password)
ha2 := tcp.HexMD5(req.Method, uri)
nc := "00000001"

// Generate a random cnonce
cnonce := core.RandString(32, 64)

response := tcp.HexMD5(ha1, nonce, nc, cnonce, qop, ha2)

header := ""
if op != "" {
header = fmt.Sprintf(
`Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s", opaque="%s", algorithm=MD5`,
username, realm, nonce, uri, qop, nc, cnonce, response, op,
)
} else {
header = fmt.Sprintf(
`Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s"`,
username, realm, nonce, uri, qop, nc, cnonce, response,
)
// https://datatracker.ietf.org/doc/html/rfc7616
header := fmt.Sprintf(
`Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s"`,
username, realm, nonce, uri, qop, nc, cnonce, response,
)

if opaque := tcp.Between(auth, `opaque="`, `"`); opaque != "" {
header += fmt.Sprintf(`, opaque="%s", algorithm=MD5`, opaque)
}

req.Header.Set("Authorization", header)
Expand Down

0 comments on commit 3bc5274

Please sign in to comment.