Skip to content

Commit 7b9faad

Browse files
committed
better challenge validation
- removing padding before comparing base64(sha256) results - plaintest string to 43 chars
1 parent e543c8e commit 7b9faad

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

const.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package oauth2
33
import (
44
"crypto/sha256"
55
"encoding/base64"
6+
"strings"
67
)
78

89
// ResponseType the type of authorization request
@@ -65,7 +66,10 @@ func (ccm CodeChallengeMethod) Validate(cc, ver string) bool {
6566
return cc == ver
6667
case CodeChallengeS256:
6768
s256 := sha256.Sum256([]byte(ver))
68-
return base64.URLEncoding.EncodeToString(s256[:]) == cc
69+
// trim padding
70+
a := strings.TrimRight(base64.URLEncoding.EncodeToString(s256[:]), "=")
71+
b := strings.TrimRight(cc, "=")
72+
return a == b
6973
default:
7074
return false
7175
}

const_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ func TestValidateS256(t *testing.T) {
1919
t.Fatal("not valid")
2020
}
2121
}
22+
23+
func TestValidateS256NoPadding(t *testing.T) {
24+
cc := oauth2.CodeChallengeS256
25+
if !cc.Validate("W6YWc_4yHwYN-cGDgGmOMHF3l7KDy7VcRjf7q2FVF-o", "s256test") {
26+
t.Fatal("not valid")
27+
}
28+
}

server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
clientID = "111111"
2525
clientSecret = "11111111"
2626

27-
plainChallenge = "plaintest"
27+
plainChallenge = "ThisIsAFourtyThreeCharactersLongStringThing"
2828
s256Challenge = "s256test"
2929
// echo s256test | sha256 | base64 | tr '/+' '_-'
3030
s256ChallengeHash = "W6YWc_4yHwYN-cGDgGmOMHF3l7KDy7VcRjf7q2FVF-o="

0 commit comments

Comments
 (0)