Skip to content

Commit 584af9b

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

24 files changed

+442
-497
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

const.go

+9-14
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

+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

generate.go

+6-6
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

+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.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,44 @@ import (
44
"time"
55
)
66

7-
// TokenGenerateRequest Provide to generate the token request parameters
7+
// TokenGenerateRequest provide to generate the token request parameters
88
type TokenGenerateRequest struct {
9-
ClientID string // The client information
10-
ClientSecret string // The client secret
11-
UserID string // The user id
12-
RedirectURI string // Redirect URI
13-
Scope string // Scope of authorization
14-
Code string // Authorization code
15-
Refresh string // Refreshing token
16-
AccessTokenExp time.Duration // Access token expiration time (in seconds)
9+
ClientID string
10+
ClientSecret string
11+
UserID string
12+
RedirectURI string
13+
Scope string
14+
Code string
15+
Refresh string
16+
AccessTokenExp time.Duration
1717
}
1818

19-
// Manager Authorization management interface
19+
// Manager authorization management interface
2020
type Manager interface {
21-
// Check the interface implementation
21+
// check the interface implementation
2222
CheckInterface() (err error)
2323

24-
// Get the client information
24+
// get the client information
2525
GetClient(clientID string) (cli ClientInfo, err error)
2626

27-
// Generate the authorization token(code)
27+
// generate the authorization token(code)
2828
GenerateAuthToken(rt ResponseType, tgr *TokenGenerateRequest) (authToken TokenInfo, err error)
2929

30-
// Generate the access token
30+
// generate the access token
3131
GenerateAccessToken(rt GrantType, tgr *TokenGenerateRequest) (accessToken TokenInfo, err error)
3232

33-
// Refreshing an access token
33+
// refreshing an access token
3434
RefreshAccessToken(tgr *TokenGenerateRequest) (accessToken TokenInfo, err error)
3535

36-
// Use the access token to delete the token information
36+
// use the access token to delete the token information
3737
RemoveAccessToken(access string) (err error)
3838

39-
// Use the refresh token to delete the token information
39+
// use the refresh token to delete the token information
4040
RemoveRefreshToken(refresh string) (err error)
4141

42-
// According to the access token for corresponding token information
42+
// according to the access token for corresponding token information
4343
LoadAccessToken(access string) (ti TokenInfo, err error)
4444

45-
// According to the refresh token for corresponding token information
45+
// according to the refresh token for corresponding token information
4646
LoadRefreshToken(refresh string) (ti TokenInfo, err error)
4747
}

manage/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import "time"
44

55
// Config authorization configuration parameters
66
type Config struct {
7-
// access token expiration time (in seconds)
7+
// access token expiration time
88
AccessTokenExp time.Duration
9-
// refresh token expiration time(in seconds)
9+
// refresh token expiration time
1010
RefreshTokenExp time.Duration
1111
// whether to generate the refreshing token
1212
IsGenerateRefresh bool

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 {

model.go

+2-32
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,44 @@ package oauth2
33
import "time"
44

55
type (
6-
// ClientInfo The client information model interface
6+
// ClientInfo the client information model interface
77
ClientInfo interface {
8-
// The client id
98
GetID() string
10-
// The client secret
119
GetSecret() string
12-
// The client domain
1310
GetDomain() string
14-
// The extension data related to the client
1511
GetExtraData() interface{}
1612
}
1713

18-
// TokenInfo The token information model interface
14+
// TokenInfo the token information model interface
1915
TokenInfo interface {
20-
// Get client id
2116
GetClientID() string
22-
// Set client id
2317
SetClientID(string)
24-
// Get user id
2518
GetUserID() string
26-
// Set user id
2719
SetUserID(string)
28-
// Get Redirect URI
2920
GetRedirectURI() string
30-
// Set Redirect URI
3121
SetRedirectURI(string)
32-
// Get Scope of authorization
3322
GetScope() string
34-
// Set Scope of authorization
3523
SetScope(string)
3624

37-
// Get Authorization code
3825
GetCode() string
39-
// Set Authorization code
4026
SetCode(string)
41-
// Get Create Time
4227
GetCodeCreateAt() time.Time
43-
// Set Create Time
4428
SetCodeCreateAt(time.Time)
45-
// Get The lifetime in seconds of the authorization code
4629
GetCodeExpiresIn() time.Duration
47-
// Set The lifetime in seconds of the authorization code
4830
SetCodeExpiresIn(time.Duration)
4931

50-
// Get Access Token
5132
GetAccess() string
52-
// Set Access Token
5333
SetAccess(string)
54-
// Get Create Time
5534
GetAccessCreateAt() time.Time
56-
// Set Create Time
5735
SetAccessCreateAt(time.Time)
58-
// Get The lifetime in seconds of the access token
5936
GetAccessExpiresIn() time.Duration
60-
// Set The lifetime in seconds of the access token
6137
SetAccessExpiresIn(time.Duration)
6238

63-
// Get Refresh Token
6439
GetRefresh() string
65-
// Set Refresh Token
6640
SetRefresh(string)
67-
// Get Create Time
6841
GetRefreshCreateAt() time.Time
69-
// Set Create Time
7042
SetRefreshCreateAt(time.Time)
71-
// Get The lifetime in seconds of the access token
7243
GetRefreshExpiresIn() time.Duration
73-
// Set The lifetime in seconds of the access token
7444
SetRefreshExpiresIn(time.Duration)
7545
}
7646
)

0 commit comments

Comments
 (0)