@@ -143,15 +143,17 @@ func SetAuthURLParam(key, value string) AuthCodeOption {
143
143
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
144
144
// that asks for permissions for the required scopes explicitly.
145
145
//
146
- // State is a token to protect the user from CSRF attacks. You must
147
- // always provide a non-empty string and validate that it matches the
148
- // state query parameter on your redirect callback.
149
- // See http://tools.ietf.org/html/rfc6749#section-10.12 for more info.
146
+ // An opaque value used by the client to maintain state between the request and
147
+ // callback. The authorization server includes this value when redirecting the
148
+ // user agent back to the client. See https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-09.html#section-4.1.1-8.12.1
150
149
//
151
150
// Opts may include AccessTypeOnline or AccessTypeOffline, as well
152
151
// as ApprovalForce.
153
- // It can also be used to pass the PKCE challenge.
154
- // See https://www.oauth.com/oauth2-servers/pkce/ for more info.
152
+ //
153
+ // To protect against CSRF attacks, opts should include a PKCE challenge
154
+ // (S256ChallengeOption). Not all servers support PKCE. An alternative is to
155
+ // generate a random state parameter and verify it after exchange.
156
+ // See https://www.ietf.org/archive/id/draft-ietf-oauth-v2-1-09.html#name-cross-site-request-forgery
155
157
func (c * Config ) AuthCodeURL (state string , opts ... AuthCodeOption ) string {
156
158
var buf bytes.Buffer
157
159
buf .WriteString (c .Endpoint .AuthURL )
@@ -166,7 +168,6 @@ func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string {
166
168
v .Set ("scope" , strings .Join (c .Scopes , " " ))
167
169
}
168
170
if state != "" {
169
- // TODO(light): Docs say never to omit state; don't allow empty.
170
171
v .Set ("state" , state )
171
172
}
172
173
for _ , opt := range opts {
@@ -211,10 +212,11 @@ func (c *Config) PasswordCredentialsToken(ctx context.Context, username, passwor
211
212
// The provided context optionally controls which HTTP client is used. See the HTTPClient variable.
212
213
//
213
214
// The code will be in the *http.Request.FormValue("code"). Before
214
- // calling Exchange, be sure to validate FormValue("state").
215
+ // calling Exchange, be sure to validate FormValue("state") if you are
216
+ // using it to protect against CSRF attacks.
215
217
//
216
- // Opts may include the PKCE verifier code if previously used in AuthCodeURL.
217
- // See https://www.oauth.com/oauth2-servers/pkce/ for more info .
218
+ // If using PKCE to protect against CSRF attacks, opts should include the
219
+ // verifier (VerifierOption) .
218
220
func (c * Config ) Exchange (ctx context.Context , code string , opts ... AuthCodeOption ) (* Token , error ) {
219
221
v := url.Values {
220
222
"grant_type" : {"authorization_code" },
0 commit comments