Skip to content

Commit b444738

Browse files
committed
Fixed token mongo store
1 parent b5f24be commit b444738

File tree

4 files changed

+9
-102
lines changed

4 files changed

+9
-102
lines changed

store/token/mongo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewMongoStore(cfg *MongoConfig) (store oauth2.TokenStore, err error) {
3535
// 创建自动过期索引
3636
err = handler.C(cfg.C).EnsureIndex(mgo.Index{
3737
Key: []string{"ExpiredAt"},
38-
ExpireAfter: time.Second,
38+
ExpireAfter: time.Second * 1,
3939
})
4040
if err != nil {
4141
return
@@ -122,7 +122,7 @@ func (ms *MongoStore) RemoveByRefresh(refresh string) (err error) {
122122
func (ms *MongoStore) get(find interface{}) (info oauth2.TokenInfo, err error) {
123123
ms.handler.CHandle(ms.cfg.C, func(c *mgo.Collection) {
124124
var tm models.Token
125-
aerr := c.Find(find).Select(bson.M{"_id": 0}).One(&tm)
125+
aerr := c.Find(find).Select(bson.M{"_id": 0}).Sort("-_id").One(&tm)
126126
if aerr != nil {
127127
if aerr == mgo.ErrNotFound {
128128
return

store/token/mongo_test.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,12 @@ func TestMongoStore(t *testing.T) {
1818
store, err := NewMongoStore(cfg)
1919
So(err, ShouldBeNil)
2020

21-
Convey("Test mongo store access", func() {
21+
Convey("Test access token store", func() {
2222
testAccessStore(store)
2323
})
2424

25-
Convey("Test mongo store refresh", func() {
25+
Convey("Test refresh token store", func() {
2626
testRefreshStore(store)
2727
})
2828
})
2929
}
30-
31-
func TestMongoStoreAccessExpired(t *testing.T) {
32-
Convey("Test mongo store access token expired", t, func() {
33-
cfg := &MongoConfig{
34-
URL: mongoURL,
35-
}
36-
store, err := NewMongoStore(cfg)
37-
So(err, ShouldBeNil)
38-
testAccessExpired(store)
39-
})
40-
}
41-
42-
func TestMongoStoreRefreshExpired(t *testing.T) {
43-
Convey("Test mongo store refresh token expired", t, func() {
44-
cfg := &MongoConfig{
45-
URL: mongoURL,
46-
}
47-
store, err := NewMongoStore(cfg)
48-
So(err, ShouldBeNil)
49-
testRefreshExpired(store)
50-
})
51-
}

store/token/redis_test.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,12 @@ func TestRedisStore(t *testing.T) {
1414
store, err := NewRedisStore(cfg)
1515
So(err, ShouldBeNil)
1616

17-
Convey("Test redis store access", func() {
17+
Convey("Test access token store", func() {
1818
testAccessStore(store)
1919
})
2020

21-
Convey("Test redis store refresh", func() {
21+
Convey("Test refresh token store", func() {
2222
testRefreshStore(store)
2323
})
2424
})
2525
}
26-
27-
func TestRedisStoreAccessExpired(t *testing.T) {
28-
Convey("Test redis store access token expired", t, func() {
29-
cfg := &RedisConfig{
30-
Addr: "192.168.33.70:6379",
31-
}
32-
store, err := NewRedisStore(cfg)
33-
So(err, ShouldBeNil)
34-
testAccessExpired(store)
35-
})
36-
}
37-
38-
func TestRedisStoreRefreshExpired(t *testing.T) {
39-
Convey("Test redis store refresh token expired", t, func() {
40-
cfg := &RedisConfig{
41-
Addr: "192.168.33.70:6379",
42-
}
43-
store, err := NewRedisStore(cfg)
44-
So(err, ShouldBeNil)
45-
testRefreshExpired(store)
46-
})
47-
}

store/token/token_test.go

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ func testAccessStore(store oauth2.TokenStore) {
3737
func testRefreshStore(store oauth2.TokenStore) {
3838
info := &models.Token{
3939
ClientID: "1",
40-
UserID: "1_1",
40+
UserID: "1_2",
4141
RedirectURI: "http://localhost/",
4242
Scope: "all",
4343
AuthType: oauth2.Code.String(),
44-
Access: "1_1_2",
44+
Access: "1_2_1",
4545
AccessCreateAt: time.Now(),
4646
AccessExpiresIn: time.Second * 5,
47-
Refresh: "1_1_2_1",
47+
Refresh: "1_2_2",
4848
RefreshCreateAt: time.Now(),
4949
RefreshExpiresIn: time.Minute * 1,
5050
}
@@ -62,52 +62,3 @@ func testRefreshStore(store oauth2.TokenStore) {
6262
So(err, ShouldBeNil)
6363
So(rinfo, ShouldBeNil)
6464
}
65-
66-
func testAccessExpired(store oauth2.TokenStore) {
67-
info := &models.Token{
68-
ClientID: "1",
69-
UserID: "1_2",
70-
RedirectURI: "http://localhost/",
71-
Scope: "all",
72-
AuthType: oauth2.Code.String(),
73-
Access: "1_2_1",
74-
AccessCreateAt: time.Now(),
75-
AccessExpiresIn: time.Second * 1,
76-
}
77-
err := store.Create(info)
78-
So(err, ShouldBeNil)
79-
80-
time.Sleep(time.Millisecond * 3000)
81-
82-
ainfo, err := store.GetByAccess(info.GetAccess())
83-
So(err, ShouldBeNil)
84-
So(ainfo, ShouldBeNil)
85-
}
86-
87-
func testRefreshExpired(store oauth2.TokenStore) {
88-
info := &models.Token{
89-
ClientID: "1",
90-
UserID: "1_3",
91-
RedirectURI: "http://localhost/",
92-
Scope: "all",
93-
AuthType: oauth2.Code.String(),
94-
Access: "1_3_1",
95-
AccessCreateAt: time.Now(),
96-
AccessExpiresIn: time.Second * 2,
97-
Refresh: "1_3_2",
98-
RefreshCreateAt: time.Now(),
99-
RefreshExpiresIn: time.Second * 1,
100-
}
101-
err := store.Create(info)
102-
So(err, ShouldBeNil)
103-
104-
time.Sleep(time.Millisecond * 3000)
105-
106-
ainfo, err := store.GetByAccess(info.GetAccess())
107-
So(err, ShouldBeNil)
108-
So(ainfo, ShouldBeNil)
109-
110-
rinfo, err := store.GetByRefresh(info.GetRefresh())
111-
So(err, ShouldBeNil)
112-
So(rinfo, ShouldBeNil)
113-
}

0 commit comments

Comments
 (0)