From f86167bda56c7f458b5c1a1f34f6b99d4415b975 Mon Sep 17 00:00:00 2001 From: Ciprian Tibulca Date: Thu, 9 Jun 2022 16:51:49 +0100 Subject: [PATCH] CLOUDP-120670 client for registration config endpoint --- auth/device_flow.go | 18 ++++++++++++++++++ auth/device_flow_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/auth/device_flow.go b/auth/device_flow.go index 3bde72aea..1b4ae3b58 100644 --- a/auth/device_flow.go +++ b/auth/device_flow.go @@ -39,6 +39,10 @@ type DeviceCode struct { timeSleep func(time.Duration) } +type RegistrationConfig struct { + RegistrationUrl string `json:"registrationUrl"` +} + const deviceBasePath = "api/private/unauth/account/device" // RequestCode initiates the authorization flow by requesting a code. @@ -149,6 +153,20 @@ func (c Config) RevokeToken(ctx context.Context, token, tokenTypeHint string) (* return c.Do(ctx, req, nil) } +// RegistrationConfig retrieves the config used for registration. +func (c Config) RegistrationConfig(ctx context.Context) (*RegistrationConfig, *atlas.Response, error) { + req, err := c.NewRequest(ctx, http.MethodGet, deviceBasePath+"/registration", url.Values{}) + if err != nil { + return nil, nil, err + } + var rc *RegistrationConfig + resp, err := c.Do(ctx, req, &rc) + if err != nil { + return nil, resp, err + } + return rc, resp, err +} + func IsTimeoutErr(err error) bool { var target *atlas.ErrorResponse return errors.Is(err, ErrTimeout) || (errors.As(err, &target) && target.ErrorCode == authExpiredError) diff --git a/auth/device_flow_test.go b/auth/device_flow_test.go index 2f2e70b0d..01f31594f 100644 --- a/auth/device_flow_test.go +++ b/auth/device_flow_test.go @@ -181,3 +181,31 @@ func TestConfig_RevokeToken(t *testing.T) { t.Fatalf("RequestCode returned error: %v", err) } } + +func TestConfig_RegistrationConfig(t *testing.T) { + config, mux, teardown := setup() + defer teardown() + + mux.HandleFunc("/api/private/unauth/account/device/registration", func(w http.ResponseWriter, r *http.Request) { + if http.MethodGet != r.Method { + t.Errorf("Request method = %v, expected %v", r.Method, http.MethodGet) + } + + fmt.Fprint(w, `{ + "registrationUrl": "http://localhost:8080/account/register/cli" + }`) + }) + + results, _, err := config.RegistrationConfig(ctx) + if err != nil { + t.Fatalf("RegistrationConfig returned error: %v", err) + } + + expected := &RegistrationConfig{ + RegistrationUrl: "http://localhost:8080/account/register/cli", + } + + if diff := deep.Equal(results, expected); diff != nil { + t.Error(diff) + } +}