Skip to content

Commit a5ac29d

Browse files
committed
To optimize the implementation
1 parent da5af9b commit a5ac29d

29 files changed

+407
-847
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"net/http"
2525

2626
"gopkg.in/oauth2.v2/manage"
27-
"gopkg.in/oauth2.v2/models"
2827
"gopkg.in/oauth2.v2/server"
2928
"gopkg.in/oauth2.v2/store/client"
3029
"gopkg.in/oauth2.v2/store/token"

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 {

example/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
OAuth2授权码模式模拟
2-
=================
1+
Authorization code simulation
2+
=============================
33

4-
运行服务端
5-
--------
6-
> 运行fasthttp服务端,请使用`cd example/fastserver`
4+
Run Server
5+
---------
6+
> Run fasthttp server(`cd example/fastserver`)
77
8-
```
8+
``` bash
99
$ cd example/server
1010
$ go run main.go
1111
```
1212

13-
运行客户端
14-
--------
13+
Run Client
14+
----------
1515

1616
```
1717
$ cd example/client
1818
$ go run main.go
1919
```
2020

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

2424
[http://localhost:9094](http://localhost:9094)
2525

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.

example/server/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import (
1212
)
1313

1414
func main() {
15-
// 创建基于redis的oauth2管理实例
1615
manager := manage.NewRedisManager(
1716
&token.RedisConfig{Addr: "192.168.33.70:6379"},
1817
)
19-
// 使用临时客户端存储
18+
// Create the client temporary storage
2019
manager.MapClientStorage(client.NewTempStore(&models.Client{
2120
ID: "222222",
2221
Secret: "22222222",
@@ -31,8 +30,8 @@ func main() {
3130
http.Error(w, err.Error(), http.StatusBadRequest)
3231
return
3332
}
33+
// TODO: User authentication...
3434
authReq.UserID = "000000"
35-
// TODO: 登录验证、授权处理
3635
err = srv.HandleAuthorizeRequest(w, authReq)
3736
if err != nil {
3837
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -46,6 +45,6 @@ func main() {
4645
}
4746
})
4847

49-
log.Println("OAuth2 server is running at 9096 port.")
48+
log.Println("Server is running at 9096 port.")
5049
log.Fatal(http.ListenAndServe(":9096", nil))
5150
}

generate.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ package oauth2
33
import "time"
44

55
type (
6-
// GenerateBasic 提供生成令牌的基础数据
6+
// GenerateBasic Provide the basis of the generated token data
77
GenerateBasic struct {
8-
Client ClientInfo // 客户端信息
9-
UserID string // 用户标识
10-
CreateAt time.Time // 创建时间
8+
Client ClientInfo // The client information
9+
UserID string // The user id
10+
CreateAt time.Time // Creation time
1111
}
1212

13-
// AuthorizeGenerate 授权令牌生成接口
13+
// AuthorizeGenerate Generate the authorization code interface
1414
AuthorizeGenerate interface {
15-
// 授权令牌
1615
Token(data *GenerateBasic) (code string, err error)
1716
}
1817

19-
// AccessGenerate 访问令牌生成接口
18+
// AccessGenerate Generate the access and refresh tokens interface
2019
AccessGenerate interface {
21-
// 访问令牌、更新令牌
2220
Token(data *GenerateBasic, isGenRefresh bool) (access, refresh string, err error)
2321
}
2422
)

generates/access.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,34 @@ package generates
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"strconv"
67
"strings"
78

89
"github.com/LyricTian/go.uuid"
9-
"gopkg.in/LyricTian/lib.v2"
1010
"gopkg.in/oauth2.v2"
1111
)
1212

13-
// NewAccessGenerate 创建访问令牌生成实例
13+
// NewAccessGenerate Create to generate the access token instance
1414
func NewAccessGenerate() *AccessGenerate {
1515
return &AccessGenerate{}
1616
}
1717

18-
// AccessGenerate 访问令牌生成
18+
// AccessGenerate Generate the access token
1919
type AccessGenerate struct {
2020
}
2121

22-
// Token 生成令牌
22+
// Token Based on the UUID generated token
2323
func (ag *AccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) {
2424
buf := bytes.NewBufferString(data.Client.GetID())
2525
buf.WriteString(data.UserID)
2626
buf.WriteString(strconv.FormatInt(data.CreateAt.UnixNano(), 10))
27-
access, err = lib.NewEncryption(uuid.NewV3(uuid.NewV4(), buf.String()).Bytes()).MD5()
28-
if err != nil {
29-
return
30-
}
31-
access = strings.ToUpper(access)
27+
28+
access = base64.URLEncoding.EncodeToString(uuid.NewV3(uuid.NewV4(), buf.String()).Bytes())
29+
access = strings.ToUpper(strings.TrimRight(access, "="))
3230
if isGenRefresh {
33-
refresh, err = lib.NewEncryption(uuid.NewV5(uuid.NewV4(), buf.String()).Bytes()).Sha1()
34-
if err != nil {
35-
return
36-
}
37-
refresh = strings.ToUpper(refresh)
31+
refresh = base64.URLEncoding.EncodeToString(uuid.NewV5(uuid.NewV4(), buf.String()).Bytes())
32+
refresh = strings.ToUpper(strings.TrimRight(refresh, "="))
3833
}
3934

4035
return

generates/access_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"testing"
55
"time"
66

7-
. "github.com/smartystreets/goconvey/convey"
87
"gopkg.in/oauth2.v2"
98
"gopkg.in/oauth2.v2/models"
9+
10+
. "github.com/smartystreets/goconvey/convey"
1011
)
1112

1213
func TestAccess(t *testing.T) {
@@ -24,5 +25,7 @@ func TestAccess(t *testing.T) {
2425
So(err, ShouldBeNil)
2526
So(access, ShouldNotBeEmpty)
2627
So(refresh, ShouldNotBeEmpty)
28+
Println("\nAccess Token:" + access)
29+
Println("Refresh Token:" + refresh)
2730
})
2831
}

generates/authorize.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@ package generates
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"strings"
67

78
"github.com/LyricTian/go.uuid"
8-
"gopkg.in/LyricTian/lib.v2"
99
"gopkg.in/oauth2.v2"
1010
)
1111

12-
// NewAuthorizeGenerate 创建授权令牌生成实例
12+
// NewAuthorizeGenerate Create to generate the authorize code instance
1313
func NewAuthorizeGenerate() *AuthorizeGenerate {
1414
return &AuthorizeGenerate{}
1515
}
1616

17-
// AuthorizeGenerate 授权令牌生成
17+
// AuthorizeGenerate Generate the authorize code
1818
type AuthorizeGenerate struct{}
1919

20-
// Token 生成令牌
20+
// Token Based on the UUID generated token
2121
func (ag *AuthorizeGenerate) Token(data *oauth2.GenerateBasic) (code string, err error) {
22-
buf := bytes.NewBuffer(uuid.NewV1().Bytes())
22+
buf := bytes.NewBufferString(data.Client.GetID())
2323
buf.WriteString(data.UserID)
24-
buf.WriteString(data.Client.GetID())
25-
code, err = lib.NewEncryption(buf.Bytes()).MD5()
26-
if err != nil {
27-
return
28-
}
29-
code = strings.ToUpper(code)
24+
token := uuid.NewV3(uuid.NewV1(), buf.String())
25+
code = base64.URLEncoding.EncodeToString(token.Bytes())
26+
code = strings.ToUpper(strings.TrimRight(code, "="))
27+
3028
return
3129
}

0 commit comments

Comments
 (0)