Skip to content

Commit d7146d6

Browse files
committed
Fix preserving existing query parameters in redirect_uri
1 parent 07a6b19 commit d7146d6

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

response.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,16 @@ func (r *Response) GetRedirectUrl() (string, error) {
117117
return "", err
118118
}
119119

120+
var q url.Values
121+
if r.RedirectInFragment {
122+
// start with empty set for fragment
123+
q = url.Values{}
124+
} else {
125+
// add parameters to existing query
126+
q = u.Query()
127+
}
128+
120129
// add parameters
121-
q := u.Query()
122130
for n, v := range r.Output {
123131
q.Set(n, fmt.Sprint(v))
124132
}
@@ -127,7 +135,6 @@ func (r *Response) GetRedirectUrl() (string, error) {
127135
// Fragment should be encoded as application/x-www-form-urlencoded (%-escaped, spaces are represented as '+')
128136
// The stdlib URL#String() doesn't make that easy to accomplish, so build this ourselves
129137
if r.RedirectInFragment {
130-
u.RawQuery = ""
131138
u.Fragment = ""
132139
redirectURI := u.String() + "#" + q.Encode()
133140
return redirectURI, nil

response_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ func TestGetRedirectUrl(t *testing.T) {
1818
ExpectedURL string
1919
}{
2020
"query": {
21-
URL: "https://foo.com/path",
21+
URL: "https://foo.com/path?abc=123",
2222
Output: ResponseData{"access_token": "12345", "state": state},
23-
ExpectedURL: "https://foo.com/path?access_token=12345&state=%7B%22then%22%3A+%22%2Findex.html%3Fa%3D1%26b%3D%252B%23fragment%22%2C+%22nonce%22%3A+%22014f%3Abff9a07c%22%7D",
23+
ExpectedURL: "https://foo.com/path?abc=123&access_token=12345&state=%7B%22then%22%3A+%22%2Findex.html%3Fa%3D1%26b%3D%252B%23fragment%22%2C+%22nonce%22%3A+%22014f%3Abff9a07c%22%7D",
2424
},
2525

2626
// https://tools.ietf.org/html/rfc6749#section-4.2.2
2727
// Fragment should be encoded as application/x-www-form-urlencoded (%-escaped, spaces are represented as '+')
2828
"fragment": {
29-
URL: "https://foo.com/path",
29+
URL: "https://foo.com/path?abc=123",
3030
Output: ResponseData{"access_token": "12345", "state": state},
3131
RedirectInFragment: true,
32-
ExpectedURL: "https://foo.com/path#access_token=12345&state=%7B%22then%22%3A+%22%2Findex.html%3Fa%3D1%26b%3D%252B%23fragment%22%2C+%22nonce%22%3A+%22014f%3Abff9a07c%22%7D",
32+
ExpectedURL: "https://foo.com/path?abc=123#access_token=12345&state=%7B%22then%22%3A+%22%2Findex.html%3Fa%3D1%26b%3D%252B%23fragment%22%2C+%22nonce%22%3A+%22014f%3Abff9a07c%22%7D",
3333
},
3434
}
3535

0 commit comments

Comments
 (0)