Skip to content

Commit e92e8ce

Browse files
CLOUDP-120670 client for registration config endpoint (#299)
1 parent 551edbf commit e92e8ce

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

auth/device_flow.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type DeviceCode struct {
3939
timeSleep func(time.Duration)
4040
}
4141

42+
type RegistrationConfig struct {
43+
RegistrationURL string `json:"registrationUrl"`
44+
}
45+
4246
const deviceBasePath = "api/private/unauth/account/device"
4347

4448
// RequestCode initiates the authorization flow by requesting a code.
@@ -149,6 +153,20 @@ func (c Config) RevokeToken(ctx context.Context, token, tokenTypeHint string) (*
149153
return c.Do(ctx, req, nil)
150154
}
151155

156+
// RegistrationConfig retrieves the config used for registration.
157+
func (c Config) RegistrationConfig(ctx context.Context) (*RegistrationConfig, *atlas.Response, error) {
158+
req, err := c.NewRequest(ctx, http.MethodGet, deviceBasePath+"/registration", url.Values{})
159+
if err != nil {
160+
return nil, nil, err
161+
}
162+
var rc *RegistrationConfig
163+
resp, err := c.Do(ctx, req, &rc)
164+
if err != nil {
165+
return nil, resp, err
166+
}
167+
return rc, resp, err
168+
}
169+
152170
func IsTimeoutErr(err error) bool {
153171
var target *atlas.ErrorResponse
154172
return errors.Is(err, ErrTimeout) || (errors.As(err, &target) && target.ErrorCode == authExpiredError)

auth/device_flow_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,31 @@ func TestConfig_RevokeToken(t *testing.T) {
181181
t.Fatalf("RequestCode returned error: %v", err)
182182
}
183183
}
184+
185+
func TestConfig_RegistrationConfig(t *testing.T) {
186+
config, mux, teardown := setup()
187+
defer teardown()
188+
189+
mux.HandleFunc("/api/private/unauth/account/device/registration", func(w http.ResponseWriter, r *http.Request) {
190+
if http.MethodGet != r.Method {
191+
t.Errorf("Request method = %v, expected %v", r.Method, http.MethodGet)
192+
}
193+
194+
fmt.Fprint(w, `{
195+
"registrationUrl": "http://localhost:8080/account/register/cli"
196+
}`)
197+
})
198+
199+
results, _, err := config.RegistrationConfig(ctx)
200+
if err != nil {
201+
t.Fatalf("RegistrationConfig returned error: %v", err)
202+
}
203+
204+
expected := &RegistrationConfig{
205+
RegistrationURL: "http://localhost:8080/account/register/cli",
206+
}
207+
208+
if diff := deep.Equal(results, expected); diff != nil {
209+
t.Error(diff)
210+
}
211+
}

0 commit comments

Comments
 (0)