-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store/redis: redis backend for hydra (#313)
Signed-off-by: Son Dinh <son.dinh@blacksquaremedia.com> * oauth2: Add Redis manager * jwk: Add Redis manager * cmd/server: Add Redis handlers to factories * config: Add Redis connections * core: Update documentation; update Redis deps * docker: Add redis container to compose * oauth2/redis: Remove tokens signatures from set store on revoke * cmd/host: Change Redis documentation port to database default * docker: Comment out non-default Hydra backends on compose
- Loading branch information
Showing
15 changed files
with
745 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/imdario/mergo" | ||
"github.com/ory-am/fosite" | ||
"github.com/ory-am/hydra/pkg" | ||
"github.com/pborman/uuid" | ||
"github.com/pkg/errors" | ||
"gopkg.in/redis.v5" | ||
) | ||
|
||
type RedisManager struct { | ||
DB *redis.Client | ||
Hasher fosite.Hasher | ||
KeyPrefix string | ||
} | ||
|
||
const redisClientTemplate = "hydra:client" | ||
|
||
func (m *RedisManager) redisClientKey() string { | ||
return m.KeyPrefix + redisClientTemplate | ||
} | ||
|
||
func (m *RedisManager) GetConcreteClient(id string) (*Client, error) { | ||
resp, err := m.DB.HGet(m.redisClientKey(), id).Bytes() | ||
if err == redis.Nil { | ||
return nil, errors.Wrap(pkg.ErrNotFound, "") | ||
} else if err != nil { | ||
return nil, errors.Wrap(err, "") | ||
} | ||
|
||
var d Client | ||
if err := json.Unmarshal(resp, &d); err != nil { | ||
return nil, errors.Wrap(err, "") | ||
} | ||
|
||
return &d, nil | ||
} | ||
|
||
func (m *RedisManager) GetClient(id string) (fosite.Client, error) { | ||
return m.GetConcreteClient(id) | ||
} | ||
|
||
func (m *RedisManager) UpdateClient(c *Client) error { | ||
o, err := m.GetClient(c.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Secret == "" { | ||
c.Secret = string(o.GetHashedSecret()) | ||
} else { | ||
h, err := m.Hasher.Hash([]byte(c.Secret)) | ||
if err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
c.Secret = string(h) | ||
} | ||
if err := mergo.Merge(c, o); err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
|
||
s, err := json.Marshal(c) | ||
if err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
|
||
if err := m.DB.HSet(m.redisClientKey(), c.ID, string(s)).Err(); err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *RedisManager) Authenticate(id string, secret []byte) (*Client, error) { | ||
c, err := m.GetConcreteClient(id) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "") | ||
} | ||
|
||
if err := m.Hasher.Compare(c.GetHashedSecret(), secret); err != nil { | ||
return nil, errors.Wrap(err, "") | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (m *RedisManager) CreateClient(c *Client) error { | ||
if c.ID == "" { | ||
c.ID = uuid.New() | ||
} | ||
|
||
hash, err := m.Hasher.Hash([]byte(c.Secret)) | ||
if err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
c.Secret = string(hash) | ||
|
||
s, err := json.Marshal(c) | ||
if err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
|
||
if err := m.DB.HSetNX(m.redisClientKey(), c.ID, string(s)).Err(); err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
return nil | ||
} | ||
|
||
func (m *RedisManager) DeleteClient(id string) error { | ||
if _, err := m.DB.HDel(m.redisClientKey(), id).Result(); err != nil { | ||
return errors.Wrap(err, "") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *RedisManager) GetClients() (map[string]Client, error) { | ||
clients := make(map[string]Client) | ||
|
||
iter := m.DB.HScan(m.redisClientKey(), 0, "", 0).Iterator() | ||
for iter.Next() { | ||
if !iter.Next() { | ||
break | ||
} | ||
|
||
resp := iter.Val() | ||
|
||
var d Client | ||
if err := json.Unmarshal([]byte(resp), &d); err != nil { | ||
return nil, errors.Wrap(err, "") | ||
} | ||
|
||
clients[d.ID] = d | ||
} | ||
if err := iter.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return clients, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.