@@ -3,12 +3,9 @@ package oauth2params
33
44import (
55 "crypto/rand"
6- "crypto/sha256"
76 "encoding/base64"
87 "encoding/binary"
98 "fmt"
10-
11- "golang.org/x/oauth2"
129)
1310
1411// NewState returns a state parameter.
@@ -21,51 +18,6 @@ func NewState() (string, error) {
2118 return base64URLEncode (b ), nil
2219}
2320
24- // PKCE represents a set of PKCE parameters.
25- // See https://tools.ietf.org/html/rfc7636.
26- type PKCE struct {
27- CodeChallenge string
28- CodeChallengeMethod string
29- CodeVerifier string
30- }
31-
32- // AuthCodeOptions returns options for oauth2.Config.AuthCodeURL().
33- func (pkce * PKCE ) AuthCodeOptions () []oauth2.AuthCodeOption {
34- return []oauth2.AuthCodeOption {
35- oauth2 .SetAuthURLParam ("code_challenge_method" , pkce .CodeChallengeMethod ),
36- oauth2 .SetAuthURLParam ("code_challenge" , pkce .CodeChallenge ),
37- }
38- }
39-
40- // TokenRequestOptions returns options for oauth2.Config.Exchange().
41- func (pkce * PKCE ) TokenRequestOptions () []oauth2.AuthCodeOption {
42- return []oauth2.AuthCodeOption {
43- oauth2 .SetAuthURLParam ("code_verifier" , pkce .CodeVerifier ),
44- }
45- }
46-
47- // NewPKCE returns a PKCE parameter.
48- // This generates 256 bits of random bytes.
49- func NewPKCE () (* PKCE , error ) {
50- b , err := random (32 )
51- if err != nil {
52- return nil , fmt .Errorf ("could not generate a random: %w" , err )
53- }
54- s := computeS256 (b )
55- return & s , nil
56- }
57-
58- func computeS256 (b []byte ) PKCE {
59- v := base64URLEncode (b )
60- s := sha256 .New ()
61- _ , _ = s .Write ([]byte (v ))
62- return PKCE {
63- CodeChallenge : base64URLEncode (s .Sum (nil )),
64- CodeChallengeMethod : "S256" ,
65- CodeVerifier : v ,
66- }
67- }
68-
6921func random (bits int ) ([]byte , error ) {
7022 b := make ([]byte , bits )
7123 if err := binary .Read (rand .Reader , binary .LittleEndian , b ); err != nil {
0 commit comments