Skip to content

Commit 6cef906

Browse files
committed
Update provider template for sms verification
1 parent 9ae616b commit 6cef906

File tree

10 files changed

+94
-78
lines changed

10 files changed

+94
-78
lines changed

server/db/models/model.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package models
22

33
// Collections / Tables available for authorizer in the database
44
type CollectionList struct {
5-
User string
6-
VerificationRequest string
7-
Session string
8-
Env string
9-
Webhook string
10-
WebhookLog string
11-
EmailTemplate string
12-
OTP string
5+
User string
6+
VerificationRequest string
7+
Session string
8+
Env string
9+
Webhook string
10+
WebhookLog string
11+
EmailTemplate string
12+
OTP string
1313
SMSVerificationRequest string
1414
}
1515

@@ -18,14 +18,14 @@ var (
1818
Prefix = "authorizer_"
1919
// Collections / Tables available for authorizer in the database (used for dbs other than gorm)
2020
Collections = CollectionList{
21-
User: Prefix + "users",
22-
VerificationRequest: Prefix + "verification_requests",
23-
Session: Prefix + "sessions",
24-
Env: Prefix + "env",
25-
Webhook: Prefix + "webhooks",
26-
WebhookLog: Prefix + "webhook_logs",
27-
EmailTemplate: Prefix + "email_templates",
28-
OTP: Prefix + "otps",
29-
SMSVerificationRequest: Prefix + "sms_verification_requests",
21+
User: Prefix + "users",
22+
VerificationRequest: Prefix + "verification_requests",
23+
Session: Prefix + "sessions",
24+
Env: Prefix + "env",
25+
Webhook: Prefix + "webhooks",
26+
WebhookLog: Prefix + "webhook_logs",
27+
EmailTemplate: Prefix + "email_templates",
28+
OTP: Prefix + "otps",
29+
SMSVerificationRequest: Prefix + "sms_verification_requests",
3030
}
3131
)

server/db/models/sms_verification_requests.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package models
22

33
// SMS verification requests model for database
44
type SMSVerificationRequest struct {
5-
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
6-
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number" cql:"phone_number" dynamo:"phone_number" index:"phone_number,hash"`
7-
Code string `json:"code" bson:"code" cql:"code" dynamo:"code"`
8-
CodeExpiresAt int64 `json:"code_expires_at" bson:"code_expires_at" cql:"code_expires_at" dynamo:"code_expires_at"`
9-
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
10-
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
5+
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
6+
PhoneNumber string `gorm:"unique" json:"phone_number" bson:"phone_number" cql:"phone_number" dynamo:"phone_number" index:"phone_number,hash"`
7+
Code string `json:"code" bson:"code" cql:"code" dynamo:"code"`
8+
CodeExpiresAt int64 `json:"code_expires_at" bson:"code_expires_at" cql:"code_expires_at" dynamo:"code_expires_at"`
9+
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
10+
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
1111
}

server/db/providers/arangodb/sms_verification_requests.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import (
44
"context"
55

66
"github.com/authorizerdev/authorizer/server/db/models"
7-
87
)
98

10-
// SMS verification Request
11-
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
12-
return sms_code, nil
9+
// UpsertSMSRequest adds/updates SMS verification request
10+
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
11+
return nil, nil
1312
}
1413

14+
// GetCodeByPhone to get code for a given phone number
1515
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
16-
var sms_verification_request models.SMSVerificationRequest
17-
18-
return &sms_verification_request, nil
16+
return nil, nil
1917
}
2018

21-
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
19+
// DeleteSMSRequest to delete SMS verification request
20+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
2221
return nil
2322
}

server/db/providers/cassandradb/sms_verification_requests.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import (
44
"context"
55

66
"github.com/authorizerdev/authorizer/server/db/models"
7-
87
)
98

10-
// SMS verification Request
11-
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
12-
return sms_code, nil
9+
// UpsertSMSRequest adds/updates SMS verification request
10+
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
11+
return nil, nil
1312
}
1413

14+
// GetCodeByPhone to get code for a given phone number
1515
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
16-
var sms_verification_request models.SMSVerificationRequest
17-
18-
return &sms_verification_request, nil
16+
return nil, nil
1917
}
2018

21-
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
19+
// DeleteSMSRequest to delete SMS verification request
20+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
2221
return nil
2322
}

server/db/providers/couchbase/sms_verification_requests.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import (
44
"context"
55

66
"github.com/authorizerdev/authorizer/server/db/models"
7-
87
)
98

10-
// SMS verification Request
11-
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
12-
return sms_code, nil
9+
// UpsertSMSRequest adds/updates SMS verification request
10+
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
11+
return nil, nil
1312
}
1413

14+
// GetCodeByPhone to get code for a given phone number
1515
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
16-
var sms_verification_request models.SMSVerificationRequest
17-
18-
return &sms_verification_request, nil
16+
return nil, nil
1917
}
2018

21-
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
19+
// DeleteSMSRequest to delete SMS verification request
20+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
2221
return nil
2322
}

server/db/providers/dynamodb/sms_verification_requests.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import (
44
"context"
55

66
"github.com/authorizerdev/authorizer/server/db/models"
7-
87
)
98

10-
// SMS verification Request
11-
func (p *provider) UpsertSMSRequest(ctx context.Context, sms_code *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
12-
return sms_code, nil
9+
// UpsertSMSRequest adds/updates SMS verification request
10+
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
11+
return nil, nil
1312
}
1413

14+
// GetCodeByPhone to get code for a given phone number
1515
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
16-
var sms_verification_request models.SMSVerificationRequest
17-
18-
return &sms_verification_request, nil
16+
return nil, nil
1917
}
2018

21-
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
19+
// DeleteSMSRequest to delete SMS verification request
20+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
2221
return nil
2322
}

server/db/providers/mongodb/sms_verification_requests.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,40 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo/options"
1111
)
1212

13-
// SMS verification Request
13+
// UpsertSMSRequest adds/updates SMS verification request
1414
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
15-
smsVerificationRequest, _ := p.GetCodeByPhone(ctx, smsRequest.PhoneNumber)
15+
smsVerificationRequest, err := p.GetCodeByPhone(ctx, smsRequest.PhoneNumber)
16+
if err != nil {
17+
return nil, err
18+
}
19+
// Boolean to check if we should create a new record or update the existing one
1620
shouldCreate := false
17-
1821
if smsVerificationRequest == nil {
1922
id := uuid.NewString()
20-
2123
smsVerificationRequest = &models.SMSVerificationRequest{
22-
ID: id,
23-
CreatedAt: time.Now().Unix(),
24-
Code: smsRequest.Code,
25-
PhoneNumber: smsRequest.PhoneNumber,
24+
ID: id,
25+
CreatedAt: time.Now().Unix(),
26+
Code: smsRequest.Code,
27+
PhoneNumber: smsRequest.PhoneNumber,
2628
CodeExpiresAt: smsRequest.CodeExpiresAt,
2729
}
2830
shouldCreate = true
2931
}
30-
32+
3133
smsVerificationRequest.UpdatedAt = time.Now().Unix()
3234
smsRequestCollection := p.db.Collection(models.Collections.SMSVerificationRequest, options.Collection())
33-
34-
var err error
3535
if shouldCreate {
3636
_, err = smsRequestCollection.InsertOne(ctx, smsVerificationRequest)
3737
} else {
3838
_, err = smsRequestCollection.UpdateOne(ctx, bson.M{"phone_number": bson.M{"$eq": smsRequest.PhoneNumber}}, bson.M{"$set": smsVerificationRequest}, options.MergeUpdateOptions())
3939
}
40-
4140
if err != nil {
4241
return nil, err
4342
}
44-
4543
return smsVerificationRequest, nil
4644
}
4745

46+
// GetCodeByPhone to get code for a given phone number
4847
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
4948
var smsVerificationRequest models.SMSVerificationRequest
5049

@@ -58,6 +57,7 @@ func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*mod
5857
return &smsVerificationRequest, nil
5958
}
6059

60+
// DeleteSMSRequest to delete SMS verification request
6161
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
6262
smsVerificationRequests := p.db.Collection(models.Collections.SMSVerificationRequest, options.Collection())
6363
_, err := smsVerificationRequests.DeleteOne(nil, bson.M{"_id": smsRequest.ID}, options.Delete())
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package provider_template
2+
3+
import (
4+
"context"
5+
6+
"github.com/authorizerdev/authorizer/server/db/models"
7+
)
8+
9+
// UpsertSMSRequest adds/updates SMS verification request
10+
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
11+
return nil, nil
12+
}
13+
14+
// GetCodeByPhone to get code for a given phone number
15+
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
16+
return nil, nil
17+
}
18+
19+
// DeleteSMSRequest to delete SMS verification request
20+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
21+
return nil
22+
}

server/db/providers/providers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ type Provider interface {
8585
// DeleteOTP to delete otp
8686
DeleteOTP(ctx context.Context, otp *models.OTP) error
8787

88-
// Upsert SMS code request
88+
// UpsertSMSRequest adds/updates SMS verification request
8989
UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error)
90-
// Get sms code by phone number
90+
// GetCodeByPhone to get code for a given phone number
9191
GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error)
92-
// Delete sms
92+
// DeleteSMSRequest to delete SMS verification request
9393
DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error
9494
}

server/db/providers/sql/sms_verification_requests.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,24 @@ import (
99
"gorm.io/gorm/clause"
1010
)
1111

12-
// SMS verification Request
12+
// UpsertSMSRequest adds/updates SMS verification request
1313
func (p *provider) UpsertSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) (*models.SMSVerificationRequest, error) {
1414
if smsRequest.ID == "" {
1515
smsRequest.ID = uuid.New().String()
1616
}
17-
1817
smsRequest.CreatedAt = time.Now().Unix()
1918
smsRequest.UpdatedAt = time.Now().Unix()
20-
2119
res := p.db.Clauses(clause.OnConflict{
2220
Columns: []clause.Column{{Name: "phone_number"}},
23-
DoUpdates: clause.AssignmentColumns([]string{"code", "code_expires_at"}),
21+
DoUpdates: clause.AssignmentColumns([]string{"code", "code_expires_at", "updated_at"}),
2422
}).Create(smsRequest)
2523
if res.Error != nil {
2624
return nil, res.Error
2725
}
28-
2926
return smsRequest, nil
3027
}
3128

32-
// GetOTPByEmail to get otp for a given email address
29+
// GetCodeByPhone to get code for a given phone number
3330
func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*models.SMSVerificationRequest, error) {
3431
var sms_verification_request models.SMSVerificationRequest
3532

@@ -40,7 +37,8 @@ func (p *provider) GetCodeByPhone(ctx context.Context, phoneNumber string) (*mod
4037
return &sms_verification_request, nil
4138
}
4239

43-
func(p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
40+
// DeleteSMSRequest to delete SMS verification request
41+
func (p *provider) DeleteSMSRequest(ctx context.Context, smsRequest *models.SMSVerificationRequest) error {
4442
result := p.db.Delete(&models.SMSVerificationRequest{
4543
ID: smsRequest.ID,
4644
})

0 commit comments

Comments
 (0)