Skip to content

Commit 10c1a3a

Browse files
authored
Merge pull request #36 from LyricTian/develop
fixed authorization request
2 parents d0d84bf + 8cee0c6 commit 10c1a3a

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func main() {
5757

5858
srv := server.NewDefaultServer(manager)
5959
srv.SetAllowGetAccessRequest(true)
60+
srv.SetClientInfoHandler(server.ClientFormHandler)
6061

6162
srv.SetInternalErrorHandler(func(err error) {
6263
log.Println("OAuth2 Error:", err.Error())
@@ -75,6 +76,7 @@ func main() {
7576

7677
http.ListenAndServe(":9096", nil)
7778
}
79+
7880
```
7981

8082
### Build and run
@@ -130,8 +132,8 @@ Copyright (c) 2016 Lyric
130132
[License-Image]: https://img.shields.io/npm/l/express.svg
131133
[Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2
132134
[Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
133-
[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.5.0
134-
[Release-image]: http://img.shields.io/badge/release-v3.5.0-1eb0fc.svg
135+
[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.5.1
136+
[Release-image]: http://img.shields.io/badge/release-v3.5.1-1eb0fc.svg
135137
[ReportCard-Url]: https://goreportcard.com/report/gopkg.in/oauth2.v3
136138
[ReportCard-Image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3
137139
[GoDoc-Url]: https://godoc.org/gopkg.in/oauth2.v3

doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// OAuth 2.0 server library for the Go programming language
2+
//
23
// package main
34
// import (
45
// "net/http"

example/client/client.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"log"
66
"net/http"
77
"net/url"
8+
"strings"
89
)
910

1011
const (
1112
redirectURI = "http://localhost:9094/oauth2"
1213
serverURI = "http://localhost:9096"
14+
clientID = "222222"
1315
)
1416

1517
func main() {
@@ -20,7 +22,7 @@ func main() {
2022
}
2123
q := u.Query()
2224
q.Add("response_type", "code")
23-
q.Add("client_id", "222222")
25+
q.Add("client_id", clientID)
2426
q.Add("scope", "all")
2527
q.Add("state", "xyz")
2628
q.Add("redirect_uri", url.QueryEscape(redirectURI))
@@ -44,9 +46,15 @@ func main() {
4446
uv.Add("code", code)
4547
uv.Add("redirect_uri", redirectURI)
4648
uv.Add("grant_type", "authorization_code")
47-
uv.Add("client_id", "222222")
48-
uv.Add("client_secret", "22222222")
49-
resp, err := http.PostForm(serverURI+"/token", uv)
49+
uv.Add("client_id", clientID)
50+
req, err := http.NewRequest(http.MethodPost, serverURI+"/token", strings.NewReader(uv.Encode()))
51+
if err != nil {
52+
http.Error(w, err.Error(), http.StatusInternalServerError)
53+
return
54+
}
55+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
56+
req.SetBasicAuth(clientID, "22222222")
57+
resp, err := http.DefaultClient.Do(req)
5058
if err != nil {
5159
http.Error(w, err.Error(), http.StatusInternalServerError)
5260
return

server/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewServer(cfg *Config, manager oauth2.Manager) *Server {
2626
Manager: manager,
2727
}
2828
// default handler
29-
srv.ClientInfoHandler = ClientFormHandler
29+
srv.ClientInfoHandler = ClientBasicHandler
3030
srv.UserAuthorizationHandler = func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
3131
err = errors.ErrAccessDenied
3232
return
@@ -292,6 +292,9 @@ func (s *Server) ValidationTokenRequest(r *http.Request) (gt oauth2.GrantType, t
292292
if tgr.RedirectURI == "" ||
293293
tgr.Code == "" {
294294
err = errors.ErrInvalidRequest
295+
return
296+
} else if cid := r.FormValue("client_id"); cid == "" || cid != clientID {
297+
err = errors.ErrInvalidClient
295298
}
296299
case oauth2.PasswordCredentials:
297300
tgr.Scope = r.FormValue("scope")

server/server_config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

3-
import oauth2 "gopkg.in/oauth2.v3"
3+
import (
4+
"gopkg.in/oauth2.v3"
5+
)
46

57
// SetTokenType token type
68
func (s *Server) SetTokenType(tokenType string) {

server/server_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestAuthorizeCode(t *testing.T) {
7373
WithFormField("code", code).
7474
WithFormField("grant_type", "authorization_code").
7575
WithFormField("client_id", clientID).
76-
WithFormField("client_secret", clientSecret).
76+
WithBasicAuth(clientID, clientSecret).
7777
Expect().
7878
Status(http.StatusOK).
7979
JSON().Raw()
@@ -145,11 +145,10 @@ func TestPasswordCredentials(t *testing.T) {
145145

146146
val := e.POST("/token").
147147
WithFormField("grant_type", "password").
148-
WithFormField("client_id", clientID).
149-
WithFormField("client_secret", clientSecret).
150148
WithFormField("username", "admin").
151149
WithFormField("password", "123456").
152150
WithFormField("scope", "all").
151+
WithBasicAuth(clientID, clientSecret).
153152
Expect().
154153
Status(http.StatusOK).
155154
JSON().Raw()
@@ -169,9 +168,8 @@ func TestClientCredentials(t *testing.T) {
169168

170169
val := e.POST("/token").
171170
WithFormField("grant_type", "client_credentials").
172-
WithFormField("client_id", clientID).
173-
WithFormField("client_secret", clientSecret).
174171
WithFormField("scope", "all").
172+
WithBasicAuth(clientID, clientSecret).
175173
Expect().
176174
Status(http.StatusOK).
177175
JSON().Raw()
@@ -200,7 +198,7 @@ func TestRefreshing(t *testing.T) {
200198
WithFormField("code", code).
201199
WithFormField("grant_type", "authorization_code").
202200
WithFormField("client_id", clientID).
203-
WithFormField("client_secret", clientSecret).
201+
WithBasicAuth(clientID, clientSecret).
204202
Expect().
205203
Status(http.StatusOK).
206204
JSON()
@@ -210,10 +208,9 @@ func TestRefreshing(t *testing.T) {
210208
refresh := jval.Object().Value("refresh_token").String().Raw()
211209
rval := e.POST("/token").
212210
WithFormField("grant_type", "refresh_token").
213-
WithFormField("client_id", clientID).
214-
WithFormField("client_secret", clientSecret).
215211
WithFormField("scope", "one").
216212
WithFormField("refresh_token", refresh).
213+
WithBasicAuth(clientID, clientSecret).
217214
Expect().
218215
Status(http.StatusOK).
219216
JSON().Raw()

0 commit comments

Comments
 (0)