Skip to content

Commit 5f468fa

Browse files
authored
Merge pull request #30 from LyricTian/develop
repair the details
2 parents 0f88327 + 009a015 commit 5f468fa

File tree

5 files changed

+93
-51
lines changed

5 files changed

+93
-51
lines changed

README.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
> An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.
44
5-
[![License][License-Image]][License-Url]
6-
[![ReportCard][ReportCard-Image]][ReportCard-Url]
7-
[![Build][Build-Status-Image]][Build-Status-Url]
8-
[![GoDoc][GoDoc-Image]][GoDoc-Url]
9-
[![Release][Release-Image]][Release-Url]
5+
[![License][License-Image]][License-Url] [![ReportCard][ReportCard-Image]][ReportCard-Url] [![Build][Build-Status-Image]][Build-Status-Url] [![GoDoc][GoDoc-Image]][GoDoc-Url] [![Release][Release-Image]][Release-Url]
106

117
## Protocol Flow
128

@@ -44,7 +40,7 @@ $ go get -u gopkg.in/oauth2.v3/...
4440
package main
4541

4642
import (
47-
"fmt"
43+
"log"
4844
"net/http"
4945

5046
"gopkg.in/oauth2.v3/manage"
@@ -63,7 +59,7 @@ func main() {
6359
srv.SetAllowGetAccessRequest(true)
6460

6561
srv.SetInternalErrorHandler(func(err error) {
66-
fmt.Println("OAuth2 Error:",err.Error())
62+
log.Println("OAuth2 Error:", err.Error())
6763
})
6864

6965
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
@@ -74,10 +70,7 @@ func main() {
7470
})
7571

7672
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
77-
err := srv.HandleTokenRequest(w, r)
78-
if err != nil {
79-
http.Error(w, err.Error(), http.StatusInternalServerError)
80-
}
73+
srv.HandleTokenRequest(w, r)
8174
})
8275

8376
http.ListenAndServe(":9096", nil)
@@ -94,14 +87,14 @@ $ ./server
9487
### Open in your web browser
9588

9689
```
97-
http://localhost:9096/token?grant_type=clientcredentials&client_id=1&client_secret=11&scope=all
90+
http://localhost:9096/token?grant_type=client_credentials&client_id=1&client_secret=11&scope=read
9891
```
9992

100-
```
93+
``` json
10194
{
102-
"access_token": "ZGF4ARHJPT2Y_QAIOJVL-Q",
95+
"access_token": "ACPT7UYYNVWS2OAPFOHVUW",
10396
"expires_in": 7200,
104-
"scope": "all",
97+
"scope": "read",
10598
"token_type": "Bearer"
10699
}
107100
```

const.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ const (
1010
)
1111

1212
func (rt ResponseType) String() string {
13-
return string(rt)
13+
if rt == Code ||
14+
rt == Token {
15+
return string(rt)
16+
}
17+
return ""
1418
}
1519

1620
// GrantType authorization model
@@ -20,11 +24,17 @@ type GrantType string
2024
const (
2125
AuthorizationCode GrantType = "authorization_code"
2226
PasswordCredentials GrantType = "password"
23-
ClientCredentials GrantType = "clientcredentials"
24-
Refreshing GrantType = "refreshtoken"
27+
ClientCredentials GrantType = "client_credentials"
28+
Refreshing GrantType = "refresh_token"
2529
Implicit GrantType = "__implicit"
2630
)
2731

2832
func (gt GrantType) String() string {
29-
return string(gt)
33+
if gt == AuthorizationCode ||
34+
gt == PasswordCredentials ||
35+
gt == ClientCredentials ||
36+
gt == Refreshing {
37+
return string(gt)
38+
}
39+
return ""
3040
}

doc.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
OAuth 2.0 server library for the Go programming language
3+
4+
package main
5+
6+
import (
7+
"net/http"
8+
9+
"gopkg.in/oauth2.v3/manage"
10+
"gopkg.in/oauth2.v3/server"
11+
"gopkg.in/oauth2.v3/store"
12+
)
13+
14+
func main() {
15+
manager := manage.NewDefaultManager()
16+
manager.MustTokenStorage(store.NewMemoryTokenStore())
17+
manager.MapClientStorage(store.NewTestClientStore())
18+
19+
srv := server.NewDefaultServer(manager)
20+
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
21+
srv.HandleAuthorizeRequest(w, r)
22+
})
23+
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
24+
srv.HandleTokenRequest(w, r)
25+
})
26+
27+
http.ListenAndServe(":9096", nil)
28+
}
29+
30+
*/
31+
32+
package oauth2

server/server.go

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,6 @@ func (s *Server) GetRedirectURI(req *AuthorizeRequest, data map[string]interface
121121
return
122122
}
123123

124-
// ValidationAuthorizeRequest the authorization request validation
125-
func (s *Server) ValidationAuthorizeRequest(r *http.Request) (req *AuthorizeRequest, err error) {
126-
if r.Method != "GET" {
127-
err = errors.ErrInvalidRequest
128-
return
129-
}
130-
redirectURI, err := url.QueryUnescape(r.FormValue("redirect_uri"))
131-
if err != nil {
132-
return
133-
}
134-
req = &AuthorizeRequest{
135-
RedirectURI: redirectURI,
136-
ResponseType: oauth2.ResponseType(r.FormValue("response_type")),
137-
ClientID: r.FormValue("client_id"),
138-
State: r.FormValue("state"),
139-
Scope: r.FormValue("scope"),
140-
}
141-
return
142-
}
143-
144124
// CheckResponseType check allows response type
145125
func (s *Server) CheckResponseType(rt oauth2.ResponseType) bool {
146126
for _, art := range s.Config.AllowedResponseTypes {
@@ -151,20 +131,41 @@ func (s *Server) CheckResponseType(rt oauth2.ResponseType) bool {
151131
return false
152132
}
153133

154-
// GetAuthorizeToken get authorization token(code)
155-
func (s *Server) GetAuthorizeToken(req *AuthorizeRequest) (ti oauth2.TokenInfo, err error) {
156-
if req.ResponseType == "" {
157-
err = errors.ErrUnsupportedResponseType
134+
// ValidationAuthorizeRequest the authorization request validation
135+
func (s *Server) ValidationAuthorizeRequest(r *http.Request) (req *AuthorizeRequest, err error) {
136+
redirectURI, err := url.QueryUnescape(r.FormValue("redirect_uri"))
137+
if err != nil {
158138
return
159-
} else if req.RedirectURI == "" ||
160-
req.ClientID == "" {
139+
}
140+
clientID := r.FormValue("client_id")
141+
if r.Method != "GET" ||
142+
clientID == "" ||
143+
redirectURI == "" {
161144
err = errors.ErrInvalidRequest
162145
return
163146
}
164-
if allowed := s.CheckResponseType(req.ResponseType); !allowed {
147+
148+
resType := oauth2.ResponseType(r.FormValue("response_type"))
149+
if resType.String() == "" {
150+
err = errors.ErrUnsupportedResponseType
151+
return
152+
} else if allowed := s.CheckResponseType(resType); !allowed {
165153
err = errors.ErrUnauthorizedClient
166154
return
167155
}
156+
157+
req = &AuthorizeRequest{
158+
RedirectURI: redirectURI,
159+
ResponseType: resType,
160+
ClientID: clientID,
161+
State: r.FormValue("state"),
162+
Scope: r.FormValue("scope"),
163+
}
164+
return
165+
}
166+
167+
// GetAuthorizeToken get authorization token(code)
168+
func (s *Server) GetAuthorizeToken(req *AuthorizeRequest) (ti oauth2.TokenInfo, err error) {
168169
// check the client allows the grant type
169170
if fn := s.ClientAuthorizedHandler; fn != nil {
170171
gt := oauth2.AuthorizationCode
@@ -180,6 +181,7 @@ func (s *Server) GetAuthorizeToken(req *AuthorizeRequest) (ti oauth2.TokenInfo,
180181
return
181182
}
182183
}
184+
183185
// check the client allows the authorized scope
184186
if fn := s.ClientScopeHandler; fn != nil {
185187
allowed, verr := fn(req.ClientID, req.Scope)
@@ -191,6 +193,7 @@ func (s *Server) GetAuthorizeToken(req *AuthorizeRequest) (ti oauth2.TokenInfo,
191193
return
192194
}
193195
}
196+
194197
tgr := &oauth2.TokenGenerateRequest{
195198
ClientID: req.ClientID,
196199
UserID: req.UserID,
@@ -221,6 +224,7 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
221224
err = s.redirectError(w, req, verr)
222225
return
223226
}
227+
224228
// user authorization
225229
userID, verr := s.UserAuthorizationHandler(w, r)
226230
if verr != nil {
@@ -230,6 +234,7 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
230234
return
231235
}
232236
req.UserID = userID
237+
233238
// specify the scope of authorization
234239
if fn := s.AuthorizeScopeHandler; fn != nil {
235240
scope, verr := fn(w, r)
@@ -240,6 +245,7 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
240245
req.Scope = scope
241246
}
242247
}
248+
243249
// specify the expiration time of access token
244250
if fn := s.AccessTokenExpHandler; fn != nil {
245251
exp, verr := fn(w, r)
@@ -249,6 +255,7 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
249255
}
250256
req.AccessTokenExp = exp
251257
}
258+
252259
ti, verr := s.GetAuthorizeToken(req)
253260
if verr != nil {
254261
err = s.redirectError(w, req, verr)
@@ -260,12 +267,13 @@ func (s *Server) HandleAuthorizeRequest(w http.ResponseWriter, r *http.Request)
260267

261268
// ValidationTokenRequest the token request validation
262269
func (s *Server) ValidationTokenRequest(r *http.Request) (gt oauth2.GrantType, tgr *oauth2.TokenGenerateRequest, err error) {
263-
if v := r.Method; !(v == "POST" || (s.Config.AllowGetAccessRequest && v == "GET")) {
270+
if v := r.Method; !(v == "POST" ||
271+
(s.Config.AllowGetAccessRequest && v == "GET")) {
264272
err = errors.ErrInvalidRequest
265273
return
266274
}
267275
gt = oauth2.GrantType(r.FormValue("grant_type"))
268-
if gt == "" {
276+
if gt.String() == "" {
269277
err = errors.ErrUnsupportedGrantType
270278
return
271279
}

server/server_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
"github.com/gavv/httpexpect"
11-
1211
"gopkg.in/oauth2.v3"
1312
"gopkg.in/oauth2.v3/manage"
1413
"gopkg.in/oauth2.v3/models"
@@ -169,7 +168,7 @@ func TestClientCredentials(t *testing.T) {
169168
srv = server.NewDefaultServer(manager)
170169

171170
val := e.POST("/token").
172-
WithFormField("grant_type", "clientcredentials").
171+
WithFormField("grant_type", "client_credentials").
173172
WithFormField("client_id", clientID).
174173
WithFormField("client_secret", clientSecret).
175174
WithFormField("scope", "all").
@@ -210,7 +209,7 @@ func TestRefreshing(t *testing.T) {
210209

211210
refresh := jval.Object().Value("refresh_token").String().Raw()
212211
rval := e.POST("/token").
213-
WithFormField("grant_type", "refreshtoken").
212+
WithFormField("grant_type", "refresh_token").
214213
WithFormField("client_id", clientID).
215214
WithFormField("client_secret", clientSecret).
216215
WithFormField("scope", "one").

0 commit comments

Comments
 (0)