first commit

This commit is contained in:
2023-08-08 12:28:43 +02:00
commit da86ee374a
29 changed files with 4883 additions and 0 deletions

54
handler/authentication.go Normal file
View File

@@ -0,0 +1,54 @@
package handler
import (
"context"
"time"
)
func (h *SilvermobiHandler) Login(ctx context.Context, username string, password string) (jwt string, err error) {
account, err := h.Services.MobilityAccounts.Login(ctx, username, password, "silvermobi")
if err != nil {
return
}
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
parsedttl, err := time.ParseDuration(ttl)
if err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
}
func (h *SilvermobiHandler) Register(ctx context.Context, username string, password string, email string, phone_number string, first_name string, last_name string) (jwt string, err error) {
account, err := h.Services.MobilityAccounts.Register(
ctx,
username,
password,
email,
phone_number,
map[string]any{
"first_name": first_name,
"last_name": last_name,
"email": email,
"phone_number": phone_number,
},
"silvermobi",
)
if err != nil {
return "", err
}
key := h.Config.GetString("identification.local.jwt_secret")
ttl := h.Config.GetString("identification.local.ttl")
parsedttl, err := time.ParseDuration(ttl)
if err != nil {
return "", err
}
return account.CreateToken(parsedttl, key)
}

View File

@@ -0,0 +1,46 @@
package handler
import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/base64"
gomail "gopkg.in/mail.v2"
)
func generateRandomPassword(length int) (string, error) {
buffer := make([]byte, length)
_, err := rand.Read(buffer)
if err != nil {
return "", err
}
password := base64.StdEncoding.EncodeToString(buffer)[:length]
return password, nil
}
func (h *SilvermobiHandler) ForgetAccount(ctx context.Context, username string, namespace string) (response bool, access_Code string) {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, username, namespace)
if err == nil {
message := []byte("Hello dear,\nYour new Silvermobi password is: ")
password, _ := generateRandomPassword(10)
update := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password)
if update == false {
return false, ""
}
m := gomail.NewMessage()
m.SetHeader("From", h.Config.GetString("emailing.smtp.username"))
m.SetHeader("To", username)
m.SetHeader("Subject", "Silvermobi Password Recovery")
message = append(message, []byte(password)...)
m.SetBody("text/plain", string(message))
d := gomail.NewDialer(h.Config.GetString("emailing.smtp.host"), h.Config.GetInt("emailing.smtp.port"), h.Config.GetString("emailing.smtp.username"),
h.Config.GetString("emailing.smtp.password"))
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
if err := d.DialAndSend(m); err != nil {
return false, ""
}
return true, password
} else {
return false, ""
}
}

21
handler/handler.go Normal file
View File

@@ -0,0 +1,21 @@
package handler
import (
"git.coopgo.io/coopgo-apps/silvermobi/services"
"git.coopgo.io/coopgo-apps/silvermobi/storage"
"github.com/spf13/viper"
)
type SilvermobiHandler struct {
Config *viper.Viper
Services *services.ServicesHandler
KVHandler storage.KVHandler
}
func NewSilvermobiHandler(cfg *viper.Viper, services *services.ServicesHandler, KVHandler storage.KVHandler) (*SilvermobiHandler, error) {
return &SilvermobiHandler{
Config: cfg,
Services: services,
KVHandler: KVHandler,
}, nil
}

View File

@@ -0,0 +1,109 @@
package handler
import (
"context"
"fmt"
"git.coopgo.io/coopgo-apps/silvermobi/services"
"git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"math/rand"
)
func (h *SilvermobiHandler) UpdatePhoneNumber(ctx context.Context, email string, phone_number string) error {
code := rand.Intn(9999)
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
if err != nil {
return err
}
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
account.ID,
phone_number,
false,
fmt.Sprintf("%04d", code),
)
if err != nil {
log.Error().Err(err).Msg("updating phone number failed")
return err
}
err = h.Services.Push.Send(
services.Notification{
Platform: services.PushToSMSFactor,
Recipients: []string{phone_number[1:]},
Title: "SILVERMOBI",
Message: fmt.Sprintf("SILVERMOBI - Votre code de validation : %04d", code),
},
)
if err != nil {
log.Error().Err(err).Msg("issue sending verification code by sms")
}
return nil
}
func (h *SilvermobiHandler) VerifyPhoneNumber(ctx context.Context, email string, phone_number string, verification_code string) error {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
request := &grpcapi.GetAccountRequest{
Id: account.ID,
}
resp, err := h.Services.MobilityAccounts.Client.GetAccount(ctx, request)
if err != nil {
return err
}
account_ := resp.Account
log.Debug().
Str("phone_number", phone_number).
Str("account.phone_number", account_.Authentication.Local.PhoneNumber).
Str("verification_code", verification_code).
Str("account.verification_code", account_.Authentication.Local.PhoneNumberValidation.ValidationCode).
Msg("Verify phone number")
if account_.Authentication.Local.PhoneNumber != phone_number || account_.Authentication.Local.PhoneNumberValidation.ValidationCode != verification_code {
return errors.New("cound not validate phone number : verification code mismatch")
}
err = h.Services.MobilityAccounts.UpdatePhoneNumber(
ctx,
account.ID,
phone_number,
true,
"",
)
if err != nil {
return err
}
return nil
}
func (h *SilvermobiHandler) UpdateBirthDate(ctx context.Context, username string, birthdate string) error {
err := h.Services.MobilityAccounts.UpdateAccountBirthDate(ctx, username, "silvermobi", birthdate)
if err != nil {
return err
}
return nil
}
func (h *SilvermobiHandler) CheckValidation(ctx context.Context, email string) (phone_validation, birth_validation bool, err error) {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, email, "silvermobi")
if err != nil {
return false, false, err
log.Error().Err(err).Msg("Failed get validation response")
}
phone_validation = false
birth_validation = false
if account.LocalCredentials.PhoneNumberVerified {
phone_validation = true
}
if account.BirthDate != "" {
birth_validation = true
}
log.Info().Msg("Getting phone and birth validation for " + email)
return phone_validation, birth_validation, nil
}

View File

@@ -0,0 +1,16 @@
package handler
import "context"
func (h *SilvermobiHandler) UpdatePassword(ctx context.Context, username string, password string) (response bool) {
account, err := h.Services.MobilityAccounts.GetAccountUsername(ctx, username, "silvermobi")
if err != nil {
return false
}
result := h.Services.MobilityAccounts.UpdatePassword(ctx, account.ID, password)
if result == true {
return true
} else {
return false
}
}