Skip to content

Commit 41a3f22

Browse files
authored
Merge pull request #6 from LyricTian/develop
Develop
2 parents 8f69df4 + 82e5495 commit 41a3f22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2079
-1845
lines changed

README.md

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,84 @@
1-
Golang OAuth 2.0协议实现
2-
========================
1+
OAuth2服务端
2+
===========
33

4-
[![GoDoc](https://godoc.org/gopkg.in/oauth2.v1?status.svg)](https://godoc.org/gopkg.in/oauth2.v1)
5-
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/oauth2.v1)](https://goreportcard.com/report/gopkg.in/oauth2.v1)
4+
> 基于Golang实现的OAuth2协议,具有简单化、模块化的特点
5+
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)
68

79
获取
810
----
911

1012
```bash
11-
$ go get -v gopkg.in/oauth2.v1
13+
$ go get -u gopkg.in/oauth2.v2/...
1214
```
1315

14-
范例
16+
使用
1517
----
1618

17-
> 使用之前,初始化客户端信息
18-
19-
```go
19+
``` go
2020
package main
2121

2222
import (
23-
"fmt"
24-
25-
"gopkg.in/oauth2.v1"
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"
2631
)
2732

2833
func main() {
29-
// 初始化配置参数
30-
ocfg := &oauth2.OAuthConfig{
31-
ACConfig: &oauth2.ACConfig{
32-
ATExpiresIn: 60 * 60 * 24,
33-
},
34-
}
35-
mcfg := oauth2.NewMongoConfig("mongodb://127.0.0.1:27017", "test")
36-
37-
// 创建默认的OAuth2管理实例(基于MongoDB)
38-
manager, err := oauth2.NewDefaultOAuthManager(ocfg, mcfg, "xxx", "xxx")
39-
if err != nil {
40-
panic(err)
41-
}
42-
manager.SetACGenerate(oauth2.NewDefaultACGenerate())
43-
manager.SetACStore(oauth2.NewACMemoryStore(0))
44-
45-
// 模拟授权码模式
46-
// 使用默认参数,生成授权码
47-
code, err := manager.GetACManager().
48-
GenerateCode("clientID_x", "userID_x", "http://www.example.com/cb", "scopes")
49-
if err != nil {
50-
panic(err)
51-
}
52-
53-
// 生成访问令牌及更新令牌
54-
genToken, err := manager.GetACManager().
55-
GenerateToken(code, "http://www.example.com/cb", "clientID_x", "clientSecret_x", true)
56-
if err != nil {
57-
panic(err)
58-
}
59-
60-
// 检查访问令牌
61-
checkToken, err := manager.CheckAccessToken(genToken.AccessToken)
62-
if err != nil {
63-
panic(err)
64-
}
65-
66-
// TODO: 使用用户标识、申请的授权范围响应数据
67-
fmt.Println(checkToken.UserID, checkToken.Scope)
68-
69-
// 更新令牌
70-
newToken, err := manager.RefreshAccessToken(checkToken.RefreshToken, "scopes")
71-
if err != nil {
72-
panic(err)
73-
}
74-
fmt.Println(newToken.AccessToken, newToken.ATExpiresIn)
75-
// TODO: 将新的访问令牌响应给客户端
76-
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))
7763
}
64+
7865
```
7966

80-
执行测试
81-
-------
67+
测试
68+
----
8269

83-
```bash
84-
$ go test -v
85-
#
86-
$ goconvey -port=9090
70+
``` bash
71+
$ goconvey -port=9092
8772
```
8873

74+
> goconvey使用明细[https://github.com/smartystreets/goconvey](https://github.com/smartystreets/goconvey)
75+
76+
范例
77+
----
78+
79+
模拟授权码模式的测试范例,请查看[example](/example)
80+
81+
8982
License
9083
-------
9184

authorizationCode.go

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

authorizationCodeGenerate.go

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

0 commit comments

Comments
 (0)