1- Golang OAuth2 Server
2- =====================
1+ OAuth2服务端
2+ ===========
3+
4+ > 基于Golang实现的OAuth2协议,具有简单化、模块化的特点
35
46[ ![ GoDoc] ( https://godoc.org/gopkg.in/oauth2.v2?status.svg )] ( https://godoc.org/gopkg.in/oauth2.v2 )
57[ ![ Go Report Card] ( https://goreportcard.com/badge/gopkg.in/oauth2.v2 )] ( https://goreportcard.com/report/gopkg.in/oauth2.v2 )
@@ -8,9 +10,75 @@ Golang OAuth2 Server
810----
911
1012``` bash
11- $ go get -v gopkg.in/oauth2.v2
13+ $ go get -u gopkg.in/oauth2.v2/...
14+ ```
15+
16+ 使用
17+ ----
18+
19+ ``` go
20+ package main
21+
22+ import (
23+ " log"
24+ " net/http"
25+
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"
31+ )
32+
33+ func main () {
34+ manager := manage.NewRedisManager (
35+ &token.RedisConfig {Addr: " 192.168.33.70:6379" },
36+ )
37+ manager.MapClientStorage (client.NewTempStore ())
38+ srv := server.NewServer (server.NewConfig (), manager)
39+
40+ 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)
50+ if err != nil {
51+ http.Error (w, err.Error (), http.StatusBadRequest )
52+ }
53+ })
54+
55+ http.HandleFunc (" /token" , func (w http.ResponseWriter , r *http.Request ) {
56+ err := srv.HandleTokenRequest (w, r)
57+ if err != nil {
58+ http.Error (w, err.Error (), http.StatusBadRequest )
59+ }
60+ })
61+
62+ log.Fatal (http.ListenAndServe (" :9096" , nil ))
63+ }
64+
65+ ```
66+
67+ 测试
68+ ----
69+
70+ ``` bash
71+ $ goconvey -port=9092
1272```
1373
74+ > goconvey使用明细[ https://github.com/smartystreets/goconvey ] ( https://github.com/smartystreets/goconvey )
75+
76+ 范例
77+ ----
78+
79+ 模拟授权码模式的测试范例,请查看[ example] ( /example )
80+
81+
1482License
1583-------
1684
0 commit comments