Skip to content

Commit 794afb1

Browse files
committed
Refactoring server package
1 parent a5ac29d commit 794afb1

File tree

22 files changed

+553
-376
lines changed

22 files changed

+553
-376
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
> 完全模块化、支持http/fasthttp的服务端处理、令牌存储支持redis/mongodb
55
6-
[![GoDoc](https://godoc.org/gopkg.in/oauth2.v2?status.svg)](https://godoc.org/gopkg.in/oauth2.v2)
7-
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/oauth2.v2)](https://goreportcard.com/report/gopkg.in/oauth2.v2)
6+
[![GoDoc](https://godoc.org/gopkg.in/oauth2.v3?status.svg)](https://godoc.org/gopkg.in/oauth2.v3)
7+
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/oauth2.v3)](https://goreportcard.com/report/gopkg.in/oauth2.v3)
88

99
获取
1010
----
1111

1212
``` bash
13-
$ go get -u gopkg.in/oauth2.v2/...
13+
$ go get -u gopkg.in/oauth2.v3/...
1414
```
1515

1616
HTTP服务端
@@ -23,10 +23,10 @@ import (
2323
"log"
2424
"net/http"
2525

26-
"gopkg.in/oauth2.v2/manage"
27-
"gopkg.in/oauth2.v2/server"
28-
"gopkg.in/oauth2.v2/store/client"
29-
"gopkg.in/oauth2.v2/store/token"
26+
"gopkg.in/oauth2.v3/manage"
27+
"gopkg.in/oauth2.v3/server"
28+
"gopkg.in/oauth2.v3/store/client"
29+
"gopkg.in/oauth2.v3/store/token"
3030
)
3131

3232
func main() {

errors/error.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package errors
2+
3+
import "errors"
4+
5+
var (
6+
// ErrUnauthorizedClient unauthorized client
7+
ErrUnauthorizedClient = errors.New("unauthorized_client")
8+
9+
// ErrAccessDenied access denied
10+
ErrAccessDenied = errors.New("access_denied")
11+
12+
// ErrUnsupportedResponseType unsupported response type
13+
ErrUnsupportedResponseType = errors.New("unsupported_response_type")
14+
15+
// ErrInvalidScope invalid scope
16+
ErrInvalidScope = errors.New("invalid_scope")
17+
18+
// ErrInvalidRequest invalid request
19+
ErrInvalidRequest = errors.New("invalid_request")
20+
21+
// ErrInvalidClient invalid client
22+
ErrInvalidClient = errors.New("invalid_client")
23+
24+
// ErrInvalidGrant invalid grant
25+
ErrInvalidGrant = errors.New("invalid_grant")
26+
27+
// ErrUnsupportedGrantType unsupported grant type
28+
ErrUnsupportedGrantType = errors.New("unsupported_grant_type")
29+
30+
// ErrServerError server error
31+
ErrServerError = errors.New("server_error")
32+
)
33+
34+
var (
35+
// ErrNilValue Nil Value
36+
ErrNilValue = errors.New("nil value")
37+
38+
// ErrInvalidRedirectURI invalid redirect uri
39+
ErrInvalidRedirectURI = errors.New("invalid redirect uri")
40+
41+
// ErrInvalidAuthorizeCode invalid authorize code
42+
ErrInvalidAuthorizeCode = errors.New("invalid authorize code")
43+
44+
// ErrInvalidAccessToken invalid access token
45+
ErrInvalidAccessToken = errors.New("invalid access token")
46+
47+
// ErrInvalidRefreshToken invalid refresh token
48+
ErrInvalidRefreshToken = errors.New("invalid refresh token")
49+
50+
// ErrExpiredAccessToken expired access token
51+
ErrExpiredAccessToken = errors.New("expired access token")
52+
53+
// ErrExpiredRefreshToken expired refresh token
54+
ErrExpiredRefreshToken = errors.New("expired refresh token")
55+
)

example/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Authorization code simulation
33

44
Run Server
55
---------
6-
> Run fasthttp server(`cd example/fastserver`)
76

87
``` bash
98
$ cd example/server

example/server/main.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"log"
55
"net/http"
66

7-
"gopkg.in/oauth2.v2/manage"
8-
"gopkg.in/oauth2.v2/models"
9-
"gopkg.in/oauth2.v2/server"
10-
"gopkg.in/oauth2.v2/store/client"
11-
"gopkg.in/oauth2.v2/store/token"
7+
"fmt"
8+
9+
"gopkg.in/oauth2.v3"
10+
"gopkg.in/oauth2.v3/manage"
11+
"gopkg.in/oauth2.v3/models"
12+
"gopkg.in/oauth2.v3/server"
13+
"gopkg.in/oauth2.v3/store/client"
14+
"gopkg.in/oauth2.v3/store/token"
1215
)
1316

1417
func main() {
@@ -23,16 +26,18 @@ func main() {
2326
}))
2427

2528
srv := server.NewServer(server.NewConfig(), manager)
29+
srv.SetAllowedResponseType(oauth2.Code)
30+
srv.SetAllowedGrantType(oauth2.AuthorizationCode)
31+
srv.SetErrorHandler(func(err error) {
32+
fmt.Println("OAuth2 Error:", err.Error())
33+
})
34+
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
35+
userID = "000000"
36+
return
37+
})
2638

2739
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
28-
authReq, err := srv.GetAuthorizeRequest(r)
29-
if err != nil {
30-
http.Error(w, err.Error(), http.StatusBadRequest)
31-
return
32-
}
33-
// TODO: User authentication...
34-
authReq.UserID = "000000"
35-
err = srv.HandleAuthorizeRequest(w, authReq)
40+
err := srv.HandleAuthorizeRequest(w, r)
3641
if err != nil {
3742
http.Error(w, err.Error(), http.StatusBadRequest)
3843
}

generates/access.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88

99
"github.com/LyricTian/go.uuid"
10-
"gopkg.in/oauth2.v2"
10+
"gopkg.in/oauth2.v3"
1111
)
1212

1313
// NewAccessGenerate Create to generate the access token instance

generates/access_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"testing"
55
"time"
66

7-
"gopkg.in/oauth2.v2"
8-
"gopkg.in/oauth2.v2/models"
7+
"gopkg.in/oauth2.v3"
8+
"gopkg.in/oauth2.v3/models"
99

1010
. "github.com/smartystreets/goconvey/convey"
1111
)

generates/authorize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
"github.com/LyricTian/go.uuid"
9-
"gopkg.in/oauth2.v2"
9+
"gopkg.in/oauth2.v3"
1010
)
1111

1212
// NewAuthorizeGenerate Create to generate the authorize code instance

generates/authorize_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"testing"
55
"time"
66

7-
"gopkg.in/oauth2.v2"
8-
"gopkg.in/oauth2.v2/models"
7+
"gopkg.in/oauth2.v3"
8+
"gopkg.in/oauth2.v3/models"
99

1010
. "github.com/smartystreets/goconvey/convey"
1111
)

manage.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package oauth2
22

33
// TokenGenerateRequest Provide to generate the token request parameters
44
type TokenGenerateRequest struct {
5-
ClientID string // The client information
6-
ClientSecret string // The client secret
7-
UserID string // The user id
8-
RedirectURI string // Redirect URI
9-
Scope string // Scope of authorization
10-
Code string // Authorization code
11-
Refresh string // Refresh token
12-
IsGenerateRefresh bool // Whether to generate refresh token
5+
ClientID string // The client information
6+
ClientSecret string // The client secret
7+
UserID string // The user id
8+
RedirectURI string // Redirect URI
9+
Scope string // Scope of authorization
10+
Code string // Authorization code
11+
Refresh string // Refreshing token
1312
}
1413

1514
// Manager Authorization management interface

manage/error.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)