Skip to content

Commit eb74444

Browse files
maksim-zuev-dbIsaac
andcommitted
Share the device code support check with DeviceCodeChallenge
CheckDeviceCodeSupported fetched the OIDC endpoints with its own client and repeated the check DeviceCodeChallenge makes. Make it a PersistentAuth method that runs the same deviceCodeConfig as DeviceCodeChallenge, so the up-front check and the login cannot disagree. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 4fea011 commit eb74444

3 files changed

Lines changed: 39 additions & 44 deletions

File tree

‎cmd/auth/login_device_code.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ func validateDeviceCodeHost(ctx context.Context, authArguments *auth.AuthArgumen
4646
return errDeviceCodeWorkspaceOnly
4747
}
4848
}
49-
return u2m.CheckDeviceCodeSupported(ctx, (&config.Config{Host: probe.Host}).CanonicalHostName())
49+
arg, err := u2m.NewBasicWorkspaceOAuthArgument((&config.Config{Host: probe.Host}).CanonicalHostName())
50+
if err != nil {
51+
return err
52+
}
53+
persistentAuth, err := u2m.NewPersistentAuth(ctx, u2m.WithOAuthArgument(arg))
54+
if err != nil {
55+
return err
56+
}
57+
defer persistentAuth.Close()
58+
return persistentAuth.CheckDeviceCodeSupported()
5059
}
5160

5261
// validateDeviceCodeLogin returns an error if device code login is not enabled

‎libs/auth/u2m/device_code.go‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,29 @@ const maxConfirmationAttempts = 3
5050
// the user can still enter, including this one.
5151
type ConfirmFunc func(ctx context.Context, da DeviceAuthorization, lastErr error, attemptsLeft int) (string, error)
5252

53-
// CheckDeviceCodeSupported returns ErrDeviceCodeNotSupported if the workspace
54-
// does not advertise a device authorization endpoint in its OIDC discovery
55-
// document. Callers use it to fail before prompting the user for anything.
56-
func CheckDeviceCodeSupported(ctx context.Context, workspaceHost string) error {
57-
supplier := &BasicOAuthEndpointSupplier{Client: httpclient.NewApiClient(httpclient.ClientConfig{})}
58-
endpoints, err := supplier.GetWorkspaceOAuthEndpoints(ctx, workspaceHost)
53+
// CheckDeviceCodeSupported returns the error DeviceCodeChallenge would fail
54+
// with before contacting the device authorization endpoint, such as
55+
// ErrDeviceCodeNotSupported. Callers use it to fail before prompting the user
56+
// for anything.
57+
func (a *PersistentAuth) CheckDeviceCodeSupported() error {
58+
_, err := a.deviceCodeConfig()
59+
return err
60+
}
61+
62+
// deviceCodeConfig returns the OAuth config for device code login, or an
63+
// error if the host does not support it.
64+
func (a *PersistentAuth) deviceCodeConfig() (*oauth2.Config, error) {
65+
if _, ok := a.oAuthArgument.(WorkspaceOAuthArgument); !ok {
66+
return nil, errors.New("device code login is only supported for workspace hosts")
67+
}
68+
cfg, err := a.oauth2Config()
5969
if err != nil {
60-
return fmt.Errorf("fetching OAuth endpoints: %w", err)
70+
return nil, fmt.Errorf("fetching oauth config: %w", err)
6171
}
62-
if endpoints.DeviceAuthorizationEndpoint == "" {
63-
return ErrDeviceCodeNotSupported
72+
if cfg.Endpoint.DeviceAuthURL == "" {
73+
return nil, ErrDeviceCodeNotSupported
6474
}
65-
return nil
75+
return cfg, nil
6676
}
6777

6878
// DeviceCodeChallenge logs in using the Databricks variant of the RFC 8628
@@ -73,15 +83,9 @@ func CheckDeviceCodeSupported(ctx context.Context, workspaceHost string) error {
7383
// it up to maxConfirmationAttempts times. Only workspace hosts are supported.
7484
// The caller is responsible for storing the returned token.
7585
func (a *PersistentAuth) DeviceCodeChallenge(confirm ConfirmFunc) (*oauth2.Token, error) {
76-
if _, ok := a.oAuthArgument.(WorkspaceOAuthArgument); !ok {
77-
return nil, errors.New("device code login is only supported for workspace hosts")
78-
}
79-
cfg, err := a.oauth2Config()
86+
cfg, err := a.deviceCodeConfig()
8087
if err != nil {
81-
return nil, fmt.Errorf("fetching oauth config: %w", err)
82-
}
83-
if cfg.Endpoint.DeviceAuthURL == "" {
84-
return nil, ErrDeviceCodeNotSupported
88+
return nil, err
8589
}
8690

8791
ctx := a.setOAuthContext(a.ctx)

‎libs/auth/u2m/device_code_test.go‎

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -325,35 +325,17 @@ func TestNormalizeCode(t *testing.T) {
325325

326326
func TestCheckDeviceCodeSupported(t *testing.T) {
327327
tests := []struct {
328-
name string
329-
body string
330-
wantErr string
328+
name string
329+
advertise bool
330+
wantErr error
331331
}{
332-
{
333-
name: "advertised",
334-
body: `{"token_endpoint": "https://workspace.test/oidc/v1/token", "device_authorization_endpoint": "https://workspace.test/oidc/v1/device/auth"}`,
335-
},
336-
{
337-
name: "not advertised",
338-
body: `{"token_endpoint": "https://workspace.test/oidc/v1/token"}`,
339-
wantErr: "device code login is not enabled for this workspace",
340-
},
332+
{name: "advertised", advertise: true},
333+
{name: "not advertised", wantErr: ErrDeviceCodeNotSupported},
341334
}
342335
for _, tc := range tests {
343336
t.Run(tc.name, func(t *testing.T) {
344-
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
345-
assert.Equal(t, "/oidc/.well-known/oauth-authorization-server", r.URL.Path)
346-
w.Header().Set("Content-Type", "application/json")
347-
_, _ = w.Write([]byte(tc.body))
348-
}))
349-
defer srv.Close()
350-
351-
err := CheckDeviceCodeSupported(t.Context(), srv.URL)
352-
if tc.wantErr != "" {
353-
assert.EqualError(t, err, tc.wantErr)
354-
} else {
355-
assert.NoError(t, err)
356-
}
337+
p := newDeviceCodeAuth(t, &fakeDeviceServer{}, tc.advertise)
338+
assert.ErrorIs(t, p.CheckDeviceCodeSupported(), tc.wantErr)
357339
})
358340
}
359341
}

0 commit comments

Comments
 (0)