mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
These structs and funcs cannot be used by the end consumer. Unexporting them helps cleans up our documentation Change-Id: I2eadb69e87de912ac39f53e83cd9bdfe76a15e3e GitHub-Last-Rev: 60b58eef7558b7e1ccf7a07794668b0b25e99d9e GitHub-Pull-Request: golang/oauth2#479 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/293752 Reviewed-by: Cody Oss <codyoss@google.com> Trust: Cody Oss <codyoss@google.com> Trust: Tyler Bui-Palsulich <tbp@google.com> Run-TryBot: Cody Oss <codyoss@google.com> TryBot-Result: Go Bot <gobot@golang.org>
42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package externalaccount
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"golang.org/x/oauth2"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// clientAuthentication represents an OAuth client ID and secret and the mechanism for passing these credentials as stated in rfc6749#2.3.1.
|
|
type clientAuthentication struct {
|
|
// AuthStyle can be either basic or request-body
|
|
AuthStyle oauth2.AuthStyle
|
|
ClientID string
|
|
ClientSecret string
|
|
}
|
|
|
|
func (c *clientAuthentication) InjectAuthentication(values url.Values, headers http.Header) {
|
|
if c.ClientID == "" || c.ClientSecret == "" || values == nil || headers == nil {
|
|
return
|
|
}
|
|
|
|
switch c.AuthStyle {
|
|
case oauth2.AuthStyleInHeader: // AuthStyleInHeader corresponds to basic authentication as defined in rfc7617#2
|
|
plainHeader := c.ClientID + ":" + c.ClientSecret
|
|
headers.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(plainHeader)))
|
|
case oauth2.AuthStyleInParams: // AuthStyleInParams corresponds to request-body authentication with ClientID and ClientSecret in the message body.
|
|
values.Set("client_id", c.ClientID)
|
|
values.Set("client_secret", c.ClientSecret)
|
|
case oauth2.AuthStyleAutoDetect:
|
|
values.Set("client_id", c.ClientID)
|
|
values.Set("client_secret", c.ClientSecret)
|
|
default:
|
|
values.Set("client_id", c.ClientID)
|
|
values.Set("client_secret", c.ClientSecret)
|
|
}
|
|
}
|