Skip to content

Commit db2f561

Browse files
committed
Fixed error handling
1 parent 45c703f commit db2f561

File tree

17 files changed

+347
-367
lines changed

17 files changed

+347
-367
lines changed

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,23 @@ func main() {
6565
// client test store
6666
manager.MapClientStorage(store.NewTestClientStore())
6767

68-
srv := server.NewServer(server.NewConfig(), manager)
69-
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
70-
// validation and to get the user id
71-
userID = "000000"
72-
return
73-
})
68+
srv := server.NewDefaultServer(manager)
69+
srv.SetAllowGetAccessRequest(true)
70+
7471
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
7572
err := srv.HandleAuthorizeRequest(w, r)
7673
if err != nil {
7774
http.Error(w, err.Error(), http.StatusBadRequest)
7875
}
7976
})
77+
8078
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
8179
err := srv.HandleTokenRequest(w, r)
8280
if err != nil {
8381
http.Error(w, err.Error(), http.StatusBadRequest)
8482
}
8583
})
84+
8685
http.ListenAndServe(":9096", nil)
8786
}
8887
```
@@ -97,7 +96,16 @@ $ ./server
9796
### Open in your web browser
9897

9998
```
100-
http://localhost:9096/authorize?response_type=code&client_id=1&redirect_uri=http%253A%252F%252Flocalhost&scope=all&state=xyz
99+
http://localhost:9096/token?grant_type=clientcredentials&client_id=1&client_secret=11&scope=all
100+
```
101+
102+
```
103+
{
104+
"access_token": "ZGF4ARHJPT2Y_QAIOJVL-Q",
105+
"expires_in": 7200,
106+
"scope": "all",
107+
"token_type": "Bearer"
108+
}
101109
```
102110

103111
## Features

example/README.md

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
Authorization code simulation
2-
=============================
1+
# Authorization Code Grant
32

4-
Run Server
5-
---------
3+
![login](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/login.png)
4+
![auth](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/auth.png)
5+
![token](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/token.png)
6+
7+
## Run Server
68

79
``` bash
810
$ cd example/server
911
$ go build server.go
1012
$ ./server
1113
```
1214

13-
Run Client
14-
----------
15+
## Run Client
1516

1617
```
1718
$ cd example/client
1819
$ go build client.go
1920
$ ./client
2021
```
2122

22-
Open the browser
23-
----------------
24-
25-
[http://localhost:9094](http://localhost:9094)
23+
## Open the browser
2624

27-
``` json
28-
{
29-
"access_token": "BIX-RYRPMHYY4L7O4QTP3Q",
30-
"expires_in": 7200,
31-
"refresh_token": "JRITD106WU6YNRE4UUEV_A",
32-
"scope": "all",
33-
"token_type": "Bearer"
34-
}
35-
```
25+
[http://localhost:9094](http://localhost:9094)

example/server/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func main() {
3737

3838
srv := server.NewServer(server.NewConfig(), manager)
3939
srv.SetUserAuthorizationHandler(userAuthorizeHandler)
40-
srv.SetInternalErrorHandler(func(r *http.Request, err error) {
41-
fmt.Println("OAuth2 Error:", r.RequestURI, err.Error())
40+
srv.SetInternalErrorHandler(func(err error) {
41+
fmt.Println("internal error:", err.Error())
4242
})
4343

4444
http.HandleFunc("/login", loginHandler)

example/server/static/auth.png

84.6 KB
Loading

example/server/static/login.png

79 KB
Loading

example/server/static/token.png

454 KB
Loading

generates/access.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
"gopkg.in/oauth2.v3"
1111
)
1212

13-
// NewAccessGenerate Create to generate the access token instance
13+
// NewAccessGenerate create to generate the access token instance
1414
func NewAccessGenerate() *AccessGenerate {
1515
return &AccessGenerate{}
1616
}
1717

18-
// AccessGenerate Generate the access token
18+
// AccessGenerate generate the access token
1919
type AccessGenerate struct {
2020
}
2121

22-
// Token Based on the UUID generated token
22+
// Token based on the UUID generated token
2323
func (ag *AccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) {
2424
buf := bytes.NewBufferString(data.Client.GetID())
2525
buf.WriteString(data.UserID)

generates/authorize.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import (
99
"gopkg.in/oauth2.v3"
1010
)
1111

12-
// NewAuthorizeGenerate Create to generate the authorize code instance
12+
// NewAuthorizeGenerate create to generate the authorize code instance
1313
func NewAuthorizeGenerate() *AuthorizeGenerate {
1414
return &AuthorizeGenerate{}
1515
}
1616

17-
// AuthorizeGenerate Generate the authorize code
17+
// AuthorizeGenerate generate the authorize code
1818
type AuthorizeGenerate struct{}
1919

20-
// Token Based on the UUID generated token
20+
// Token based on the UUID generated token
2121
func (ag *AuthorizeGenerate) Token(data *oauth2.GenerateBasic) (code string, err error) {
2222
buf := bytes.NewBufferString(data.Client.GetID())
2323
buf.WriteString(data.UserID)

manage/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
)
99

1010
type (
11-
// ValidateURIHandler Validates that RedirectURI is contained in baseURI
11+
// ValidateURIHandler validates that redirectURI is contained in baseURI
1212
ValidateURIHandler func(baseURI, redirectURI string) (err error)
1313
)
1414

15-
// DefaultValidateURI Validates that RedirectURI is contained in baseURI
15+
// DefaultValidateURI validates that redirectURI is contained in baseURI
1616
func DefaultValidateURI(baseURI string, redirectURI string) (err error) {
1717
base, err := url.Parse(baseURI)
1818
if err != nil {

models/client.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ type Client struct {
77
Domain string
88
}
99

10-
// GetID The client id
10+
// GetID client id
1111
func (c *Client) GetID() string {
1212
return c.ID
1313
}
1414

15-
// GetSecret The client domain
15+
// GetSecret client domain
1616
func (c *Client) GetSecret() string {
1717
return c.Secret
1818
}
1919

20-
// GetDomain The client domain
20+
// GetDomain client domain
2121
func (c *Client) GetDomain() string {
2222
return c.Domain
2323
}
2424

25-
// GetExtraData The extension data related to the client
25+
// GetExtraData extension data related to the client
2626
func (c *Client) GetExtraData() interface{} {
2727
return nil
2828
}

server/config.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package server
22

3-
import "gopkg.in/oauth2.v3"
3+
import (
4+
"time"
5+
6+
"gopkg.in/oauth2.v3"
7+
)
48

59
// Config configuration parameters
610
type Config struct {
7-
TokenType string // TokenType token type(Default is Bearer)
8-
AllowedResponseTypes []oauth2.ResponseType // Allow the authorization type(Default is all)
9-
AllowedGrantTypes []oauth2.GrantType // Allow the grant type(Default is all)
11+
TokenType string // token type
12+
AllowGetAccessRequest bool // to allow GET requests for the token
13+
AllowedResponseTypes []oauth2.ResponseType // allow the authorization type
14+
AllowedGrantTypes []oauth2.GrantType // allow the grant type
1015
}
1116

1217
// NewConfig create to configuration instance
@@ -22,3 +27,14 @@ func NewConfig() *Config {
2227
},
2328
}
2429
}
30+
31+
// AuthorizeRequest authorization request
32+
type AuthorizeRequest struct {
33+
ResponseType oauth2.ResponseType
34+
ClientID string
35+
Scope string
36+
RedirectURI string
37+
State string
38+
UserID string
39+
AccessTokenExp time.Duration
40+
}

server/handler.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,34 @@ import (
1111
type (
1212
// ClientInfoHandler get client info from request
1313
ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error)
14+
1415
// ClientAuthorizedHandler check the client allows to use this authorization grant type
1516
ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error)
17+
1618
// ClientScopeHandler check the client allows to use scope
1719
ClientScopeHandler func(clientID, scope string) (allowed bool, err error)
20+
1821
// UserAuthorizationHandler get user id from request authorization
1922
UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)
23+
2024
// PasswordAuthorizationHandler get user id from username and password
2125
PasswordAuthorizationHandler func(username, password string) (userID string, err error)
26+
2227
// RefreshingScopeHandler check the scope of the refreshing token
2328
RefreshingScopeHandler func(newScope, oldScope string) (allowed bool, err error)
29+
2430
// ResponseErrorHandler response error handing
25-
ResponseErrorHandler func(re *errors.Response)
31+
ResponseErrorHandler func(err error) (re *errors.Response)
32+
2633
// InternalErrorHandler internal error handing
27-
InternalErrorHandler func(r *http.Request, err error)
34+
InternalErrorHandler func(err error)
35+
2836
// AuthorizeScopeHandler set the authorized scope
2937
AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error)
38+
3039
// AccessTokenExpHandler set expiration date for the access token
3140
AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error)
41+
3242
// ExtensionFieldsHandler in response to the access token with the extension of the field
3343
ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{})
3444
)
@@ -38,7 +48,7 @@ func ClientFormHandler(r *http.Request) (clientID, clientSecret string, err erro
3848
clientID = r.Form.Get("client_id")
3949
clientSecret = r.Form.Get("client_secret")
4050
if clientID == "" || clientSecret == "" {
41-
err = errors.ErrInvalidRequest
51+
err = errors.ErrInvalidClient
4252
}
4353
return
4454
}

server/request.go

-18
This file was deleted.

0 commit comments

Comments
 (0)