Correction fake-sms iso powow

This commit is contained in:
2021-03-17 09:11:02 +01:00
parent c419d6b79f
commit 91d736cc82
5 changed files with 195 additions and 84 deletions

View File

@ -4,14 +4,16 @@ import (
"context"
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"forge.cadoles.com/Cadoles/fake-sms/internal/command"
"forge.cadoles.com/Cadoles/fake-sms/internal/config"
"forge.cadoles.com/Cadoles/fake-sms/internal/model/powow"
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
"github.com/davecgh/go-spew/spew"
php "github.com/kovetskiy/go-php-serialize"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/logger"
@ -58,6 +60,11 @@ type PowowRequest struct {
}
type PowowResponse struct {
Success bool
ErrorCode map[int]ErrorCode
}
type PowowResponseSuccess struct {
Success bool
ErrorCode ErrorCode
}
@ -68,38 +75,87 @@ func handlePowowEntrypoint(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(ctx)
conf := config.Must(ctn)
data, err := ioutil.ReadAll(r.Body)
err := r.ParseForm()
if err != nil {
logger.Error(ctx, "could not read body", logger.E(errors.WithStack(err)))
logger.Error(ctx, "could not parse form", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
defer r.Body.Close()
//data, err := ioutil.ReadAll(r.Body)
//if err != nil {
// logger.Error(ctx, "could not read body", logger.E(errors.WithStack(err)))
// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
// return
//}
//defer r.Body.Close()
pr := &PowowRequest{
Payload: make(map[string]interface{}),
}
if err := json.Unmarshal(data, pr); err != nil {
logger.Error(ctx, "could not parse request", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
pr.APIKey = r.FormValue("APIKey")
pr.Command = Command(r.FormValue("Command"))
pr.ResponseFormat = r.FormValue("ResponseFormat")
return
if pr.Command == CommandTransactionalSMSUpdate {
smsid, err := strconv.ParseFloat(r.FormValue("SmsID"), 64)
if err != nil {
logger.Error(ctx, "could not convert SmsID to float", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
pr.Payload = map[string]interface{}{
"SmsID": smsid,
"Content": r.FormValue("Content"),
"ShortLink": r.FormValue("ShortLink"),
"Language": r.FormValue("Language"),
"FromName": r.FormValue("FromName"),
"SmsName": r.FormValue("SmsName"),
}
}
if err := json.Unmarshal(data, &pr.Payload); err != nil {
logger.Error(ctx, "could not parse request payload", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
if pr.Command == CommandTransactionalSMSSend {
customData, err := php.Decode(r.FormValue("CustomData"))
if err != nil {
logger.Error(ctx, "could not cunserialized custom data", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
var smsid float64
if r.FormValue("SmsID") != "" {
smsid, err = strconv.ParseFloat(r.FormValue("SmsID"), 64)
if err != nil {
logger.Error(ctx, "could not convert SmsID to float", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
pr.Payload = map[string]interface{}{
"SmsID": smsid,
"MobilePhoneNumber": r.FormValue("MobilePhoneNumber"),
"TimeToSend": r.FormValue("TimeToSend"),
"CustomData": customData,
}
}
//if err := json.Unmarshal(data, pr); err != nil {
// logger.Error(ctx, "could not parse request", logger.E(errors.WithStack(err)))
// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
// return
//}
//if err := json.Unmarshal(data, &pr.Payload); err != nil {
// logger.Error(ctx, "could not parse request payload", logger.E(errors.WithStack(err)))
// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
// return
//}
// Authenticate user
if conf.Powow.APIKey != pr.APIKey {
res := &PowowResponse{
Success: false,
ErrorCode: ErrorCodeAuthenticationFailure,
ErrorCode: map[int]ErrorCode{0: ErrorCodeAuthenticationFailure},
}
sendPowowResponse(w, res)
@ -124,7 +180,7 @@ func handlePowowEntrypoint(w http.ResponseWriter, r *http.Request) {
default:
res := &PowowResponse{
Success: false,
ErrorCode: ErrorCodeInvalidCommand,
ErrorCode: map[int]ErrorCode{0: ErrorCodeInvalidCommand},
}
sendPowowResponse(w, res)
@ -141,7 +197,7 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
smsID, exists, valid := getPowowSMSID(req)
if !exists {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSSendMissingSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSSendMissingSMSID},
Success: false,
})
@ -150,7 +206,7 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
if !valid {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSSendInvalidSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSSendInvalidSMSID},
Success: false,
})
@ -162,7 +218,7 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
if err := db.One("ID", smsID, smsTmpl); err != nil {
if errors.Is(err, storm.ErrNotFound) {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSSendInvalidSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSSendInvalidSMSID},
Success: false,
})
@ -174,19 +230,21 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
customData := make(map[string]interface{})
var ok bool
rawCustomData, exists := req.Payload["CustomData"]
if exists {
customData, ok = rawCustomData.(map[string]interface{})
cData, ok := rawCustomData.(map[interface{}]interface{})
if !ok {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSSendInvalidCustomData,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSSendInvalidCustomData},
Success: false,
})
return
}
for k, v := range cData {
customData[k.(string)] = v
}
req.Payload["CustomData"] = customData
}
body, err := createSMSBody(smsTmpl.Content, customData)
@ -209,10 +267,10 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
}
res := &struct {
PowowResponse
PowowResponseSuccess
TransactionalID int
}{
PowowResponse: PowowResponse{
PowowResponseSuccess: PowowResponseSuccess{
Success: true,
ErrorCode: 0,
},
@ -224,7 +282,7 @@ func handleTransactionalSMSSend(ctx context.Context, ctn *service.Container, w h
func createSMSBody(template string, customData map[string]interface{}) (string, error) {
content := template
spew.Dump(customData)
for k, v := range customData {
decoded, err := base64.StdEncoding.DecodeString(v.(string))
if err != nil {
@ -250,10 +308,10 @@ func handleTransactionalSMSCreate(ctx context.Context, ctn *service.Container, w
}
res := &struct {
PowowResponse
PowowResponseSuccess
SmsID int
}{
PowowResponse: PowowResponse{
PowowResponseSuccess: PowowResponseSuccess{
Success: true,
ErrorCode: 0,
},
@ -268,7 +326,7 @@ func handleTransactionalSMSUpdate(ctx context.Context, ctn *service.Container, w
smsID, exists, valid := getPowowSMSID(req)
if !exists {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSUpdateMissingSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSUpdateMissingSMSID},
Success: false,
})
@ -277,7 +335,7 @@ func handleTransactionalSMSUpdate(ctx context.Context, ctn *service.Container, w
if !valid {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSUpdateInvalidSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSUpdateInvalidSMSID},
Success: false,
})
@ -291,7 +349,7 @@ func handleTransactionalSMSUpdate(ctx context.Context, ctn *service.Container, w
if err := db.One("ID", smsID, smsTmpl); err != nil {
if errors.Is(err, storm.ErrNotFound) {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSUpdateInvalidSMSID,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSUpdateInvalidSMSID},
Success: false,
})
@ -315,7 +373,7 @@ func handleTransactionalSMSUpdate(ctx context.Context, ctn *service.Container, w
if ok {
if !contains(language, "en", "fr", "it", "es", "de", "pt", "pl", "zh") {
sendPowowResponse(w, &PowowResponse{
ErrorCode: ErrorCodeTransactionSMSUpdateInvalidLanguage,
ErrorCode: map[int]ErrorCode{0: ErrorCodeTransactionSMSUpdateInvalidLanguage},
Success: false,
})
@ -370,7 +428,7 @@ func handleTransactionalSMSUpdate(ctx context.Context, ctn *service.Container, w
panic(errors.Wrap(err, "could not save sms template"))
}
sendPowowResponse(w, &PowowResponse{
sendPowowResponse(w, &PowowResponseSuccess{
ErrorCode: 0,
Success: true,
})