Skip to content

Commit 9a2e5d0

Browse files
authored
Merge pull request #8 from LyricTian/develop
To reconstruct the service implementation
2 parents 97df1e5 + 5c03098 commit 9a2e5d0

36 files changed

+1224
-1230
lines changed

README.md

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,78 @@
1-
基于Golang的OAuth2服务实现
2-
=======================
1+
OAuth 2.0
2+
=========
3+
> [OAuth 2.0](http://oauth.net/2/) is the next evolution of the OAuth protocol which was originally created in late 2006.
34
4-
> 完全模块化、支持http/fasthttp的服务端处理、令牌存储支持redis/mongodb
5+
[![GoDoc](https://godoc.org/gopkg.in/oauth2.v3?status.svg)](https://godoc.org/gopkg.in/oauth2.v3)
6+
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/oauth2.v3)](https://goreportcard.com/report/gopkg.in/oauth2.v3)
57

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)
8+
Quick Start
9+
-----------
810

9-
获取
10-
----
11+
### Download and install
1112

1213
``` bash
13-
$ go get -u gopkg.in/oauth2.v2/...
14+
$ go get -u gopkg.in/oauth2.v3/...
1415
```
1516

16-
HTTP服务端
17-
--------
17+
### Create file `server.go`
1818

1919
``` go
2020
package main
2121

2222
import (
23-
"log"
2423
"net/http"
2524

26-
"gopkg.in/oauth2.v2/manage"
27-
"gopkg.in/oauth2.v2/models"
28-
"gopkg.in/oauth2.v2/server"
29-
"gopkg.in/oauth2.v2/store/client"
30-
"gopkg.in/oauth2.v2/store/token"
25+
"gopkg.in/oauth2.v3/manage"
26+
"gopkg.in/oauth2.v3/server"
27+
"gopkg.in/oauth2.v3/store/token"
3128
)
3229

3330
func main() {
3431
manager := manage.NewRedisManager(
3532
&token.RedisConfig{Addr: "192.168.33.70:6379"},
3633
)
37-
manager.MapClientStorage(client.NewTempStore())
3834
srv := server.NewServer(server.NewConfig(), manager)
39-
35+
srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
36+
// validation and to get the user id
37+
userID = "000000"
38+
return
39+
})
4040
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
41-
authReq, err := srv.GetAuthorizeRequest(r)
42-
if err != nil {
43-
http.Error(w, err.Error(), http.StatusBadRequest)
44-
return
45-
}
46-
// TODO: 登录验证、授权处理
47-
authReq.UserID = "000000"
48-
49-
err = srv.HandleAuthorizeRequest(w, authReq)
41+
err := srv.HandleAuthorizeRequest(w, r)
5042
if err != nil {
5143
http.Error(w, err.Error(), http.StatusBadRequest)
5244
}
5345
})
54-
5546
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
5647
err := srv.HandleTokenRequest(w, r)
5748
if err != nil {
5849
http.Error(w, err.Error(), http.StatusBadRequest)
5950
}
6051
})
61-
62-
log.Fatal(http.ListenAndServe(":9096", nil))
52+
http.ListenAndServe(":9096", nil)
6353
}
64-
65-
```
66-
67-
FastHTTP服务端
68-
-------------
69-
70-
``` go
71-
srv := server.NewFastServer(server.NewConfig(), manager)
72-
73-
fasthttp.ListenAndServe(":9096", func(ctx *fasthttp.RequestCtx) {
74-
switch string(ctx.Request.URI().Path()) {
75-
case "/authorize":
76-
authReq, err := srv.GetAuthorizeRequest(ctx)
77-
if err != nil {
78-
ctx.Error(err.Error(), 400)
79-
return
80-
}
81-
authReq.UserID = "000000"
82-
// TODO: 登录验证、授权处理
83-
err = srv.HandleAuthorizeRequest(ctx, authReq)
84-
if err != nil {
85-
ctx.Error(err.Error(), 400)
86-
}
87-
case "/token":
88-
err := srv.HandleTokenRequest(ctx)
89-
if err != nil {
90-
ctx.Error(err.Error(), 400)
91-
}
92-
}
93-
})
9454
```
9555

96-
测试
97-
----
98-
> [goconvey](https://github.com/smartystreets/goconvey)
56+
### Build and run
9957

10058
``` bash
101-
$ goconvey -port=9092
59+
$ go build server.go
60+
$ ./server
10261
```
10362

104-
范例
105-
----
63+
Features
64+
--------
65+
66+
* Based on the [RFC 6749](https://tools.ietf.org/html/rfc6749) implementation
67+
* Easy to use
68+
* Modularity
69+
* Flexible
70+
* Elegant
10671

107-
模拟授权码模式的测试范例,请查看[example](/example)
72+
Example
73+
-------
10874

75+
Simulation examples of authorization code model, please check [example](/example)
10976

11077
License
11178
-------

const.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package oauth2
22

3-
// ResponseType 定义授权类型
3+
// ResponseType Response Type
44
type ResponseType string
55

66
const (
7-
// Code 授权码类型
7+
// Code Authorization code type
88
Code ResponseType = "code"
9-
// Token 令牌类型
9+
// Token Token type
1010
Token ResponseType = "token"
1111
)
1212

1313
func (rt ResponseType) String() string {
1414
return string(rt)
1515
}
1616

17-
// GrantType 定义授权模式
17+
// GrantType Authorization Grant
1818
type GrantType string
1919

2020
const (
21-
// AuthorizationCodeCredentials 授权码模式
22-
AuthorizationCodeCredentials GrantType = "authorization_code"
23-
// PasswordCredentials 密码模式
21+
// AuthorizationCode Authorization Code
22+
AuthorizationCode GrantType = "authorization_code"
23+
// PasswordCredentials Resource Owner Password Credentials
2424
PasswordCredentials GrantType = "password"
25-
// ClientCredentials 客户端模式
25+
// ClientCredentials Client Credentials
2626
ClientCredentials GrantType = "clientcredentials"
27-
// RefreshCredentials 更新令牌模式
28-
RefreshCredentials GrantType = "refreshtoken"
27+
// Refreshing Refresh Token
28+
Refreshing GrantType = "refreshtoken"
29+
// Implicit Implicit Grant
30+
Implicit GrantType = "__implicit"
2931
)
3032

3133
func (gt GrantType) String() string {

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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
OAuth2授权码模式模拟
2-
=================
1+
Authorization code simulation
2+
=============================
33

4-
运行服务端
5-
--------
6-
> 运行fasthttp服务端,请使用`cd example/fastserver`
4+
Run Server
5+
---------
76

8-
```
7+
``` bash
98
$ cd example/server
109
$ go run main.go
1110
```
1211

13-
运行客户端
14-
--------
12+
Run Client
13+
----------
1514

1615
```
1716
$ cd example/client
1817
$ go run main.go
1918
```
2019

21-
打开浏览器
22-
--------
20+
Open the browser
21+
----------------
2322

2423
[http://localhost:9094](http://localhost:9094)
2524

example/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ func main() {
5454
io.Copy(w, resp.Body)
5555
})
5656

57-
log.Println("OAuth2 client is running at 9094 port.")
57+
log.Println("Client is running at 9094 port.")
5858
log.Fatal(http.ListenAndServe(":9094", nil))
5959
}

example/fastserver/main.go

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

0 commit comments

Comments
 (0)