Skip to content

Commit 584af9b

Browse files
authored
Merge pull request #23 from LyricTian/develop
Fixed error handling
2 parents 4153b1e + db2f561 commit 584af9b

File tree

24 files changed

+442
-497
lines changed

24 files changed

+442
-497
lines changed

README.md

Lines changed: 15 additions & 7 deletions
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

const.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
package oauth2
22

3-
// ResponseType Response Type
3+
// ResponseType the type of authorization request
44
type ResponseType string
55

6+
// define the type of authorization request
67
const (
7-
// Code Authorization code type
8-
Code ResponseType = "code"
9-
// Token Token type
8+
Code ResponseType = "code"
109
Token ResponseType = "token"
1110
)
1211

1312
func (rt ResponseType) String() string {
1413
return string(rt)
1514
}
1615

17-
// GrantType Authorization Grant
16+
// GrantType authorization model
1817
type GrantType string
1918

19+
// define authorization model
2020
const (
21-
// AuthorizationCode Authorization Code
22-
AuthorizationCode GrantType = "authorization_code"
23-
// PasswordCredentials Resource Owner Password Credentials
21+
AuthorizationCode GrantType = "authorization_code"
2422
PasswordCredentials GrantType = "password"
25-
// ClientCredentials Client Credentials
26-
ClientCredentials GrantType = "clientcredentials"
27-
// Refreshing Refresh Token
28-
Refreshing GrantType = "refreshtoken"
29-
// Implicit Implicit Grant
30-
Implicit GrantType = "__implicit"
23+
ClientCredentials GrantType = "clientcredentials"
24+
Refreshing GrantType = "refreshtoken"
25+
Implicit GrantType = "__implicit"
3126
)
3227

3328
func (gt GrantType) String() string {

example/README.md

Lines changed: 9 additions & 19 deletions
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

Lines changed: 2 additions & 2 deletions
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

generate.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package oauth2
33
import "time"
44

55
type (
6-
// GenerateBasic Provide the basis of the generated token data
6+
// GenerateBasic provide the basis of the generated token data
77
GenerateBasic struct {
8-
Client ClientInfo // The client information
9-
UserID string // The user id
10-
CreateAt time.Time // Creation time
8+
Client ClientInfo
9+
UserID string
10+
CreateAt time.Time
1111
}
1212

13-
// AuthorizeGenerate Generate the authorization code interface
13+
// AuthorizeGenerate generate the authorization code interface
1414
AuthorizeGenerate interface {
1515
Token(data *GenerateBasic) (code string, err error)
1616
}
1717

18-
// AccessGenerate Generate the access and refresh tokens interface
18+
// AccessGenerate generate the access and refresh tokens interface
1919
AccessGenerate interface {
2020
Token(data *GenerateBasic, isGenRefresh bool) (access, refresh string, err error)
2121
}

generates/access.go

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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)

0 commit comments

Comments
 (0)