-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from MussieT/feat/sms_confirmation
Feat/sms confirmation
- Loading branch information
Showing
26 changed files
with
1,134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
// SMS verification requests model for database | ||
type SMSVerificationRequest struct { | ||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"` | ||
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number" cql:"phone_number" dynamo:"phone_number" index:"phone_number,hash"` | ||
Code string `json:"code" bson:"code" cql:"code" dynamo:"code"` | ||
CodeExpiresAt int64 `json:"code_expires_at" bson:"code_expires_at" cql:"code_expires_at" dynamo:"code_expires_at"` | ||
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"` | ||
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package arangodb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
|
||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
return sms_code, nil | ||
} | ||
|
||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var sms_verification_request models.SMSVerificationRequest | ||
|
||
return &sms_verification_request, nil | ||
} | ||
|
||
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
return nil | ||
} |
23 changes: 23 additions & 0 deletions
23
server/db/providers/cassandradb/sms_verification_requests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cassandradb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
|
||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
return sms_code, nil | ||
} | ||
|
||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var sms_verification_request models.SMSVerificationRequest | ||
|
||
return &sms_verification_request, nil | ||
} | ||
|
||
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
return nil | ||
} |
23 changes: 23 additions & 0 deletions
23
server/db/providers/couchbase/sms_verification_requests.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package couchbase | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
|
||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
return sms_code, nil | ||
} | ||
|
||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var sms_verification_request models.SMSVerificationRequest | ||
|
||
return &sms_verification_request, nil | ||
} | ||
|
||
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dynamodb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
|
||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
return sms_code, nil | ||
} | ||
|
||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var sms_verification_request models.SMSVerificationRequest | ||
|
||
return &sms_verification_request, nil | ||
} | ||
|
||
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package mongodb | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
"github.com/google/uuid" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
smsVerificationRequest, _ := p.GetCodeByPhone(ctx, smsRequest.PhoneNumber) | ||
shouldCreate := false | ||
|
||
if smsVerificationRequest == nil { | ||
id := uuid.NewString() | ||
|
||
smsVerificationRequest = &models.SMSVerificationRequest{ | ||
ID: id, | ||
CreatedAt: time.Now().Unix(), | ||
Code: smsRequest.Code, | ||
PhoneNumber: smsRequest.PhoneNumber, | ||
CodeExpiresAt: smsRequest.CodeExpiresAt, | ||
} | ||
shouldCreate = true | ||
} | ||
|
||
smsVerificationRequest.UpdatedAt = time.Now().Unix() | ||
smsRequestCollection := p.db.Collection(models.Collections.SMSVerificationRequest, options.Collection()) | ||
|
||
var err error | ||
if shouldCreate { | ||
_, err = smsRequestCollection.InsertOne(ctx, smsVerificationRequest) | ||
} else { | ||
_, err = smsRequestCollection.UpdateOne(ctx, bson.M{"phone_number": bson.M{"$eq": smsRequest.PhoneNumber}}, bson.M{"$set": smsVerificationRequest}, options.MergeUpdateOptions()) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return smsVerificationRequest, nil | ||
} | ||
|
||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var smsVerificationRequest models.SMSVerificationRequest | ||
|
||
smsRequestCollection := p.db.Collection(models.Collections.SMSVerificationRequest, options.Collection()) | ||
err := smsRequestCollection.FindOne(ctx, bson.M{"phone_number": phoneNumber}).Decode(&smsVerificationRequest) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &smsVerificationRequest, nil | ||
} | ||
|
||
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
smsVerificationRequests := p.db.Collection(models.Collections.SMSVerificationRequest, options.Collection()) | ||
_, err := smsVerificationRequests.DeleteOne(nil, bson.M{"_id": smsRequest.ID}, options.Delete()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package sql | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/authorizerdev/authorizer/server/db/models" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
// SMS verification Request | ||
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) { | ||
if smsRequest.ID == "" { | ||
smsRequest.ID = uuid.New().String() | ||
} | ||
|
||
smsRequest.CreatedAt = time.Now().Unix() | ||
smsRequest.UpdatedAt = time.Now().Unix() | ||
|
||
res := p.db.Clauses(clause.OnConflict{ | ||
Columns: []clause.Column{{Name: "phone_number"}}, | ||
DoUpdates: clause.AssignmentColumns([]string{"code", "code_expires_at"}), | ||
}).Create(smsRequest) | ||
if res.Error != nil { | ||
return nil, res.Error | ||
} | ||
|
||
return smsRequest, nil | ||
} | ||
|
||
// GetOTPByEmail to get otp for a given email address | ||
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) { | ||
var sms_verification_request models.SMSVerificationRequest | ||
|
||
result := p.db.Where("phone_number = ?", phoneNumber).First(&sms_verification_request) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return &sms_verification_request, nil | ||
} | ||
|
||
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error { | ||
result := p.db.Delete(&models.SMSVerificationRequest{ | ||
ID: smsRequest.ID, | ||
}) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.