feat(auth): api for adding totp

This commit is contained in:
Denis Gukov 2025-01-01 22:44:55 +05:00
parent 21263baf17
commit a5ad050d3e
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
3 changed files with 37 additions and 3 deletions

View File

@ -276,7 +276,7 @@ type Store interface {
UpdateUser(user UserWithPwd) error
SetUserPassword(userID int, password string) error
AddUserTotpVerification(userID int, secret string) (UserTotp, error)
DeleteTotpVerification(userID int) error
DeleteTotpVerification(userID int, totpID int) error
GetUser(userID int) (User, error)
GetUserByLoginOrEmail(login string, email string) (User, error)
@ -534,6 +534,12 @@ var TemplateVaultProps = ObjectProps{
ReferringColumnSuffix: "template_id",
}
var UserTotpProps = ObjectProps{
TableName: "user__totp",
Type: reflect.TypeOf(UserTotp{}),
PrimaryColumnName: "id",
}
func (p ObjectProps) GetReferringFieldsFrom(t reflect.Type) (fields []string, err error) {
n := t.NumField()
for i := 0; i < n; i++ {

View File

@ -216,3 +216,31 @@ func (d *BoltDb) GetAllAdmins() (users []db.User, err error) {
}, &users)
return
}
func (d *BoltDb) AddUserTotpVerification(userID int, secret string) (totp db.UserTotp, err error) {
current := make([]db.UserTotp, 0)
err = d.getObjects(userID, db.UserTotpProps, db.RetrieveQueryParams{}, nil, current)
if len(current) > 0 {
err = fmt.Errorf("already exists")
return
}
totp.UserID = userID
totp.Secret = secret
totp.Created = db.GetParsedTime(time.Now().UTC())
newTotp, err := d.createObject(userID, db.UserTotpProps, totp)
if err != nil {
return
}
totp = newTotp.(db.UserTotp)
return
}
func (d *BoltDb) DeleteTotpVerification(userID int, totpID int) error {
return d.deleteObject(userID, db.UserTotpProps, intObjectID(totpID), nil)
}

View File

@ -287,7 +287,7 @@ func (d *SqlDb) AddUserTotpVerification(userID int, secret string) (totp db.User
return
}
func (d *SqlDb) DeleteTotpVerification(userID int) error {
_, err := d.exec("delete from user__totp where user_id=?", userID)
func (d *SqlDb) DeleteTotpVerification(userID int, totpID int) error {
_, err := d.exec("delete from user__totp where user_id=? and id = ?", userID, totpID)
return err
}