Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions token_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ func (s *TokenStore) gc() {
for range s.ticker.C {
now := time.Now().Unix()
var count int64
if err := s.db.Table(s.tableName).Where("expired_at <= ?", now).Or("code = ? and access = ? AND refresh = ?", "", "", "").Count(&count).Error; err != nil {
db := s.db.Table(s.tableName).Where("expired_at != 0 AND expired_at <= ?", now).Or("code = ? and access = ? AND refresh = ?", "", "", "")
if err := db.Count(&count).Error; err != nil {
s.errorf("[ERROR]:%s\n", err)
return
}
if count > 0 {
// not soft delete.
if err := s.db.Table(s.tableName).Where("expired_at <= ?", now).Or("code = ? and access = ? AND refresh = ?", "", "", "").Unscoped().Delete(&TokenStoreItem{}).Error; err != nil {
if err := db.Unscoped().Delete(&TokenStoreItem{}).Error; err != nil {
s.errorf("[ERROR]:%s\n", err)
}
}
Expand All @@ -125,11 +126,21 @@ func (s *TokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
item.ExpiredAt = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn()).Unix()
} else {
item.Access = info.GetAccess()
item.ExpiredAt = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn()).Unix()
if accessExpiresIn := info.GetAccessExpiresIn(); accessExpiresIn != 0 {
item.ExpiredAt = info.GetAccessCreateAt().Add(accessExpiresIn).Unix()
}

if refresh := info.GetRefresh(); refresh != "" {
item.Refresh = info.GetRefresh()
item.ExpiredAt = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn()).Unix()
item.Refresh = refresh
refreshExpiresIn := info.GetRefreshExpiresIn()
refreshExpiredAt := info.GetRefreshCreateAt().Add(refreshExpiresIn).Unix()
if item.ExpiredAt != 0 {
if refreshExpiresIn == 0 {
item.ExpiredAt = 0
} else if refreshExpiredAt > item.ExpiredAt {
item.ExpiredAt = refreshExpiredAt
}
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions token_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ func TestTokenStore(t *testing.T) {
So(ainfo, ShouldBeNil)
})

Convey("Test access token(no expiration time) store", func() {
info := &models.Token{
ClientID: "1",
UserID: "1_1",
RedirectURI: "http://localhost/",
Scope: "all",
Access: "1_1_2",
AccessCreateAt: time.Now(),
AccessExpiresIn: 0,
}
err := store.Create(context.Background(), info)
So(err, ShouldBeNil)

// wait gc
time.Sleep(time.Second)

ainfo, err := store.GetByAccess(context.Background(), info.GetAccess())
So(err, ShouldBeNil)
So(ainfo.GetUserID(), ShouldEqual, info.GetUserID())

err = store.RemoveByAccess(context.Background(), info.GetAccess())
So(err, ShouldBeNil)

ainfo, err = store.GetByAccess(context.Background(), info.GetAccess())
So(err, ShouldBeNil)
So(ainfo, ShouldBeNil)
})

Convey("Test refresh token store", func() {
info := &models.Token{
ClientID: "1",
Expand Down