tstor/pkg/qbittorrent/authentication.go

92 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-08-31 23:00:13 +00:00
package qbittorrent
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
)
type Authentication interface {
// Login cookie-based authentication, after calling NewClient, do not need to call Login again,
// it is the default behavior
Login(ctx context.Context) error
// Logout deactivate cookies
Logout(ctx context.Context) error
}
func (c *client) Login(ctx context.Context) error {
2024-09-08 18:39:11 +00:00
ctx, span := trace.Start(ctx, "qbittorrent.Authentication.Login")
defer span.End()
2024-08-31 23:00:13 +00:00
if c.config.Username == "" || c.config.Password == "" {
return errors.New("username or password is empty")
}
formData := url.Values{}
formData.Set("username", c.config.Username)
formData.Set("password", c.config.Password)
encodedFormData := formData.Encode()
apiUrl := fmt.Sprintf("%s/api/v2/auth/login", c.config.Address)
result, err := c.doRequest(ctx, &requestData{
method: http.MethodPost,
url: apiUrl,
body: strings.NewReader(encodedFormData),
})
if err != nil {
return err
}
if result.code != 200 {
return errors.New("login failed: " + string(result.body))
}
if string(result.body) == "Fails." {
return ErrAuthFailed
}
if string(result.body) != "Ok." {
return errors.New("login failed: " + string(result.body))
}
2024-09-08 18:39:11 +00:00
if c.client.Jar == nil {
c.client.Jar, err = cookiejar.New(nil)
2024-08-31 23:00:13 +00:00
if err != nil {
return err
}
}
u, err := url.Parse(c.config.Address)
if err != nil {
return err
}
2024-09-08 18:39:11 +00:00
c.client.Jar.SetCookies(u, result.cookies)
2024-08-31 23:00:13 +00:00
return nil
}
func (c *client) Logout(ctx context.Context) error {
2024-09-08 18:39:11 +00:00
ctx, span := trace.Start(ctx, "qbittorrent.Authentication.Logout")
defer span.End()
2024-08-31 23:00:13 +00:00
apiUrl := fmt.Sprintf("%s/api/v2/auth/logout", c.config.Address)
result, err := c.doRequest(ctx, &requestData{
method: http.MethodPost,
url: apiUrl,
})
if err != nil {
return err
}
if result.code != 200 {
return errors.New("logout failed: " + string(result.body))
}
return nil
}