Fix logout and use FlexBool/FlexInt

This commit is contained in:
spsobole 2021-08-13 10:47:38 -06:00
parent fb3f2e5578
commit 7b122409df
3 changed files with 55 additions and 31 deletions

View File

@ -38,8 +38,8 @@ const (
APILoginPath string = "/api/login" APILoginPath string = "/api/login"
// APILoginPathNew is how we log into UDM 5.12.55+. // APILoginPathNew is how we log into UDM 5.12.55+.
APILoginPathNew string = "/api/auth/login" APILoginPathNew string = "/api/auth/login"
// APILogoutPath is the how we logout from UDM // APILogoutPath is how we logout from UDM
APILogoutPath string = "/api/auth/logout" APILogoutPath string = "/api/logout"
// APIEventPathIDS returns Intrusion Detection/Prevention Systems Events. // APIEventPathIDS returns Intrusion Detection/Prevention Systems Events.
APIEventPathIDS string = "/api/s/%s/stat/ips/event" APIEventPathIDS string = "/api/s/%s/stat/ips/event"
// APIEventPathAlarms contains the site alarms. // APIEventPathAlarms contains the site alarms.

View File

@ -28,7 +28,7 @@ import (
var ( var (
ErrAuthenticationFailed = fmt.Errorf("authentication failed") ErrAuthenticationFailed = fmt.Errorf("authentication failed")
ErrInvalidStatusCode = fmt.Errorf("invalid status code from server") ErrInvalidStatusCode = fmt.Errorf("invalid status code from server")
ErrNoParams = fmt.Errorf("requedted PUT with no parameters") ErrNoParams = fmt.Errorf("requested PUT with no parameters")
ErrInvalidSignature = fmt.Errorf("certificate signature does not match") ErrInvalidSignature = fmt.Errorf("certificate signature does not match")
) )
@ -144,9 +144,9 @@ func (u *Unifi) Login() error {
// Logout closes the current session // Logout closes the current session
func (u *Unifi) Logout() error { func (u *Unifi) Logout() error {
var response struct { // a post is needed for logout
} _, err := u.PostJSON(APILogoutPath)
return u.GetData(APILogoutPath, &response) return err
} }
// with the release of controller version 5.12.55 on UDM in Jan 2020 the api paths // with the release of controller version 5.12.55 on UDM in Jan 2020 the api paths
@ -286,6 +286,19 @@ func (u *Unifi) UniReqPut(apiPath string, params string) (*http.Request, error)
return req, nil return req, nil
} }
// UniReqPost is the Post call equivalent to UniReq.
func (u *Unifi) UniReqPost(apiPath string, params string) (*http.Request, error) {
apiPath = u.path(apiPath)
req, err := http.NewRequest(http.MethodPost, u.URL+apiPath, bytes.NewBufferString("")) //nolint:noctx
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
u.setHeaders(req, params)
return req, nil
}
// GetJSON returns the raw JSON from a path. This is useful for debugging. // GetJSON returns the raw JSON from a path. This is useful for debugging.
func (u *Unifi) GetJSON(apiPath string, params ...string) ([]byte, error) { func (u *Unifi) GetJSON(apiPath string, params ...string) ([]byte, error) {
req, err := u.UniReq(apiPath, strings.Join(params, " ")) req, err := u.UniReq(apiPath, strings.Join(params, " "))
@ -307,6 +320,17 @@ func (u *Unifi) PutJSON(apiPath string, params ...string) ([]byte, error) {
return u.do(req) return u.do(req)
} }
// PostJSON uses a POST call and returns the raw JSON in the same way as GetData
// Use this if you want to change data via the REST API.
func (u *Unifi) PostJSON(apiPath string, params ...string) ([]byte, error) {
req, err := u.UniReqPost(apiPath, strings.Join(params, " "))
if err != nil {
return []byte{}, err
}
return u.do(req)
}
func (u *Unifi) do(req *http.Request) ([]byte, error) { func (u *Unifi) do(req *http.Request) ([]byte, error) {
var ( var (
cancel func() cancel func()

View File

@ -41,29 +41,29 @@ func (u *Unifi) GetUsers(sites []*Site, hours int) ([]*User, error) {
// User defines the metadata available for previously connected clients // User defines the metadata available for previously connected clients
type User struct { type User struct {
SourceName string `json:"-"` SourceName string `json:"-"`
SiteName string `json:"-"` SiteName string `json:"-"`
ID string `json:"_id"` ID string `json:"_id"`
Mac string `json:"mac"` Mac string `json:"mac"`
SiteID string `json:"site_id"` SiteID string `json:"site_id"`
Oui string `json:"oui,omitempty"` Oui string `json:"oui,omitempty"`
IsGuest bool `json:"is_guest"` IsGuest bool `json:"is_guest"`
FirstSeen int64 `json:"first_seen,omitempty"` FirstSeen FlexInt `json:"first_seen,omitempty"`
LastSeen int64 `json:"last_seen,omitempty"` LastSeen FlexInt `json:"last_seen,omitempty"`
IsWired bool `json:"is_wired,omitempty"` IsWired bool `json:"is_wired,omitempty"`
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
Duration int64 `json:"duration,omitempty"` Duration FlexInt `json:"duration,omitempty"`
TxBytes int64 `json:"tx_bytes,omitempty"` TxBytes FlexInt `json:"tx_bytes,omitempty"`
TxPackets int64 `json:"tx_packets,omitempty"` TxPackets FlexInt `json:"tx_packets,omitempty"`
RxBytes int64 `json:"rx_bytes,omitempty"` RxBytes FlexInt `json:"rx_bytes,omitempty"`
RxPackets int64 `json:"rx_packets,omitempty"` RxPackets FlexInt `json:"rx_packets,omitempty"`
WifiTxAttempts int64 `json:"wifi_tx_attempts,omitempty"` WifiTxAttempts FlexInt `json:"wifi_tx_attempts,omitempty"`
TxRetries int64 `json:"tx_retries,omitempty"` TxRetries FlexInt `json:"tx_retries,omitempty"`
UsergroupID string `json:"usergroup_id,omitempty"` UsergroupID string `json:"usergroup_id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Note string `json:"note,omitempty"` Note string `json:"note,omitempty"`
Noted bool `json:"noted,omitempty"` Noted FlexBool `json:"noted,omitempty"`
Blocked bool `json:"blocked,omitempty"` Blocked FlexBool `json:"blocked,omitempty"`
DevIDOverride int64 `json:"dev_id_override,omitempty"` DevIDOverride FlexInt `json:"dev_id_override,omitempty"`
FingerprintOverride bool `json:"fingerprint_override,omitempty"` FingerprintOverride FlexBool `json:"fingerprint_override,omitempty"`
} }