2021-07-12 18:22:16 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-12-17 15:55:07 +00:00
|
|
|
"fmt"
|
2021-07-12 18:22:16 +00:00
|
|
|
"log"
|
2021-12-20 17:51:27 +00:00
|
|
|
"time"
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
"github.com/arangodb/go-driver"
|
2021-10-03 16:03:55 +00:00
|
|
|
"github.com/google/uuid"
|
2021-12-20 17:51:27 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2021-07-12 18:22:16 +00:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
2021-07-18 04:22:54 +00:00
|
|
|
type VerificationRequest struct {
|
2021-12-22 10:01:45 +00:00
|
|
|
Key string `json:"_key,omitempty" bson:"_key"` // for arangodb
|
2021-12-22 05:21:12 +00:00
|
|
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id"`
|
2021-12-20 17:51:27 +00:00
|
|
|
Token string `gorm:"type:text" json:"token" bson:"token"`
|
|
|
|
Identifier string `gorm:"uniqueIndex:idx_email_identifier" json:"identifier" bson:"identifier"`
|
|
|
|
ExpiresAt int64 `json:"expires_at" bson:"expires_at"`
|
|
|
|
CreatedAt int64 `gorm:"autoCreateTime" json:"created_at" bson:"created_at"`
|
|
|
|
UpdatedAt int64 `gorm:"autoUpdateTime" json:"updated_at" bson:"updated_at"`
|
|
|
|
Email string `gorm:"uniqueIndex:idx_email_identifier" json:"email" bson:"email"`
|
2021-10-03 16:03:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
// AddVerification function to add verification record
|
2021-07-18 04:22:54 +00:00
|
|
|
func (mgr *manager) AddVerification(verification VerificationRequest) (VerificationRequest, error) {
|
2021-12-17 15:55:07 +00:00
|
|
|
if verification.ID == "" {
|
|
|
|
verification.ID = uuid.New().String()
|
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
// copy id as value for fields required for mongodb & arangodb
|
|
|
|
verification.Key = verification.ID
|
|
|
|
result := mgr.sqlDB.Clauses(clause.OnConflict{
|
2021-12-17 16:20:57 +00:00
|
|
|
Columns: []clause.Column{{Name: "email"}, {Name: "identifier"}},
|
2021-12-17 15:55:07 +00:00
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"token", "expires_at"}),
|
|
|
|
}).Create(&verification)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error saving verification record`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
2021-12-20 17:51:27 +00:00
|
|
|
verification.CreatedAt = time.Now().Unix()
|
|
|
|
verification.UpdatedAt = time.Now().Unix()
|
2021-12-17 15:55:07 +00:00
|
|
|
verificationRequestCollection, _ := mgr.arangodb.Collection(nil, Collections.VerificationRequest)
|
|
|
|
meta, err := verificationRequestCollection.CreateDocument(nil, verification)
|
|
|
|
if err != nil {
|
2021-12-20 17:51:27 +00:00
|
|
|
log.Println("error saving verification record:", err)
|
2021-12-17 15:55:07 +00:00
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
verification.Key = meta.Key
|
2021-12-22 10:01:45 +00:00
|
|
|
verification.ID = meta.ID.String()
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
if IsMongoDB {
|
|
|
|
verification.CreatedAt = time.Now().Unix()
|
|
|
|
verification.UpdatedAt = time.Now().Unix()
|
|
|
|
verification.Key = verification.ID
|
|
|
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
|
|
|
_, err := verificationRequestCollection.InsertOne(nil, verification)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error saving verification record:", err)
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:16 +00:00
|
|
|
return verification, nil
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
// GetVerificationRequests function to get all verification requests
|
|
|
|
func (mgr *manager) GetVerificationRequests() ([]VerificationRequest, error) {
|
|
|
|
var verificationRequests []VerificationRequest
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Find(&verificationRequests)
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println("error getting verification requests:", result.Error)
|
|
|
|
return verificationRequests, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s RETURN d", Collections.VerificationRequest)
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, nil)
|
|
|
|
if err != nil {
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var verificationRequest VerificationRequest
|
|
|
|
meta, err := cursor.ReadDocument(nil, &verificationRequest)
|
|
|
|
|
|
|
|
if driver.IsNoMoreDocuments(err) {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if meta.Key != "" {
|
|
|
|
verificationRequests = append(verificationRequests, verificationRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
if IsMongoDB {
|
|
|
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
|
|
|
cursor, err := verificationRequestCollection.Find(nil, bson.M{}, options.Find())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error getting verification requests:", err)
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
defer cursor.Close(nil)
|
|
|
|
|
|
|
|
for cursor.Next(nil) {
|
|
|
|
var verificationRequest VerificationRequest
|
|
|
|
err := cursor.Decode(&verificationRequest)
|
|
|
|
if err != nil {
|
|
|
|
return verificationRequests, err
|
|
|
|
}
|
|
|
|
verificationRequests = append(verificationRequests, verificationRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return verificationRequests, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 04:22:54 +00:00
|
|
|
func (mgr *manager) GetVerificationByToken(token string) (VerificationRequest, error) {
|
|
|
|
var verification VerificationRequest
|
2021-07-13 20:06:11 +00:00
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Where("token = ?", token).First(&verification)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification request:`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.token == @token LIMIT 1 RETURN d", Collections.VerificationRequest)
|
|
|
|
bindVars := map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
2021-12-20 12:03:11 +00:00
|
|
|
if !cursor.HasMore() {
|
|
|
|
if verification.Key == "" {
|
|
|
|
return verification, fmt.Errorf("verification request not found")
|
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
break
|
2021-12-20 12:03:11 +00:00
|
|
|
}
|
|
|
|
_, err := cursor.ReadDocument(nil, &verification)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
|
|
|
err := verificationRequestCollection.FindOne(nil, bson.M{"token": token}).Decode(&verification)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 20:06:11 +00:00
|
|
|
return verification, nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:01:52 +00:00
|
|
|
func (mgr *manager) GetVerificationByEmail(email string, identifier string) (VerificationRequest, error) {
|
2021-07-18 07:26:17 +00:00
|
|
|
var verification VerificationRequest
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-23 05:01:52 +00:00
|
|
|
result := mgr.sqlDB.Where("email = ? AND identifier = ?", email, identifier).First(&verification)
|
2021-07-18 07:26:17 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error getting verification token:`, result.Error)
|
|
|
|
return verification, result.Error
|
|
|
|
}
|
2021-07-18 07:26:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
if IsArangoDB {
|
2021-12-23 05:01:52 +00:00
|
|
|
query := fmt.Sprintf("FOR d in %s FILTER d.email == @email FILTER d.identifier == @identifier LIMIT 1 RETURN d", Collections.VerificationRequest)
|
2021-12-17 15:55:07 +00:00
|
|
|
bindVars := map[string]interface{}{
|
2021-12-23 05:01:52 +00:00
|
|
|
"email": email,
|
|
|
|
"identifier": identifier,
|
2021-12-17 15:55:07 +00:00
|
|
|
}
|
2021-07-18 07:26:17 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
cursor, err := mgr.arangodb.Query(nil, query, bindVars)
|
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
defer cursor.Close()
|
|
|
|
|
|
|
|
for {
|
2021-12-20 12:03:11 +00:00
|
|
|
if !cursor.HasMore() {
|
|
|
|
if verification.Key == "" {
|
|
|
|
return verification, fmt.Errorf("verification request not found")
|
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
break
|
2021-12-20 12:03:11 +00:00
|
|
|
}
|
|
|
|
_, err := cursor.ReadDocument(nil, &verification)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
2021-12-23 05:01:52 +00:00
|
|
|
err := verificationRequestCollection.FindOne(nil, bson.M{"email": email, "identifier": identifier}).Decode(&verification)
|
2021-12-20 17:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return verification, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return verification, nil
|
2021-07-13 20:06:11 +00:00
|
|
|
}
|
2021-07-18 04:22:54 +00:00
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
func (mgr *manager) DeleteVerificationRequest(verificationRequest VerificationRequest) error {
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsORMSupported {
|
2021-12-17 15:55:07 +00:00
|
|
|
result := mgr.sqlDB.Delete(&verificationRequest)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Println(`error deleting verification request:`, result.Error)
|
|
|
|
return result.Error
|
|
|
|
}
|
2021-07-18 04:22:54 +00:00
|
|
|
}
|
2021-12-17 15:55:07 +00:00
|
|
|
|
|
|
|
if IsArangoDB {
|
|
|
|
collection, _ := mgr.arangodb.Collection(nil, Collections.VerificationRequest)
|
|
|
|
_, err := collection.RemoveDocument(nil, verificationRequest.Key)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`error deleting verification request:`, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
if IsMongoDB {
|
|
|
|
verificationRequestCollection := mgr.mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
2021-12-23 05:01:52 +00:00
|
|
|
_, err := verificationRequestCollection.DeleteOne(nil, bson.M{"_id": verificationRequest.ID}, options.Delete())
|
2021-12-20 17:51:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error deleting verification request::", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
return nil
|
2021-07-18 04:22:54 +00:00
|
|
|
}
|