Skip to content

Commit

Permalink
CLOUDP-120670 client for registration config endpoint (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibulca committed Jun 9, 2022
1 parent 551edbf commit e92e8ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions auth/device_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions auth/device_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e92e8ce

Please sign in to comment.