|
1 | | -基于Golang的OAuth2服务实现 |
2 | | -======================= |
3 | | - |
4 | | -> 完全模块化、支持http/fasthttp的服务端处理、令牌存储支持redis/mongodb |
| 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. |
5 | 4 |
|
6 | 5 | [](https://godoc.org/gopkg.in/oauth2.v3) |
7 | 6 | [](https://goreportcard.com/report/gopkg.in/oauth2.v3) |
8 | 7 |
|
9 | | -获取 |
10 | | ----- |
| 8 | +Quick Start |
| 9 | +----------- |
| 10 | + |
| 11 | +### Download and install |
11 | 12 |
|
12 | 13 | ``` bash |
13 | 14 | $ go get -u gopkg.in/oauth2.v3/... |
14 | 15 | ``` |
15 | 16 |
|
16 | | -HTTP服务端 |
17 | | --------- |
| 17 | +### Create file `server.go` |
18 | 18 |
|
19 | 19 | ``` go |
20 | 20 | package main |
21 | 21 |
|
22 | 22 | import ( |
23 | | - "log" |
24 | 23 | "net/http" |
25 | 24 |
|
26 | 25 | "gopkg.in/oauth2.v3/manage" |
27 | 26 | "gopkg.in/oauth2.v3/server" |
28 | | - "gopkg.in/oauth2.v3/store/client" |
29 | 27 | "gopkg.in/oauth2.v3/store/token" |
30 | 28 | ) |
31 | 29 |
|
32 | 30 | func main() { |
33 | 31 | manager := manage.NewRedisManager( |
34 | 32 | &token.RedisConfig{Addr: "192.168.33.70:6379"}, |
35 | 33 | ) |
36 | | - manager.MapClientStorage(client.NewTempStore()) |
37 | 34 | srv := server.NewServer(server.NewConfig(), manager) |
38 | | - |
| 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 | + }) |
39 | 40 | http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { |
40 | | - authReq, err := srv.GetAuthorizeRequest(r) |
41 | | - if err != nil { |
42 | | - http.Error(w, err.Error(), http.StatusBadRequest) |
43 | | - return |
44 | | - } |
45 | | - // TODO: 登录验证、授权处理 |
46 | | - authReq.UserID = "000000" |
47 | | - |
48 | | - err = srv.HandleAuthorizeRequest(w, authReq) |
| 41 | + err := srv.HandleAuthorizeRequest(w, r) |
49 | 42 | if err != nil { |
50 | 43 | http.Error(w, err.Error(), http.StatusBadRequest) |
51 | 44 | } |
52 | 45 | }) |
53 | | - |
54 | 46 | http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { |
55 | 47 | err := srv.HandleTokenRequest(w, r) |
56 | 48 | if err != nil { |
57 | 49 | http.Error(w, err.Error(), http.StatusBadRequest) |
58 | 50 | } |
59 | 51 | }) |
60 | | - |
61 | | - log.Fatal(http.ListenAndServe(":9096", nil)) |
| 52 | + http.ListenAndServe(":9096", nil) |
62 | 53 | } |
63 | | - |
64 | | -``` |
65 | | - |
66 | | -FastHTTP服务端 |
67 | | -------------- |
68 | | - |
69 | | -``` go |
70 | | -srv := server.NewFastServer(server.NewConfig(), manager) |
71 | | - |
72 | | -fasthttp.ListenAndServe(":9096", func(ctx *fasthttp.RequestCtx) { |
73 | | - switch string(ctx.Request.URI().Path()) { |
74 | | - case "/authorize": |
75 | | - authReq, err := srv.GetAuthorizeRequest(ctx) |
76 | | - if err != nil { |
77 | | - ctx.Error(err.Error(), 400) |
78 | | - return |
79 | | - } |
80 | | - authReq.UserID = "000000" |
81 | | - // TODO: 登录验证、授权处理 |
82 | | - err = srv.HandleAuthorizeRequest(ctx, authReq) |
83 | | - if err != nil { |
84 | | - ctx.Error(err.Error(), 400) |
85 | | - } |
86 | | - case "/token": |
87 | | - err := srv.HandleTokenRequest(ctx) |
88 | | - if err != nil { |
89 | | - ctx.Error(err.Error(), 400) |
90 | | - } |
91 | | - } |
92 | | -}) |
93 | 54 | ``` |
94 | 55 |
|
95 | | -测试 |
96 | | ----- |
97 | | -> [goconvey](https://github.com/smartystreets/goconvey) |
| 56 | +### Build and run |
98 | 57 |
|
99 | 58 | ``` bash |
100 | | -$ goconvey -port=9092 |
| 59 | +$ go build server.go |
| 60 | +$ ./server |
101 | 61 | ``` |
102 | 62 |
|
103 | | -范例 |
104 | | ----- |
| 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 |
105 | 71 |
|
106 | | -模拟授权码模式的测试范例,请查看[example](/example) |
| 72 | +Example |
| 73 | +------- |
107 | 74 |
|
| 75 | +Simulation examples of authorization code model, please check [example](/example) |
108 | 76 |
|
109 | 77 | License |
110 | 78 | ------- |
|
0 commit comments