Skip to content

Commit 1ee9ded

Browse files
authored
Remove PKCE implementation from oauth2params package (#262)
* Replace PKCE with oauth2 Verifier * Fix comment
1 parent 6848862 commit 1ee9ded

File tree

4 files changed

+5
-83
lines changed

4 files changed

+5
-83
lines changed

example/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/int128/oauth2cli"
12-
"github.com/int128/oauth2cli/oauth2params"
1312
"github.com/pkg/browser"
1413
"golang.org/x/oauth2"
1514
"golang.org/x/sync/errgroup"
@@ -51,12 +50,9 @@ Then set the following options:`)
5150
log.Printf("Using the TLS certificate: %s", o.localServerCert)
5251
}
5352

54-
pkce, err := oauth2params.NewPKCE()
55-
if err != nil {
56-
log.Fatalf("error: %s", err)
57-
}
5853
ready := make(chan string, 1)
5954
defer close(ready)
55+
pkceVerifier := oauth2.GenerateVerifier()
6056
cfg := oauth2cli.Config{
6157
OAuth2Config: oauth2.Config{
6258
ClientID: o.clientID,
@@ -67,8 +63,8 @@ Then set the following options:`)
6763
},
6864
Scopes: strings.Split(o.scopes, ","),
6965
},
70-
AuthCodeOptions: pkce.AuthCodeOptions(),
71-
TokenRequestOptions: pkce.TokenRequestOptions(),
66+
AuthCodeOptions: []oauth2.AuthCodeOption{oauth2.S256ChallengeOption(pkceVerifier)},
67+
TokenRequestOptions: []oauth2.AuthCodeOption{oauth2.VerifierOption(pkceVerifier)},
7268
LocalServerReadyChan: ready,
7369
LocalServerCertFile: o.localServerCert,
7470
LocalServerKeyFile: o.localServerKey,

oauth2cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ type Config struct {
5454
OAuth2Config oauth2.Config
5555

5656
// Options for an authorization request.
57-
// You can set oauth2.AccessTypeOffline and the PKCE options here.
57+
// You can set oauth2.AccessTypeOffline or oauth2.S256ChallengeOption.
5858
AuthCodeOptions []oauth2.AuthCodeOption
5959
// Options for a token request.
60-
// You can set the PKCE options here.
60+
// You can set oauth2.VerifierOption.
6161
TokenRequestOptions []oauth2.AuthCodeOption
6262
// State parameter in the authorization request.
6363
// Default to a string of random 32 bytes.

oauth2params/params.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package oauth2params
33

44
import (
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-
6921
func random(bits int) ([]byte, error) {
7022
b := make([]byte, bits)
7123
if err := binary.Read(rand.Reader, binary.LittleEndian, b); err != nil {

oauth2params/params_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)