Skip to content

Commit 750649c

Browse files
GiteaBotlunny
andauthored
Fix oauth2 s256 (#36462) (#36477)
Backport #36462 by @lunny --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent eb95bbc commit 750649c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

routers/web/auth/oauth2_provider.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ func AuthorizeOAuth(ctx *context.Context) {
230230

231231
// pkce support
232232
switch form.CodeChallengeMethod {
233-
case "S256":
234-
case "plain":
233+
case "S256", "plain":
235234
if err := ctx.Session.Set("CodeChallengeMethod", form.CodeChallengeMethod); err != nil {
236235
handleAuthorizeError(ctx, AuthorizeError{
237236
ErrorCode: ErrorCodeServerError,

tests/integration/oauth_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/http"
1212
"net/http/httptest"
13+
"net/url"
1314
"strings"
1415
"testing"
1516

@@ -95,6 +96,45 @@ func TestAuthorizeShow(t *testing.T) {
9596
htmlDoc.GetCSRF()
9697
}
9798

99+
func TestAuthorizeGrantS256RequiresVerifier(t *testing.T) {
100+
defer tests.PrepareTestEnv(t)()
101+
ctx := loginUser(t, "user4")
102+
codeChallenge := "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg"
103+
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate&code_challenge_method=S256&code_challenge="+url.QueryEscape(codeChallenge))
104+
resp := ctx.MakeRequest(t, req, http.StatusOK)
105+
106+
htmlDoc := NewHTMLParser(t, resp.Body)
107+
AssertHTMLElement(t, htmlDoc, "#authorize-app", true)
108+
109+
grantReq := NewRequestWithValues(t, "POST", "/login/oauth/grant", map[string]string{
110+
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
111+
"state": "thestate",
112+
"scope": "",
113+
"nonce": "",
114+
"redirect_uri": "a",
115+
"granted": "true",
116+
"_csrf": htmlDoc.GetCSRF(),
117+
})
118+
grantResp := ctx.MakeRequest(t, grantReq, http.StatusSeeOther)
119+
u, err := grantResp.Result().Location()
120+
assert.NoError(t, err)
121+
code := u.Query().Get("code")
122+
assert.NotEmpty(t, code)
123+
124+
accessReq := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
125+
"grant_type": "authorization_code",
126+
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
127+
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
128+
"redirect_uri": "a",
129+
"code": code,
130+
})
131+
accessResp := MakeRequest(t, accessReq, http.StatusBadRequest)
132+
parsedError := new(oauth2_provider.AccessTokenError)
133+
assert.NoError(t, json.Unmarshal(accessResp.Body.Bytes(), parsedError))
134+
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
135+
assert.Equal(t, "failed PKCE code challenge", parsedError.ErrorDescription)
136+
}
137+
98138
func TestAuthorizeRedirectWithExistingGrant(t *testing.T) {
99139
defer tests.PrepareTestEnv(t)()
100140
req := NewRequest(t, "GET", "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=https%3A%2F%2Fexample.com%2Fxyzzy&response_type=code&state=thestate")

0 commit comments

Comments
 (0)