Rename account_recovery -> two_factor_recovery

This commit is contained in:
Neeraj Gupta 2024-03-05 14:51:43 +05:30 committed by Neeraj Gupta
parent 42e4364fda
commit f766484b2e
8 changed files with 30 additions and 29 deletions

View file

@ -5,7 +5,7 @@ import (
"database/sql"
b64 "encoding/base64"
"fmt"
"github.com/ente-io/museum/pkg/repo/accountrecovery"
"github.com/ente-io/museum/pkg/repo/two_factor_recovery"
"net/http"
"os"
"os/signal"
@ -138,7 +138,7 @@ func main() {
twoFactorRepo := &repo.TwoFactorRepository{DB: db, SecretEncryptionKey: secretEncryptionKeyBytes}
userAuthRepo := &repo.UserAuthRepository{DB: db}
accountRecoveryRepo := &accountrecovery.Repository{Db: db}
twoFactorRecoveryRepo := &two_factor_recovery.Repository{Db: db}
billingRepo := &repo.BillingRepository{DB: db}
userEntityRepo := &userEntityRepo.Repository{DB: db}
locationTagRepository := &locationtagRepo.Repository{DB: db}
@ -306,7 +306,7 @@ func main() {
usageRepo,
userAuthRepo,
twoFactorRepo,
accountRecoveryRepo,
twoFactorRecoveryRepo,
passkeysRepo,
storagBonusRepo,
fileRepo,

View file

@ -18,7 +18,7 @@ type ConfigurePassKeySkipRequest struct {
EncPassKeySkipSecret EncData `json:"encPassKeySkipSecret" binding:"required"`
}
type AccountRecoveryStatus struct {
type TwoFactorRecoveryStatus struct {
// AllowAdminReset is a boolean that determines if the admin can reset the user's MFA.
// If true, in the event that the user loses their MFA device, the admin can reset the user's MFA.
AllowAdminReset bool `json:"allowAdminReset" binding:"required"`

View file

@ -1,2 +0,0 @@
DROP TABLE IF NOT EXISTS account_recovery;
DROP TRIGGER IF EXISTS update_account_recovery_updated_at ON account_recovery;

View file

@ -0,0 +1,2 @@
DROP TABLE IF NOT EXISTS two_factor_recovery;
DROP TRIGGER IF EXISTS update_two_factor_recovery_updated_at ON two_factor_recovery;

View file

@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS account_recovery (
CREATE TABLE IF NOT EXISTS two_factor_recovery (
user_id bigint NOT NULL,
-- if false, the support team team will not be able to reset the MFA for the user
enable_admin_mfa_reset boolean NOT NULL DEFAULT true,
@ -10,9 +10,9 @@ CREATE TABLE IF NOT EXISTS account_recovery (
updated_at bigint NOT NULL DEFAULT now_utc_micro_seconds()
);
CREATE TRIGGER update_account_recovery_updated_at
CREATE TRIGGER update_two_factor_recovery_updated_at
BEFORE UPDATE
ON account_recovery
ON two_factor_recovery
FOR EACH ROW
EXECUTE PROCEDURE
trigger_updated_at_microseconds_column();

View file

@ -8,14 +8,14 @@ import (
)
// GetAccountRecoveryStatus returns a user's passkey reset status
func (c *UserController) GetAccountRecoveryStatus(ctx *gin.Context) (*ente.AccountRecoveryStatus, error) {
func (c *UserController) GetAccountRecoveryStatus(ctx *gin.Context) (*ente.TwoFactorRecoveryStatus, error) {
userID := auth.GetUserID(ctx.Request.Header)
return c.AccountRecoveryRepo.GetAccountRecoveryStatus(userID)
return c.TwoFactorRecoveryRepo.GetStatus(userID)
}
func (c *UserController) ConfigurePassKeySkip(ctx *gin.Context, req *ente.ConfigurePassKeySkipRequest) error {
userID := auth.GetUserID(ctx.Request.Header)
return c.AccountRecoveryRepo.ConfigurePassKeyRecovery(ctx, userID, req)
return c.TwoFactorRecoveryRepo.ConfigurePassKeyRecovery(ctx, userID, req)
}
func (c *UserController) GetPasskeySkipChallenge(ctx *gin.Context, passKeySessionID string) (*ente.EncData, error) {
@ -23,7 +23,7 @@ func (c *UserController) GetPasskeySkipChallenge(ctx *gin.Context, passKeySessio
if err != nil {
return nil, err
}
recoveryStatus, err := c.AccountRecoveryRepo.GetAccountRecoveryStatus(userID)
recoveryStatus, err := c.TwoFactorRecoveryRepo.GetStatus(userID)
if err != nil {
return nil, err
}
@ -31,7 +31,7 @@ func (c *UserController) GetPasskeySkipChallenge(ctx *gin.Context, passKeySessio
return nil, ente.NewBadRequestWithMessage("Passkey reset is not configured")
}
result, err := c.AccountRecoveryRepo.GetPasskeyResetChallenge(ctx, userID)
result, err := c.TwoFactorRecoveryRepo.GetPasskeyResetChallenge(ctx, userID)
if err != nil {
return nil, err
}
@ -46,7 +46,7 @@ func (c *UserController) SkipPassKey(context *gin.Context, req *ente.SkipPassKey
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
exists, err := c.AccountRecoveryRepo.VerifyRecoveryKeyForPassKey(userID, req.PassKeySkipSecret)
exists, err := c.TwoFactorRecoveryRepo.VerifyRecoveryKeyForPassKey(userID, req.PassKeySkipSecret)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}

View file

@ -3,7 +3,7 @@ package user
import (
"errors"
"fmt"
"github.com/ente-io/museum/pkg/repo/accountrecovery"
"github.com/ente-io/museum/pkg/repo/two_factor_recovery"
"strings"
cache2 "github.com/ente-io/museum/ente/cache"
@ -31,7 +31,7 @@ import (
// UserController exposes request handlers for all user related requests
type UserController struct {
UserRepo *repo.UserRepository
AccountRecoveryRepo *accountrecovery.Repository
TwoFactorRecoveryRepo *two_factor_recovery.Repository
UsageRepo *repo.UsageRepository
UserAuthRepo *repo.UserAuthRepository
TwoFactorRepo *repo.TwoFactorRepository
@ -101,7 +101,7 @@ func NewUserController(
usageRepo *repo.UsageRepository,
userAuthRepo *repo.UserAuthRepository,
twoFactorRepo *repo.TwoFactorRepository,
accountRecoveryRepo *accountrecovery.Repository,
twoFactorRecoveryRepo *two_factor_recovery.Repository,
passkeyRepo *passkey.Repository,
storageBonusRepo *storageBonusRepo.Repository,
fileRepo *repo.FileRepository,
@ -124,7 +124,7 @@ func NewUserController(
return &UserController{
UserRepo: userRepo,
UsageRepo: usageRepo,
AccountRecoveryRepo: accountRecoveryRepo,
TwoFactorRecoveryRepo: twoFactorRecoveryRepo,
UserAuthRepo: userAuthRepo,
StorageBonusRepo: storageBonusRepo,
TwoFactorRepo: twoFactorRepo,

View file

@ -1,4 +1,4 @@
package accountrecovery
package two_factor_recovery
import (
"context"
@ -11,35 +11,36 @@ type Repository struct {
Db *sql.DB
}
// GetAccountRecoveryStatus returns `ente.AccountRecoveryStatus` for a user
func (r *Repository) GetAccountRecoveryStatus(userID int64) (*ente.AccountRecoveryStatus, error) {
// GetStatus returns `ente.TwoFactorRecoveryStatus` for a user
func (r *Repository) GetStatus(userID int64) (*ente.TwoFactorRecoveryStatus, error) {
var isAdminResetEnabled bool
var resetKey sql.NullString
row := r.Db.QueryRow("SELECT enable_admin_mfa_reset, pass_key_reset_key FROM account_recovery WHERE user_id = $1", userID)
row := r.Db.QueryRow("SELECT enable_admin_mfa_reset, pass_key_reset_key FROM two_factor_recovery WHERE user_id = $1", userID)
err := row.Scan(&isAdminResetEnabled, &resetKey)
if err != nil {
if err == sql.ErrNoRows {
// by default, admin
return &ente.AccountRecoveryStatus{
return &ente.TwoFactorRecoveryStatus{
AllowAdminReset: true,
IsPassKeySkipEnabled: false,
}, nil
}
return nil, err
}
return &ente.AccountRecoveryStatus{AllowAdminReset: isAdminResetEnabled, IsPassKeySkipEnabled: resetKey.Valid}, nil
return &ente.TwoFactorRecoveryStatus{AllowAdminReset: isAdminResetEnabled, IsPassKeySkipEnabled: resetKey.Valid}, nil
}
func (r *Repository) ConfigurePassKeyRecovery(ctx context.Context, userID int64, req *ente.ConfigurePassKeySkipRequest) error {
_, err := r.Db.ExecContext(ctx, `INSERT INTO account_recovery (user_id, pass_key_reset_key, pass_key_reset_enc_data)
_, err := r.Db.ExecContext(ctx, `INSERT INTO two_factor_recovery (user_id, pass_key_reset_key, pass_key_reset_enc_data)
VALUES ($1, $2,$3) ON CONFLICT (user_id)
DO UPDATE SET pass_key_reset_key = $2, pass_key_reset_enc_data = $3`, userID, req.PassKeySkipKey, req.EncPassKeySkipSecret)
DO UPDATE SET pass_key_reset_key = $2, pass_key_reset_enc_data = $3`, userID, req.PassKeySkipSecret,
req.EncPassKeySkipSecret)
return err
}
func (r *Repository) GetPasskeyResetChallenge(ctx context.Context, userID int64) (*ente.EncData, error) {
var encData *ente.EncData
err := r.Db.QueryRowContext(ctx, "SELECT pass_key_reset_enc_data FROM account_recovery WHERE user_id= $1", userID).Scan(encData)
err := r.Db.QueryRowContext(ctx, "SELECT pass_key_reset_enc_data FROM two_factor_recovery WHERE user_id= $1", userID).Scan(encData)
if err != nil {
return nil, err
}
@ -49,7 +50,7 @@ func (r *Repository) GetPasskeyResetChallenge(ctx context.Context, userID int64)
// VerifyRecoveryKeyForPassKey checks if the passkey reset key is valid for a user
func (r *Repository) VerifyRecoveryKeyForPassKey(userID int64, passKeyResetKey string) (bool, error) {
var exists bool
row := r.Db.QueryRow(`SELECT EXISTS( SELECT 1 FROM account_recovery WHERE user_id = $1 AND pass_key_reset_key = $2)`, userID, passKeyResetKey)
row := r.Db.QueryRow(`SELECT EXISTS( SELECT 1 FROM two_factor_recovery WHERE user_id = $1 AND pass_key_reset_key = $2)`, userID, passKeyResetKey)
err := row.Scan(&exists)
if err != nil {
return false, stacktrace.Propagate(err, "")