Skip to content

Commit d2a14ae

Browse files
author
Malte Isberner
committed
Allow making raw HTTP POST requests to the authz server
1 parent 08078c5 commit d2a14ae

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

internal/token.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func cloneURLValues(v url.Values) url.Values {
185185
return v2
186186
}
187187

188-
func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string, v url.Values, authStyle AuthStyle) (*Token, error) {
188+
func PostRawRequest(ctx context.Context, clientID, clientSecret, tokenURL string, v url.Values, authStyle AuthStyle) (*http.Response, error) {
189189
needsAuthStyleProbe := authStyle == 0
190190
if needsAuthStyleProbe {
191191
if style, ok := lookupAuthStyle(tokenURL); ok {
@@ -199,8 +199,11 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
199199
if err != nil {
200200
return nil, err
201201
}
202-
token, err := doTokenRoundTrip(ctx, req)
203-
if err != nil && needsAuthStyleProbe {
202+
resp, err := ctxhttp.Do(ctx, ContextClient(ctx), req)
203+
if err != nil {
204+
return nil, err // transport errors are not related to auth style
205+
}
206+
if resp.StatusCode >= 400 && resp.StatusCode <= 499 && needsAuthStyleProbe {
204207
// If we get an error, assume the server wants the
205208
// clientID & clientSecret in a different form.
206209
// See https://code.google.com/p/goauth2/issues/detail?id=31 for background.
@@ -215,24 +218,27 @@ func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string,
215218
// So just try both ways.
216219
authStyle = AuthStyleInParams // the second way we'll try
217220
req, _ = newTokenRequest(tokenURL, clientID, clientSecret, v, authStyle)
218-
token, err = doTokenRoundTrip(ctx, req)
221+
resp, err = ctxhttp.Do(ctx, ContextClient(ctx), req)
219222
}
220-
if needsAuthStyleProbe && err == nil {
223+
if needsAuthStyleProbe && err == nil && (resp.StatusCode < 400 || resp.StatusCode > 499) {
221224
setAuthStyle(tokenURL, authStyle)
222225
}
223-
// Don't overwrite `RefreshToken` with an empty value
224-
// if this was a token refreshing request.
226+
return resp, err
227+
}
228+
229+
func RetrieveToken(ctx context.Context, clientID, clientSecret, tokenURL string, v url.Values, authStyle AuthStyle) (*Token, error) {
230+
resp, err := PostRawRequest(ctx, clientID, clientSecret, tokenURL, v, authStyle)
231+
if err != nil {
232+
return nil, err
233+
}
234+
token, err := parseTokenResponse(resp)
225235
if token != nil && token.RefreshToken == "" {
226236
token.RefreshToken = v.Get("refresh_token")
227237
}
228238
return token, err
229239
}
230240

231-
func doTokenRoundTrip(ctx context.Context, req *http.Request) (*Token, error) {
232-
r, err := ctxhttp.Do(ctx, ContextClient(ctx), req)
233-
if err != nil {
234-
return nil, err
235-
}
241+
func parseTokenResponse(r *http.Response) (*Token, error) {
236242
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1<<20))
237243
r.Body.Close()
238244
if err != nil {

oauth2.go

+12
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ func (c *Config) Exchange(ctx context.Context, code string, opts ...AuthCodeOpti
224224
return retrieveToken(ctx, c, v)
225225
}
226226

227+
// PostRawRequest makes a raw HTTP POST request to the given endpoint, which is assumed to be an endpoint of the
228+
// authorization server.
229+
//
230+
// This can be used to invoke endpoints which are non-standard, or not otherwise supported by this library (e.g., token
231+
// revocation), ensuring the use of a consistent logic for, e.g., automatically inferring authentication styles.
232+
//
233+
// Note that a non-2xx response is passed through as-is, without setting an error value. Nothing is ever read from the
234+
// response body.
235+
func (c *Config) PostRawRequest(ctx context.Context, endpoint string, v url.Values) (*http.Response, error) {
236+
return internal.PostRawRequest(ctx, c.ClientID, c.ClientSecret, endpoint, v, internal.AuthStyle(c.Endpoint.AuthStyle))
237+
}
238+
227239
// Client returns an HTTP client using the provided token.
228240
// The token will auto-refresh as necessary. The underlying
229241
// HTTP transport will be obtained using the provided context.

0 commit comments

Comments
 (0)