feat: add mfa session to secure otp login

This commit is contained in:
catusax
2023-07-20 15:11:39 +08:00
parent 87a962504f
commit 5018462559
9 changed files with 185 additions and 9 deletions

View File

@@ -16,6 +16,8 @@ var (
envStorePrefix = "authorizer_env"
)
const mfaSessionPrefix = "mfa_sess_"
// SetUserSession sets the user session for given user identifier in form recipe:user_id
func (c *provider) SetUserSession(userId, key, token string, expiration int64) error {
currentTime := time.Now()
@@ -91,6 +93,34 @@ func (c *provider) DeleteSessionForNamespace(namespace string) error {
return nil
}
func (c *provider) SetMfaSession(email, key string, expiration int64) error {
currentTime := time.Now()
expireTime := time.Unix(expiration, 0)
duration := expireTime.Sub(currentTime)
err := c.store.Set(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key), email, duration).Err()
if err != nil {
log.Debug("Error saving user session to redis: ", err)
return err
}
return nil
}
func (c *provider) GetMfaSession(email, key string) (string, error) {
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Result()
if err != nil {
return "", err
}
return data, nil
}
func (c *provider) DeleteMfaSession(email, key string) error {
if err := c.store.Del(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Err(); err != nil {
log.Debug("Error deleting user session from redis: ", err)
// continue
}
return nil
}
// SetState sets the state in redis store.
func (c *provider) SetState(key, value string) error {
err := c.store.Set(c.ctx, stateStorePrefix+key, value, 0).Err()