Skip to content

Commit

Permalink
call ChallengeCode without VerifyCode
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaybabloo committed Mar 6, 2021
1 parent 333d50c commit 4587eb3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func main() {
Length: 128,
}

fmt.Println(p.VerifyCode())
fmt.Println(p.VerifyCode()) // This is optional
// Output: 45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914

fmt.Println(p.ChallengeCode())
// Output: iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U
}
```

> Note: You have to call `p.VerifyCode()` before `p.ChallengeCode()` to generate a random string and its hash
> Note: Calling `p.VerifyCode()` optional, but calling it after `p.ChallengeCode()` will reset `pkce.RandomString`
11 changes: 9 additions & 2 deletions pkce.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ func (p *Pkce) VerifyCode() (string, error) {
// ChallengeCode returns a challenge code as mentioned in https://tools.ietf.org/html/rfc7636#section-4.2.
// The code is based on
// code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
func (p *Pkce) ChallengeCode() string {
func (p *Pkce) ChallengeCode() (string, error) {
if p.RandomString == "" {
code, err := p.VerifyCode()
if err != nil {
return "", err
}
p.RandomString = code
}
hash := sha256.Sum256([]byte(p.RandomString))
toBase64 := base64.RawURLEncoding.EncodeToString(hash[:])
return toBase64
return toBase64, nil
}
37 changes: 34 additions & 3 deletions pkce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,30 @@ func TestPkce_ChallengeCode(t *testing.T) {
RandomString: "45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914",
}

assert.Equal(t, "iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U", p.ChallengeCode())
code, err := p.ChallengeCode()
if assert.Nil(t, err) {
assert.Equal(t, "iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U", code)
}
}

func TestPkce_ChallengeCode2(t *testing.T) {
p := Pkce{
Length: 50,
}

code, err := p.ChallengeCode()
if assert.Nil(t, err) {
assert.IsType(t, "", code)
}
}

func TestPkce_ChallengeCode3(t *testing.T) {
p := Pkce{}

_, err := p.ChallengeCode()
if assert.Error(t, err) {
assert.Equal(t, errors.New("length should be greater than 0"), err)
}
}

func TestPkce_VerifyCode(t *testing.T) {
Expand Down Expand Up @@ -103,7 +126,11 @@ func ExamplePkce_ChallengeCode() {
p := Pkce{
RandomString: "45d7820e694481f399e7fb9c444f0cb63301a7254d1401443835d9af2c9a6a5ec5b243c3470feb945336025964ef05c8d2f0e44baf76762ba6136914",
}
fmt.Println(p.ChallengeCode())
code, err := p.ChallengeCode()
if err != nil {
panic(err)
}
fmt.Println(code)
// Output: iQoF8w9kq5RnuMdisRXypyOoMCF7FGz-ro7dwHjC28U
}

Expand All @@ -115,5 +142,9 @@ func ExamplePkce_ChallengeCode_generatedString() {
if err != nil {
panic(err)
}
fmt.Println(code, p.ChallengeCode())
challengeCode, err := p.ChallengeCode()
if err != nil {
panic(err)
}
fmt.Println(code, challengeCode)
}

0 comments on commit 4587eb3

Please sign in to comment.