Skip to content

Commit

Permalink
add namespace support
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 7, 2018
1 parent e2240e5 commit de96075
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
5 changes: 4 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Options struct {
// TLS Config to use. When set TLS will be negotiated.
TLSConfig *tls.Config

//KeyNamespace used for prefixing/namespacing keys.
// KeyNamespace used for prefixing/namespacing keys.
KeyNamespace string
}

Expand Down Expand Up @@ -128,6 +128,9 @@ type ClusterOptions struct {
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration

// KeyNamespace used for prefixing/namespacing keys.
KeyNamespace string
}

func (o *ClusterOptions) redisClusterOptions() *redis.ClusterOptions {
Expand Down
45 changes: 32 additions & 13 deletions redis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"fmt"
"time"

"github.com/go-redis/redis"
Expand All @@ -21,29 +22,39 @@ func NewRedisStore(opts *Options) *TokenStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisStoreWithCli(redis.NewClient(opts.redisOptions()))
return NewRedisStoreWithCli(redis.NewClient(opts.redisOptions()), opts.KeyNamespace)
}

// NewRedisStoreWithCli create an instance of a redis store
func NewRedisStoreWithCli(cli *redis.Client) *TokenStore {
return &TokenStore{
func NewRedisStoreWithCli(cli *redis.Client, keyNamespace ...string) *TokenStore {
store := &TokenStore{
cli: cli,
}

if len(keyNamespace) > 0 {
store.ns = keyNamespace[0]
}
return store
}

// NewRedisClusterStore create an instance of a redis cluster store
func NewRedisClusterStore(opts *ClusterOptions) *TokenStore {
if opts == nil {
panic("options cannot be nil")
}
return NewRedisClusterStoreWithCli(redis.NewClusterClient(opts.redisClusterOptions()))
return NewRedisClusterStoreWithCli(redis.NewClusterClient(opts.redisClusterOptions()), opts.KeyNamespace)
}

// NewRedisClusterStoreWithCli create an instance of a redis cluster store
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient) *TokenStore {
return &TokenStore{
func NewRedisClusterStoreWithCli(cli *redis.ClusterClient, keyNamespace ...string) *TokenStore {
store := &TokenStore{
cli: cli,
}

if len(keyNamespace) > 0 {
store.ns = keyNamespace[0]
}
return store
}

type clienter interface {
Expand All @@ -56,13 +67,18 @@ type clienter interface {
// TokenStore redis token store
type TokenStore struct {
cli clienter
ns string
}

// Close close the store
func (s *TokenStore) Close() error {
return s.cli.Close()
}

func (s *TokenStore) wrapperKey(key string) string {
return fmt.Sprintf("%s%s", s.ns, key)
}

// Create Create and store the new token information
func (s *TokenStore) Create(info oauth2.TokenInfo) (err error) {
ct := time.Now()
Expand All @@ -73,7 +89,7 @@ func (s *TokenStore) Create(info oauth2.TokenInfo) (err error) {

pipe := s.cli.TxPipeline()
if code := info.GetCode(); code != "" {
pipe.Set(code, jv, info.GetCodeExpiresIn())
pipe.Set(s.wrapperKey(code), jv, info.GetCodeExpiresIn())
} else {
basicID := uuid.Must(uuid.NewRandom()).String()
aexp := info.GetAccessExpiresIn()
Expand All @@ -84,11 +100,11 @@ func (s *TokenStore) Create(info oauth2.TokenInfo) (err error) {
if aexp.Seconds() > rexp.Seconds() {
aexp = rexp
}
pipe.Set(refresh, basicID, rexp)
pipe.Set(s.wrapperKey(refresh), basicID, rexp)
}

pipe.Set(info.GetAccess(), basicID, aexp)
pipe.Set(basicID, jv, rexp)
pipe.Set(s.wrapperKey(info.GetAccess()), basicID, aexp)
pipe.Set(s.wrapperKey(basicID), jv, rexp)
}

if _, verr := pipe.Exec(); verr != nil {
Expand All @@ -99,7 +115,7 @@ func (s *TokenStore) Create(info oauth2.TokenInfo) (err error) {

// remove
func (s *TokenStore) remove(key string) (err error) {
_, verr := s.cli.Del(key).Result()
_, verr := s.cli.Del(s.wrapperKey(key)).Result()
if verr != redis.Nil {
err = verr
}
Expand All @@ -125,29 +141,32 @@ func (s *TokenStore) RemoveByRefresh(refresh string) (err error) {
}

func (s *TokenStore) getData(key string) (ti oauth2.TokenInfo, err error) {
result := s.cli.Get(key)
result := s.cli.Get(s.wrapperKey(key))
if verr := result.Err(); verr != nil {
if verr == redis.Nil {
return
}
err = verr
return
}

iv, err := result.Bytes()
if err != nil {
return
}

var tm models.Token
if verr := jsonUnmarshal(iv, &tm); verr != nil {
err = verr
return
}

ti = &tm
return
}

func (s *TokenStore) getBasicID(token string) (basicID string, err error) {
tv, verr := s.cli.Get(token).Result()
tv, verr := s.cli.Get(s.wrapperKey(token)).Result()
if verr != nil {
if verr == redis.Nil {
return
Expand Down
11 changes: 5 additions & 6 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func TestTokenStore(t *testing.T) {
Addr: addr,
DB: db,
}
store, err := redis.NewRedisStore(opts)
So(err, ShouldBeNil)
store := redis.NewRedisStore(opts)

Convey("Test authorization code store", func() {
info := &models.Token{
Expand Down Expand Up @@ -106,12 +105,12 @@ func TestTokenStore(t *testing.T) {

func TestTokenStoreWithKeyNamespace(t *testing.T) {
Convey("Test redis token store", t, func() {
cfg := &redis.Config{
Addr: "127.0.0.1:6379",
opts := &redis.Options{
Addr: addr,
DB: db,
KeyNamespace: "test:",
}
store, err := redis.NewTokenStore(cfg)
So(err, ShouldBeNil)
store := redis.NewRedisStore(opts)

Convey("Test authorization code store", func() {
info := &models.Token{
Expand Down

0 comments on commit de96075

Please sign in to comment.