Skip to content

Commit

Permalink
Merge pull request 'logs-at-sessionid' (#245) from logs-at-sessionid …
Browse files Browse the repository at this point in the history
  • Loading branch information
lash committed Jan 4, 2025
2 parents d2fce05 + 62f3681 commit 5ee10d8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
52 changes: 48 additions & 4 deletions cmd/africastalking/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"path"
Expand All @@ -29,7 +30,7 @@ import (
)

var (
logg = logging.NewVanilla()
logg = logging.NewVanilla().WithDomain("AfricasTalking").WithContextKey("at-session-id")
scriptDir = path.Join("services", "registration")
build = "dev"
menuSeparator = ": "
Expand All @@ -39,7 +40,43 @@ func init() {
initializers.LoadEnvVariables()
}

type atRequestParser struct{}
type atRequestParser struct {
context context.Context
}

func parseQueryParams(query string) map[string]string {
params := make(map[string]string)

queryParams := strings.Split(query, "&")
for _, param := range queryParams {
// Split each key-value pair by '='
parts := strings.SplitN(param, "=", 2)
if len(parts) == 2 {
params[parts[0]] = parts[1]
}
}
return params
}

func extractATSessionId(decodedStr string) (string, error) {
var data map[string]string
err := json.Unmarshal([]byte(decodedStr), &data)

if err != nil {
logg.Errorf("Error unmarshalling JSON: %v", err)
return "", nil
}
decodedBody, err := url.QueryUnescape(data["body"])
if err != nil {
logg.Errorf("Error URL-decoding body: %v", err)
return "", nil
}
params := parseQueryParams(decodedBody)

sessionId := params["sessionId"]
return sessionId, nil

}

func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
rqv, ok := rq.(*http.Request)
Expand All @@ -63,7 +100,12 @@ func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
if err != nil {
logg.Warnf("failed to marshal request body", "err", err)
} else {
logg.Debugf("received request", "bytes", logBytes)
decodedStr := string(logBytes)
sessionId, err := extractATSessionId(decodedStr)
if err != nil {
context.WithValue(arp.context, "at-session-id", sessionId)
}
logg.Debugf("Received request:", decodedStr)
}

if err := rqv.ParseForm(); err != nil {
Expand Down Expand Up @@ -191,7 +233,9 @@ func main() {
}
defer stateStore.Close()

rp := &atRequestParser{}
rp := &atRequestParser{
context: ctx,
}
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
sh := httpserver.NewATSessionHandler(bsh)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ module git.grassecon.net/urdt/ussd
go 1.23.0

require (
git.defalsify.org/vise.git v0.2.3-0.20241231085136-8582c7e157d9
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
github.com/alecthomas/assert/v2 v2.2.2
github.com/gofrs/uuid v4.4.0+incompatible
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
github.com/joho/godotenv v1.5.1
github.com/peteole/testdata-loader v0.3.0
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.27.0
gopkg.in/leonelquinteros/gotext.v1 v1.3.1
)

Expand All @@ -32,7 +33,6 @@ require (
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
git.defalsify.org/vise.git v0.2.3-0.20241231085136-8582c7e157d9 h1:O3m+NgWDWtJm8OculT99c4bDMAO4xLe2c8hpCKpsd9g=
git.defalsify.org/vise.git v0.2.3-0.20241231085136-8582c7e157d9/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw=
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g=
Expand Down
9 changes: 6 additions & 3 deletions internal/handlers/ussd/menuhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
logg = logging.NewVanilla().WithDomain("ussdmenuhandler")
logg = logging.NewVanilla().WithDomain("ussdmenuhandler").WithContextKey("session-id")
scriptDir = path.Join("services", "registration")
translationDir = path.Join(scriptDir, "locale")
)
Expand Down Expand Up @@ -122,9 +122,12 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource
h.st.Code = []byte{}
}

sessionId, _ := ctx.Value("SessionId").(string)
flag_admin_privilege, _ := h.flagManager.GetFlag("flag_admin_privilege")
sessionId, ok := ctx.Value("SessionId").(string)
if ok {
context.WithValue(ctx, "session-id", sessionId)
}

flag_admin_privilege, _ := h.flagManager.GetFlag("flag_admin_privilege")
isAdmin, _ := h.adminstore.IsAdmin(sessionId)

if isAdmin {
Expand Down

0 comments on commit 5ee10d8

Please sign in to comment.