Skip to content

Commit ba0cf18

Browse files
committed
userid ass mfa session key
1 parent 9f52c08 commit ba0cf18

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

server/memorystore/providers/inmemory/store.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ func (c *provider) DeleteSessionForNamespace(namespace string) error {
4242
return nil
4343
}
4444

45-
// SetMfaSession sets the mfa session with key and value of email
46-
func (c *provider) SetMfaSession(email, key string, expiration int64) error {
47-
c.mfasessionStore.Set(email, key, email, expiration)
45+
// SetMfaSession sets the mfa session with key and value of userId
46+
func (c *provider) SetMfaSession(userId, key string, expiration int64) error {
47+
c.mfasessionStore.Set(userId, key, userId, expiration)
4848
return nil
4949
}
5050

5151
// GetMfaSession returns value of given mfa session
52-
func (c *provider) GetMfaSession(email, key string) (string, error) {
53-
val := c.mfasessionStore.Get(email, key)
52+
func (c *provider) GetMfaSession(userId, key string) (string, error) {
53+
val := c.mfasessionStore.Get(userId, key)
5454
if val == "" {
5555
return "", fmt.Errorf("Not found")
5656
}
5757
return val, nil
5858
}
5959

6060
// DeleteMfaSession deletes given mfa session from in-memory store.
61-
func (c *provider) DeleteMfaSession(email, key string) error {
62-
c.mfasessionStore.Remove(email, key)
61+
func (c *provider) DeleteMfaSession(userId, key string) error {
62+
c.mfasessionStore.Remove(userId, key)
6363
return nil
6464
}
6565

server/memorystore/providers/providers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ type Provider interface {
1212
DeleteAllUserSessions(userId string) error
1313
// DeleteSessionForNamespace deletes the session for a given namespace
1414
DeleteSessionForNamespace(namespace string) error
15-
// SetMfaSession sets the mfa session with key and value of email
16-
SetMfaSession(email, key string, expiration int64) error
15+
// SetMfaSession sets the mfa session with key and value of userId
16+
SetMfaSession(userId, key string, expiration int64) error
1717
// GetMfaSession returns value of given mfa session
18-
GetMfaSession(email, key string) (string, error)
18+
GetMfaSession(userId, key string) (string, error)
1919
// DeleteMfaSession deletes given mfa session from in-memory store.
20-
DeleteMfaSession(email, key string) error
20+
DeleteMfaSession(userId, key string) error
2121

2222
// SetState sets the login state (key, value form) in the session store
2323
SetState(key, state string) error

server/memorystore/providers/redis/store.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,31 @@ func (c *provider) DeleteSessionForNamespace(namespace string) error {
9393
return nil
9494
}
9595

96-
// SetMfaSession sets the mfa session with key and value of email
97-
func (c *provider) SetMfaSession(email, key string, expiration int64) error {
96+
// SetMfaSession sets the mfa session with key and value of userId
97+
func (c *provider) SetMfaSession(userId, key string, expiration int64) error {
9898
currentTime := time.Now()
9999
expireTime := time.Unix(expiration, 0)
100100
duration := expireTime.Sub(currentTime)
101-
err := c.store.Set(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key), email, duration).Err()
101+
err := c.store.Set(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, key), userId, duration).Err()
102102
if err != nil {
103103
log.Debug("Error saving user session to redis: ", err)
104104
return err
105105
}
106106
return nil
107107
}
108108

109-
// GetMfaSession returns value of given mfa session
110-
func (c *provider) GetMfaSession(email, key string) (string, error) {
111-
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Result()
109+
// GetMfaSession returns value of given mfa session
110+
func (c *provider) GetMfaSession(userId, key string) (string, error) {
111+
data, err := c.store.Get(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, key)).Result()
112112
if err != nil {
113113
return "", err
114114
}
115115
return data, nil
116116
}
117117

118118
// DeleteMfaSession deletes given mfa session from in-memory store.
119-
func (c *provider) DeleteMfaSession(email, key string) error {
120-
if err := c.store.Del(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, email, key)).Err(); err != nil {
119+
func (c *provider) DeleteMfaSession(userId, key string) error {
120+
if err := c.store.Del(c.ctx, fmt.Sprintf("%s%s:%s", mfaSessionPrefix, userId, key)).Err(); err != nil {
121121
log.Debug("Error deleting user session from redis: ", err)
122122
// continue
123123
}

server/resolvers/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
125125
}
126126

127127
mfaSession := uuid.NewString()
128-
err = memorystore.Provider.SetMfaSession(params.Email, mfaSession, expires)
128+
err = memorystore.Provider.SetMfaSession(user.ID, mfaSession, expires)
129129
if err != nil {
130130
log.Debug("Failed to add mfasession: ", err)
131131
return nil, err

server/resolvers/mobile_login.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,25 @@ func MobileLoginResolver(ctx context.Context, params model.MobileLoginInput) (*m
122122
smsBody := strings.Builder{}
123123
smsBody.WriteString("Your verification code is: ")
124124
smsBody.WriteString(smsCode)
125+
expires := time.Now().Add(duration).Unix()
125126
_, err := db.Provider.UpsertOTP(ctx, &models.OTP{
126127
PhoneNumber: params.PhoneNumber,
127128
Otp: smsCode,
128-
ExpiresAt: time.Now().Add(duration).Unix(),
129+
ExpiresAt: expires,
129130
})
130131
if err != nil {
131132
log.Debug("error while upserting OTP: ", err.Error())
132133
return nil, err
133134
}
135+
136+
mfaSession := uuid.NewString()
137+
err = memorystore.Provider.SetMfaSession(user.ID, mfaSession, expires)
138+
if err != nil {
139+
log.Debug("Failed to add mfasession: ", err)
140+
return nil, err
141+
}
142+
cookie.SetMfaSession(gc, mfaSession)
143+
134144
go func() {
135145
utils.RegisterEvent(ctx, constants.UserLoginWebhookEvent, constants.AuthRecipeMethodMobileBasicAuth, *user)
136146
smsproviders.SendSMS(params.PhoneNumber, smsBody.String())

server/resolvers/verify_otp.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod
2727
log.Debug("Failed to get GinContext: ", err)
2828
return res, err
2929
}
30+
31+
mfaSession, err := cookie.GetMfaSession(gc)
32+
if err != nil {
33+
log.Debug("Failed to get otp request by email: ", err)
34+
return res, fmt.Errorf(`invalid session: %s`, err.Error())
35+
}
36+
3037
if refs.StringValue(params.Email) == "" && refs.StringValue(params.PhoneNumber) == "" {
3138
log.Debug("Email or phone number is required")
3239
return res, fmt.Errorf(`email or phone_number is required`)
@@ -68,6 +75,12 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod
6875
log.Debug("Failed to get user by email: ", err)
6976
return res, err
7077
}
78+
79+
if _, err := memorystore.Provider.GetMfaSession(user.ID, mfaSession); err != nil {
80+
log.Debug("Failed to get mfa session: ", err)
81+
return res, fmt.Errorf(`invalid session: %s`, err.Error())
82+
}
83+
7184
isSignUp := user.EmailVerifiedAt == nil && user.PhoneNumberVerifiedAt == nil
7285
// TODO - Add Login method in DB when we introduce OTP for social media login
7386
loginMethod := constants.AuthRecipeMethodBasicAuth

0 commit comments

Comments
 (0)