-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #603 Fixes golang/go#59835 Change-Id: Ica0cfef975ba9511e00f097498d33ba27dafca0d GitHub-Last-Rev: f01f759 GitHub-Pull-Request: #625 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/463979 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Matt Hickford <matt.hickford@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org>
- Loading branch information
1 parent
e3fb0fb
commit 55cd552
Showing
3 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2023 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 oauth2 | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
codeChallengeKey = "code_challenge" | ||
codeChallengeMethodKey = "code_challenge_method" | ||
codeVerifierKey = "code_verifier" | ||
) | ||
|
||
// GenerateVerifier generates a PKCE code verifier with 32 octets of randomness. | ||
// This follows recommendations in RFC 7636. | ||
// | ||
// A fresh verifier should be generated for each authorization. | ||
// S256ChallengeOption(verifier) should then be passed to Config.AuthCodeURL | ||
// (or Config.DeviceAccess) and VerifierOption(verifier) to Config.Exchange | ||
// (or Config.DeviceAccessToken). | ||
func GenerateVerifier() string { | ||
// "RECOMMENDED that the output of a suitable random number generator be | ||
// used to create a 32-octet sequence. The octet sequence is then | ||
// base64url-encoded to produce a 43-octet URL-safe string to use as the | ||
// code verifier." | ||
// https://datatracker.ietf.org/doc/html/rfc7636#section-4.1 | ||
data := make([]byte, 32) | ||
if _, err := rand.Read(data); err != nil { | ||
panic(err) | ||
} | ||
return base64.RawURLEncoding.EncodeToString(data) | ||
} | ||
|
||
// VerifierOption returns a PKCE code verifier AuthCodeOption. It should be | ||
// passed to Config.Exchange or Config.DeviceAccessToken only. | ||
func VerifierOption(verifier string) AuthCodeOption { | ||
return setParam{k: codeVerifierKey, v: verifier} | ||
} | ||
|
||
// S256ChallengeFromVerifier returns a PKCE code challenge derived from verifier with method S256. | ||
// | ||
// Prefer to use S256ChallengeOption where possible. | ||
func S256ChallengeFromVerifier(verifier string) string { | ||
sha := sha256.Sum256([]byte(verifier)) | ||
return base64.RawURLEncoding.EncodeToString(sha[:]) | ||
} | ||
|
||
// S256ChallengeOption derives a PKCE code challenge derived from verifier with | ||
// method S256. It should be passed to Config.AuthCodeURL or Config.DeviceAccess | ||
// only. | ||
func S256ChallengeOption(verifier string) AuthCodeOption { | ||
return challengeOption{ | ||
challenge_method: "S256", | ||
challenge: S256ChallengeFromVerifier(verifier), | ||
} | ||
} | ||
|
||
type challengeOption struct{ challenge_method, challenge string } | ||
|
||
func (p challengeOption) setValue(m url.Values) { | ||
m.Set(codeChallengeMethodKey, p.challenge_method) | ||
m.Set(codeChallengeKey, p.challenge) | ||
} |