Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-120670 client for registration config endpoint #299

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}