authorizer/server/db/providers/couchbase/otp.go

106 lines
2.6 KiB
Go
Raw Normal View History

2022-10-09 18:54:14 +00:00
package couchbase
import (
"context"
2022-11-26 12:13:39 +00:00
"fmt"
"time"
2022-10-09 18:54:14 +00:00
"github.com/authorizerdev/authorizer/server/db/models"
2022-11-26 12:13:39 +00:00
"github.com/couchbase/gocb/v2"
"github.com/google/uuid"
2022-10-09 18:54:14 +00:00
)
// UpsertOTP to add or update otp
2022-11-26 12:13:39 +00:00
func (p *provider) UpsertOTP(ctx context.Context, otpParam *models.OTP) (*models.OTP, error) {
// otp, _ = p.GetOTPByEmail(ctx, otp.Email)
// if otp == nil {
// id := uuid.NewString()
// otp = &models.OTP{
// ID: id,
// Key: id,
// Otp: otp.Otp,
// Email: otp.Email,
// ExpiresAt: otp.ExpiresAt,
// CreatedAt: time.Now().Unix(),
// }
// }
// otp.UpdatedAt = time.Now().Unix()
// unsertOpt := gocb.UpsertOptions{
// Context: ctx,
// }
// _, err := p.db.Collection(models.Collections.OTP).Upsert(otp.ID, otp, &unsertOpt)
// if err != nil {
// return nil, err
// }
// return otp, nil
otp, _ := p.GetOTPByEmail(ctx, otpParam.Email)
shouldCreate := false
if otp == nil {
shouldCreate = true
otp = &models.OTP{
ID: uuid.NewString(),
Otp: otpParam.Otp,
Email: otpParam.Email,
ExpiresAt: otpParam.ExpiresAt,
CreatedAt: time.Now().Unix(),
UpdatedAt: time.Now().Unix(),
}
} else {
otp.Otp = otpParam.Otp
otp.ExpiresAt = otpParam.ExpiresAt
}
otp.UpdatedAt = time.Now().Unix()
if shouldCreate {
insertOpt := gocb.InsertOptions{
Context: ctx,
}
_, err := p.db.Collection(models.Collections.OTP).Insert(otp.ID, otp, &insertOpt)
if err != nil {
return otp, err
}
} else {
2022-12-01 09:28:35 +00:00
query := fmt.Sprintf(`UPDATE auth._default.%s SET otp="%s", expires_at=%d, updated_at=%d WHERE _id="%s"`, models.Collections.OTP, otp.Otp, otp.ExpiresAt, otp.UpdatedAt, otp.ID)
2022-11-26 12:13:39 +00:00
scope := p.db.Scope("_default")
_, err := scope.Query(query, &gocb.QueryOptions{})
if err != nil {
return otp, err
}
}
return otp, nil
2022-10-09 18:54:14 +00:00
}
// GetOTPByEmail to get otp for a given email address
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
2022-11-26 12:13:39 +00:00
otp := models.OTP{}
query := fmt.Sprintf(`SELECT _id, email, otp, expires_at, created_at, updated_at FROM auth._default.%s WHERE email = '%s' LIMIT 1`, models.Collections.OTP, emailAddress)
q, err := p.db.Scope("_default").Query(query, &gocb.QueryOptions{
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
})
if err != nil {
return nil, err
}
err = q.One(&otp)
if err != nil {
return nil, err
}
return &otp, nil
2022-10-09 18:54:14 +00:00
}
// DeleteOTP to delete otp
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
2022-11-26 12:13:39 +00:00
removeOpt := gocb.RemoveOptions{
Context: ctx,
}
_, err := p.db.Collection(models.Collections.OTP).Remove(otp.ID, &removeOpt)
if err != nil {
return err
}
2022-10-09 18:54:14 +00:00
return nil
}