feat: add otp model + implementation for sql
This commit is contained in:
parent
9ef5f33f7a
commit
f6c67243b9
|
@ -9,6 +9,7 @@ type CollectionList struct {
|
||||||
Webhook string
|
Webhook string
|
||||||
WebhookLog string
|
WebhookLog string
|
||||||
EmailTemplate string
|
EmailTemplate string
|
||||||
|
OTP string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -23,5 +24,6 @@ var (
|
||||||
Webhook: Prefix + "webhook",
|
Webhook: Prefix + "webhook",
|
||||||
WebhookLog: Prefix + "webhook_log",
|
WebhookLog: Prefix + "webhook_log",
|
||||||
EmailTemplate: Prefix + "email_template",
|
EmailTemplate: Prefix + "email_template",
|
||||||
|
OTP: Prefix + "otps",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
12
server/db/models/otp.go
Normal file
12
server/db/models/otp.go
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
// OTP model for database
|
||||||
|
type OTP struct {
|
||||||
|
Key string `json:"_key,omitempty" bson:"_key,omitempty" cql:"_key,omitempty"` // for arangodb
|
||||||
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id"`
|
||||||
|
Email string `gorm:"unique" json:"email" bson:"email" cql:"email"`
|
||||||
|
Otp string `json:"otp" bson:"otp" cql:"otp"`
|
||||||
|
ExpiresAt int64 `json:"expires_at" bson:"expires_at" cql:"expires_at"`
|
||||||
|
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at"`
|
||||||
|
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at"`
|
||||||
|
}
|
38
server/db/providers/arangodb/otp.go
Normal file
38
server/db/providers/arangodb/otp.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package arangodb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddOTP to add otp
|
||||||
|
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
|
if otp.ID == "" {
|
||||||
|
otp.ID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
otp.Key = otp.ID
|
||||||
|
otp.CreatedAt = time.Now().Unix()
|
||||||
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
|
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()
|
||||||
|
return otp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOTPByEmail to get otp for a given email address
|
||||||
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
return nil
|
||||||
|
}
|
38
server/db/providers/cassandradb/otp.go
Normal file
38
server/db/providers/cassandradb/otp.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package cassandradb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddOTP to add otp
|
||||||
|
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
|
if otp.ID == "" {
|
||||||
|
otp.ID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
otp.Key = otp.ID
|
||||||
|
otp.CreatedAt = time.Now().Unix()
|
||||||
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
|
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()
|
||||||
|
return otp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOTPByEmail to get otp for a given email address
|
||||||
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
return nil
|
||||||
|
}
|
38
server/db/providers/mongodb/otp.go
Normal file
38
server/db/providers/mongodb/otp.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package mongodb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddOTP to add otp
|
||||||
|
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
|
if otp.ID == "" {
|
||||||
|
otp.ID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
otp.Key = otp.ID
|
||||||
|
otp.CreatedAt = time.Now().Unix()
|
||||||
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
|
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()
|
||||||
|
return otp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOTPByEmail to get otp for a given email address
|
||||||
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
return nil
|
||||||
|
}
|
32
server/db/providers/provider_template/otp.go
Normal file
32
server/db/providers/provider_template/otp.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package provider_template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddOTP to add otp
|
||||||
|
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
|
if otp.ID == "" {
|
||||||
|
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
|
||||||
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -72,4 +72,13 @@ type Provider interface {
|
||||||
GetEmailTemplateByEventName(ctx context.Context, eventName string) (*model.EmailTemplate, error)
|
GetEmailTemplateByEventName(ctx context.Context, eventName string) (*model.EmailTemplate, error)
|
||||||
// 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
|
||||||
|
AddOTP(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(ctx context.Context, emailAddress string) (*models.OTP, error)
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
DeleteOTP(ctx context.Context, otp *models.OTP) error
|
||||||
}
|
}
|
||||||
|
|
60
server/db/providers/sql/otp.go
Normal file
60
server/db/providers/sql/otp.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
package sql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddOTP to add otp
|
||||||
|
func (p *provider) AddOTP(ctx context.Context, otp *models.OTP) (*models.OTP, error) {
|
||||||
|
if otp.ID == "" {
|
||||||
|
otp.ID = uuid.New().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
otp.Key = otp.ID
|
||||||
|
otp.CreatedAt = time.Now().Unix()
|
||||||
|
otp.UpdatedAt = time.Now().Unix()
|
||||||
|
|
||||||
|
res := p.db.Create(&otp)
|
||||||
|
if res.Error != nil {
|
||||||
|
return nil, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
func (p *provider) GetOTPByEmail(ctx context.Context, emailAddress string) (*models.OTP, error) {
|
||||||
|
var otp models.OTP
|
||||||
|
|
||||||
|
result := p.db.Where("email = ?", emailAddress).First(&otp)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &otp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteOTP to delete otp
|
||||||
|
func (p *provider) DeleteOTP(ctx context.Context, otp *models.OTP) error {
|
||||||
|
result := p.db.Delete(&models.OTP{
|
||||||
|
ID: otp.ID,
|
||||||
|
})
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -60,7 +60,7 @@ func NewProvider() (*provider, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = sqlDB.AutoMigrate(&models.User{}, &models.VerificationRequest{}, &models.Session{}, &models.Env{}, &models.Webhook{}, models.WebhookLog{}, models.EmailTemplate{})
|
err = sqlDB.AutoMigrate(&models.User{}, &models.VerificationRequest{}, &models.Session{}, &models.Env{}, &models.Webhook{}, models.WebhookLog{}, models.EmailTemplate{}, &models.OTP{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user