Skip to content

Commit

Permalink
Add 2FA TOTP generation for delivery persons
Browse files Browse the repository at this point in the history
Implemented TOTP generation for delivery persons and updated related models and functions to store authentication keys and URLs. Adjusted handler and service logic to accommodate these changes while ensuring data consistency and error handling.
  • Loading branch information
mukulmantosh committed Sep 12, 2024
1 parent 1792b5c commit ffe7840
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Microsoft/hcsshim v0.11.5 // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/bytedance/sonic v1.12.0 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
Expand Down Expand Up @@ -78,6 +79,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/pquerna/otp v1.4.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Microsoft/hcsshim v0.11.5 h1:haEcLNpj9Ka1gd3B3tAEs9CpE0c+1IhoL59w/exYU38=
github.com/Microsoft/hcsshim v0.11.5/go.mod h1:MV8xMfmECjl5HdO7U/3/hFVnkmSBjAjmA09d4bExKcU=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls=
github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
Expand Down Expand Up @@ -159,6 +161,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/pquerna/otp v1.4.0 h1:wZvl1TIVxKRThZIBiwOOHOGP/1+nZyWBil9Y2XNEDzg=
github.com/pquerna/otp v1.4.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
Expand Down
3 changes: 2 additions & 1 deletion pkg/abstract/delivery/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import (
)

type DeliveryPerson interface {
AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error)
AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (bool, error)
GenerateTOTP(_ context.Context, phone string) (string, string, error)
}
11 changes: 10 additions & 1 deletion pkg/database/models/delivery/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ type DeliveryPerson struct {
bun.BaseModel `bun:"table:delivery_person"`
DeliveryPersonID int64 `bun:",pk,autoincrement" json:"delivery_person_id"`
Name string `bun:"name,notnull" json:"name"`
Phone string `bun:"phone,notnull" json:"phone"`
Phone string `bun:"phone,unique,notnull" json:"phone"`
VehicleDetails string `bun:"vehicle_details,notnull" json:"vehicle_details"`
Status string `bun:"status,notnull" json:"status"`
AuthKey string `bun:"auth_key,notnull" json:"auth_key"`
AuthKeyURL string `bun:"auth_key_url,notnull" json:"auth_key_url"`
IsAuthSet bool `bun:"is_auth_set,notnull" json:"is_auth_set"`
utils.Timestamp
}

Expand All @@ -27,3 +30,9 @@ type Deliveries struct {
Order *order.Order `bun:"rel:belongs-to,join:order_id=order_id" json:"-"`
utils.Timestamp
}

type DeliveryPersonParams struct {
Name string `json:"name"`
Phone string `json:"phone"`
VehicleDetails string `json:"vehicle_details"`
}
19 changes: 17 additions & 2 deletions pkg/handler/delivery/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@ func (s *DeliveryHandler) addDeliveryPerson(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()

var deliverPerson delivery.DeliveryPerson
var deliverPerson delivery.DeliveryPersonParams
var deliverPersonModel delivery.DeliveryPerson

if err := c.BindJSON(&deliverPerson); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}

_, err := s.service.AddDeliveryPerson(ctx, &deliverPerson)
deliverPersonModel.Name = deliverPerson.Name
deliverPersonModel.Phone = deliverPerson.Phone
deliverPersonModel.VehicleDetails = deliverPerson.VehicleDetails
deliverPersonModel.Status = "AVAILABLE"

authKey, authKeyURL, err := s.service.GenerateTOTP(ctx, deliverPerson.Phone)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
deliverPersonModel.AuthKey = authKey
deliverPersonModel.AuthKeyURL = authKeyURL
deliverPersonModel.IsAuthSet = false

_, err = s.service.AddDeliveryPerson(ctx, &deliverPersonModel)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/delivery/add_delivery_person.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"context"
)

func (deliverSrv *DeliveryService) AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (*delivery.DeliveryPerson, error) {
func (deliverSrv *DeliveryService) AddDeliveryPerson(ctx context.Context, deliveryPerson *delivery.DeliveryPerson) (bool, error) {
_, err := deliverSrv.db.Insert(ctx, deliveryPerson)
if err != nil {
return nil, err
return false, err
}
return deliveryPerson, nil
return true, nil
}
18 changes: 18 additions & 0 deletions pkg/service/delivery/generate_2fa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package delivery

import (
"context"
"errors"
"github.com/pquerna/otp/totp"
)

func (deliverSrv *DeliveryService) GenerateTOTP(_ context.Context, phone string) (string, string, error) {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: "Food Delivery",
AccountName: phone,
})
if err != nil {
return "", "", errors.New("error generating key")
}
return key.Secret(), key.URL(), nil
}

0 comments on commit ffe7840

Please sign in to comment.