feat: use upsert for otp + implement otp methods for cassandradb
This commit is contained in:
parent
22ae3bca54
commit
f6029fb7bf
|
@ -9,30 +9,28 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
func (p *provider) UpsertOTP(ctx context.Context, otpParam *models.OTP) (*models.OTP, error) {
|
||||||
if otp.ID == "" {
|
otp, _ := p.GetOTPByEmail(ctx, otpParam.Email)
|
||||||
|
shouldCreate := false
|
||||||
|
if otp == nil {
|
||||||
|
shouldCreate = true
|
||||||
otp.ID = uuid.New().String()
|
otp.ID = uuid.New().String()
|
||||||
}
|
|
||||||
|
|
||||||
otp.Key = otp.ID
|
otp.Key = otp.ID
|
||||||
otp.CreatedAt = time.Now().Unix()
|
otp.CreatedAt = time.Now().Unix()
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
} else {
|
||||||
|
otp = otpParam
|
||||||
|
}
|
||||||
|
|
||||||
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
otpCollection, _ := p.db.Collection(ctx, models.Collections.OTP)
|
otpCollection, _ := p.db.Collection(ctx, models.Collections.OTP)
|
||||||
|
|
||||||
|
if shouldCreate {
|
||||||
_, err := otpCollection.CreateDocument(ctx, otp)
|
_, err := otpCollection.CreateDocument(ctx, otp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
return otp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateOTP to update otp for a given email address
|
|
||||||
func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
otpCollection, _ := p.db.Collection(ctx, models.Collections.OTP)
|
|
||||||
meta, err := otpCollection.UpdateDocument(ctx, otp.Key, otp)
|
meta, err := otpCollection.UpdateDocument(ctx, otp.Key, otp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -40,6 +38,8 @@ func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP,
|
||||||
|
|
||||||
otp.Key = meta.Key
|
otp.Key = meta.Key
|
||||||
otp.ID = meta.ID.String()
|
otp.ID = meta.ID.String()
|
||||||
|
}
|
||||||
|
|
||||||
return otp, nil
|
return otp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,37 +2,60 @@ package cassandradb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/db/models"
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/gocql/gocql"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
func (p *provider) UpsertOTP(ctx context.Context, otpParam *models.OTP) (*models.OTP, error) {
|
||||||
if otp.ID == "" {
|
otp, _ := p.GetOTPByEmail(ctx, otpParam.Email)
|
||||||
|
shouldCreate := false
|
||||||
|
if otp == nil {
|
||||||
|
shouldCreate = true
|
||||||
otp.ID = uuid.New().String()
|
otp.ID = uuid.New().String()
|
||||||
}
|
|
||||||
|
|
||||||
otp.Key = otp.ID
|
otp.Key = otp.ID
|
||||||
otp.CreatedAt = time.Now().Unix()
|
otp.CreatedAt = time.Now().Unix()
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
} else {
|
||||||
|
otp = otpParam
|
||||||
|
}
|
||||||
|
|
||||||
return otp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateOTP to update otp for a given email address
|
|
||||||
func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
query := ""
|
||||||
|
|
||||||
|
if shouldCreate {
|
||||||
|
query = fmt.Sprintf(`INSERT INTO %s (id, email, otp, expires_at, created_at, updated_at) VALUES ('%s', '%s', '%s', %d, %d, %d)`, KeySpace+"."+models.Collections.OTP, otp.ID, otp.Email, otp.Otp, otp.ExpiresAt, otp.CreatedAt, otp.UpdatedAt)
|
||||||
|
} else {
|
||||||
|
query = fmt.Sprintf(`UPDATE %s SET otp = '%s', expires_at = %d, updated_at = %d WHERE email = '%s'`, KeySpace+"."+models.Collections.OTP, otp.Otp, otp.ExpiresAt, otp.UpdatedAt, otp.Email)
|
||||||
|
}
|
||||||
|
err := p.db.Query(query).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return otp, nil
|
return otp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOTPByEmail to get otp for a given email address
|
// GetOTPByEmail to get otp for a given email address
|
||||||
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
return nil, nil
|
var otp models.OTP
|
||||||
|
query := fmt.Sprintf(`SELECT id, email, otp, expires_at, created_at, updated_at FROM %s WHERE email = '%s' LIMIT 1 ALLOW FILTERING`, KeySpace+"."+models.Collections.OTP, emailAddress)
|
||||||
|
err := p.db.Query(query).Consistency(gocql.One).Scan(&otp.ID, &otp.Email, &otp.Otp, &otp.ExpiresAt, &otp.CreatedAt, &otp.UpdatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &otp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteOTP to delete otp
|
// DeleteOTP to delete otp
|
||||||
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
query := fmt.Sprintf("DELETE FROM %s WHERE id = '%s'", KeySpace+"."+models.Collections.OTP, otp.ID)
|
||||||
|
err := p.db.Query(query).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,6 +221,17 @@ func NewProvider() (*provider, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
otpCollection := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, email text, otp text, expires_at bigint, updated_at bigint, created_at bigint, PRIMARY KEY (id))", KeySpace, models.Collections.OTP)
|
||||||
|
err = session.Query(otpCollection).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
otpIndexQuery := fmt.Sprintf("CREATE INDEX IF NOT EXISTS authorizer_otp_email ON %s.%s (email)", KeySpace, models.Collections.OTP)
|
||||||
|
err = session.Query(otpIndexQuery).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &provider{
|
return &provider{
|
||||||
db: session,
|
db: session,
|
||||||
}, err
|
}, err
|
||||||
|
|
|
@ -10,31 +10,20 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
func (p *provider) UpsertOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
if otp.ID == "" {
|
if otp.ID == "" {
|
||||||
otp.ID = uuid.New().String()
|
otp.ID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
otp.Key = otp.ID
|
otp.Key = otp.ID
|
||||||
|
if otp.CreatedAt <= 0 {
|
||||||
otp.CreatedAt = time.Now().Unix()
|
otp.CreatedAt = time.Now().Unix()
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
otpCollection := p.db.Collection(models.Collections.OTP, options.Collection())
|
|
||||||
_, err := otpCollection.InsertOne(ctx, otp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return otp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateOTP to update otp for a given email address
|
|
||||||
func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
otpCollection := p.db.Collection(models.Collections.OTP, options.Collection())
|
otpCollection := p.db.Collection(models.Collections.OTP, options.Collection())
|
||||||
_, err := otpCollection.UpdateOne(ctx, bson.M{"_id": bson.M{"$eq": otp.ID}}, bson.M{"$set": otp}, options.MergeUpdateOptions())
|
_, err := otpCollection.UpdateOne(ctx, bson.M{"_id": bson.M{"$eq": otp.ID}}, bson.M{"$set": otp}, options.MergeUpdateOptions().SetUpsert(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,23 +2,13 @@ package provider_template
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/db/models"
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
func (p *provider) UpsertOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
if otp.ID == "" {
|
return nil, nil
|
||||||
otp.ID = uuid.New().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
otp.Key = otp.ID
|
|
||||||
otp.CreatedAt = time.Now().Unix()
|
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
return otp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOTPByEmail to get otp for a given email address
|
// GetOTPByEmail to get otp for a given email address
|
||||||
|
|
|
@ -73,10 +73,8 @@ type Provider interface {
|
||||||
// DeleteEmailTemplate to delete EmailTemplate
|
// DeleteEmailTemplate to delete EmailTemplate
|
||||||
DeleteEmailTemplate(ctx context.Context, emailTemplate *model.EmailTemplate) error
|
DeleteEmailTemplate(ctx context.Context, emailTemplate *model.EmailTemplate) error
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error)
|
UpsertOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error)
|
||||||
// UpdateOTP to update otp for a given email address
|
|
||||||
UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error)
|
|
||||||
// GetOTPByEmail to get otp for a given email address
|
// GetOTPByEmail to get otp for a given email address
|
||||||
GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error)
|
GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error)
|
||||||
// DeleteOTP to delete otp
|
// DeleteOTP to delete otp
|
||||||
|
|
|
@ -6,10 +6,11 @@ import (
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/db/models"
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOTP to add otp
|
// UpsertOTP to add or update otp
|
||||||
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
func (p *provider) UpsertOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
if otp.ID == "" {
|
if otp.ID == "" {
|
||||||
otp.ID = uuid.New().String()
|
otp.ID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
@ -18,7 +19,10 @@ func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, er
|
||||||
otp.CreatedAt = time.Now().Unix()
|
otp.CreatedAt = time.Now().Unix()
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
res := p.db.Create(&otp)
|
res := p.db.Clauses(clause.OnConflict{
|
||||||
|
Columns: []clause.Column{{Name: "email"}},
|
||||||
|
DoUpdates: clause.AssignmentColumns([]string{"otp", "expires_at", "updated_at"}),
|
||||||
|
}).Create(&otp)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return nil, res.Error
|
return nil, res.Error
|
||||||
}
|
}
|
||||||
|
@ -26,17 +30,6 @@ func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, er
|
||||||
return otp, nil
|
return otp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateOTP to update otp for a given email address
|
|
||||||
func (p *provider) UpdateOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
|
||||||
otp.UpdatedAt = time.Now().Unix()
|
|
||||||
|
|
||||||
res := p.db.Save(&otp)
|
|
||||||
if res.Error != nil {
|
|
||||||
return nil, res.Error
|
|
||||||
}
|
|
||||||
return otp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetOTPByEmail to get otp for a given email address
|
// GetOTPByEmail to get otp for a given email address
|
||||||
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
var otp *models.OTP
|
var otp *models.OTP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user