Skip to content

Commit 8322f7f

Browse files
committed
x/oauth2: make DeviceAuth retrieval populate RetrieveError
Endpoints may return errors when attempting to request device authorization. Currently, these error codes are ignored and an otherwise empty RetrieveError returned. This change populates the RetrieveError similar to the oauth2 token exchange. Fix golang/go#75759
1 parent 792c877 commit 8322f7f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

deviceauth.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"mime"
910
"net/http"
1011
"net/url"
1112
"strings"
@@ -116,10 +117,27 @@ func retrieveDeviceAuth(ctx context.Context, c *Config, v url.Values) (*DeviceAu
116117
return nil, fmt.Errorf("oauth2: cannot auth device: %v", err)
117118
}
118119
if code := r.StatusCode; code < 200 || code > 299 {
119-
return nil, &RetrieveError{
120+
retrieveError := &RetrieveError{
120121
Response: r,
121122
Body: body,
122123
}
124+
125+
content, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
126+
switch content {
127+
case "application/x-www-form-urlencoded", "text/plain":
128+
// some endpoints return a query string
129+
vals, err := url.ParseQuery(string(body))
130+
if err != nil {
131+
return nil, retrieveError
132+
}
133+
retrieveError.ErrorCode = vals.Get("error")
134+
retrieveError.ErrorDescription = vals.Get("error_description")
135+
retrieveError.ErrorURI = vals.Get("error_uri")
136+
default:
137+
json.Unmarshal(body, &retrieveError) // no error checks
138+
}
139+
140+
return nil, retrieveError
123141
}
124142

125143
da := &DeviceAuthResponse{}

0 commit comments

Comments
 (0)