Source File
pkce.go
Belonging Package
golang.org/x/oauth2
// 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 oauth2import ()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.// The resulting verifier should be passed to [Config.AuthCodeURL] or [Config.DeviceAuth]// with [S256ChallengeOption], and to [Config.Exchange] or [Config.DeviceAccessToken]// with [VerifierOption].func () 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:= make([]byte, 32)if , := rand.Read(); != nil {panic()}return base64.RawURLEncoding.EncodeToString()}// VerifierOption returns a PKCE code verifier [AuthCodeOption]. It should only be// passed to [Config.Exchange] or [Config.DeviceAccessToken].func ( string) AuthCodeOption {return setParam{k: codeVerifierKey, v: }}// S256ChallengeFromVerifier returns a PKCE code challenge derived from verifier with method S256.//// Prefer to use [S256ChallengeOption] where possible.func ( string) string {:= sha256.Sum256([]byte())return base64.RawURLEncoding.EncodeToString([:])}// S256ChallengeOption derives a PKCE code challenge derived from verifier with// method S256. It should be passed to [Config.AuthCodeURL] or [Config.DeviceAuth]// only.func ( string) AuthCodeOption {return challengeOption{challenge_method: "S256",challenge: S256ChallengeFromVerifier(),}}type challengeOption struct{ challenge_method, challenge string }func ( challengeOption) ( url.Values) {.Set(codeChallengeMethodKey, .challenge_method).Set(codeChallengeKey, .challenge)}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |