From 6a156f6243dccff7f6f9985eb74c14012bf2da32 Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Wed, 31 May 2023 16:45:02 +0100 Subject: [PATCH] client/tailscale: support deauthorizing a device This adds a new `SetAuthorized` method that allows setting device authorization to true or false. I chose the method name to be consistent with SetTags. Updates https://github.com/tailscale/corp/issues/10160 Signed-off-by: Anton Tolchanov --- client/tailscale/devices.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/client/tailscale/devices.go b/client/tailscale/devices.go index ddca5822c..e75f9ea17 100644 --- a/client/tailscale/devices.go +++ b/client/tailscale/devices.go @@ -12,7 +12,6 @@ "fmt" "net/http" "net/url" - "strings" "tailscale.com/types/opt" ) @@ -213,8 +212,20 @@ func (c *Client) DeleteDevice(ctx context.Context, deviceID string) (err error) // AuthorizeDevice marks a device as authorized. func (c *Client) AuthorizeDevice(ctx context.Context, deviceID string) error { + return c.SetAuthorized(ctx, deviceID, true) +} + +// SetAuthorized marks a device as authorized or not. +func (c *Client) SetAuthorized(ctx context.Context, deviceID string, authorized bool) error { + params := &struct { + Authorized bool `json:"authorized"` + }{Authorized: authorized} + data, err := json.Marshal(params) + if err != nil { + return err + } path := fmt.Sprintf("%s/api/v2/device/%s/authorized", c.baseURL(), url.PathEscape(deviceID)) - req, err := http.NewRequestWithContext(ctx, "POST", path, strings.NewReader(`{"authorized":true}`)) + req, err := http.NewRequestWithContext(ctx, "POST", path, bytes.NewBuffer(data)) if err != nil { return err }