Skip to content

Commit 06bbb81

Browse files
authored
Handle optional fields of the Device Authorization Response. (#133)
1 parent 1a78abe commit 06bbb81

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

cmd/oauth2_device.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ func (c *OAuth2Cmd) DeviceGrantFlow(clientConfig oauth2.ClientConfig, serverConf
3131

3232
LogRequestAndResponse(authorizationRequest, authorizationResponse)
3333

34-
Logfln("\nGo to the following URL:\n\n%s", authorizationResponse.VerificationURIComplete)
34+
verificationUri := authorizationResponse.VerificationURI
35+
if authorizationResponse.VerificationURIComplete != nil {
36+
verificationUri = *authorizationResponse.VerificationURIComplete
37+
}
38+
39+
Logfln("\nGo to the following URL:\n\n%s", verificationUri)
3540

3641
if !clientConfig.NoBrowser {
3742
Logfln("\nOpening browser...")
38-
if err = browser.OpenURL(authorizationResponse.VerificationURIComplete); err != nil {
43+
if err = browser.OpenURL(verificationUri); err != nil {
3944
LogError(err)
4045
}
4146
}
@@ -45,7 +50,11 @@ func (c *OAuth2Cmd) DeviceGrantFlow(clientConfig oauth2.ClientConfig, serverConf
4550
// polling
4651
tokenStatus := LogAction("Waiting for token. Go to the browser to authenticate...")
4752

48-
ticker := time.NewTicker(time.Duration(authorizationResponse.Interval) * time.Second)
53+
interval := 5 * time.Second
54+
if authorizationResponse.Interval != nil {
55+
interval = time.Duration(*authorizationResponse.Interval) * time.Second
56+
}
57+
ticker := time.NewTicker(interval)
4958
done := make(chan error)
5059

5160
go func() {

internal/oauth2/oauth2_device.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
)
1313

1414
type DeviceAuthorizationResponse struct {
15-
DeviceCode string `json:"device_code"`
16-
UserCode string `json:"user_code"`
17-
VerificationURI string `json:"verification_uri"`
18-
VerificationURIComplete string `json:"verification_uri_complete"`
19-
ExpiresIn int64 `json:"expires_in"`
20-
Interval int64 `json:"interval"`
15+
DeviceCode string `json:"device_code"`
16+
UserCode string `json:"user_code"`
17+
VerificationURI string `json:"verification_uri"`
18+
VerificationURIComplete *string `json:"verification_uri_complete"`
19+
ExpiresIn int64 `json:"expires_in"`
20+
Interval *int64 `json:"interval"`
2121
}
2222

2323
func RequestDeviceAuthorization(ctx context.Context, cconfig ClientConfig, sconfig ServerConfig, hc *http.Client) (request Request, response DeviceAuthorizationResponse, err error) {

0 commit comments

Comments
 (0)