Skip to content

Commit 159700f

Browse files
authored
Merge pull request #2293 from mkmccarty/mm-branch-3
✨refactor: remove userID, add external secure hasher to Auth API call
2 parents 3a5270c + b9fd730 commit 159700f

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/ei/ei_api.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"net/url"
1414
"os"
15+
"os/exec"
1516
"strings"
1617
"time"
1718

@@ -507,8 +508,36 @@ func APICall(reqURL string, request proto.Message) []byte {
507508
return decodedAuthBuf.Message
508509
}
509510

511+
func getSecureHash(data []byte) (string, error) {
512+
binaryPath := "./secure_hasher"
513+
info, err := os.Stat(binaryPath)
514+
if err != nil {
515+
if os.IsNotExist(err) {
516+
return "", fmt.Errorf("secure hasher binary not found at %s", binaryPath)
517+
}
518+
return "", fmt.Errorf("failed to stat secure hasher binary: %w", err)
519+
}
520+
if info.IsDir() {
521+
return "", fmt.Errorf("secure hasher path is a directory: %s", binaryPath)
522+
}
523+
524+
cmd := exec.Command(binaryPath)
525+
526+
// Pipe the raw bytes into the binary's stdin
527+
cmd.Stdin = bytes.NewReader(data)
528+
529+
var out bytes.Buffer
530+
cmd.Stdout = &out
531+
532+
if err := cmd.Run(); err != nil {
533+
return "", fmt.Errorf("failed to execute hasher: %w", err)
534+
}
535+
536+
return strings.TrimSpace(out.String()), nil
537+
}
538+
510539
// APIAuthenticatedCall wraps the request in an AuthenticatedMessage before sending, then decodes the response the same way as APICall.
511-
func APIAuthenticatedCall(reqURL string, userID string, request proto.Message) []byte {
540+
func APIAuthenticatedCall(reqURL string, request proto.Message) []byte {
512541
enc := base64.StdEncoding
513542

514543
innerBin, err := proto.Marshal(request)
@@ -517,9 +546,15 @@ func APIAuthenticatedCall(reqURL string, userID string, request proto.Message) [
517546
return nil
518547
}
519548

549+
secureHash, err := getSecureHash(innerBin)
550+
if err != nil {
551+
log.Print(err)
552+
return nil
553+
}
554+
520555
authMsg := &AuthenticatedMessage{
521-
Message: []byte(enc.EncodeToString(innerBin)),
522-
UserId: &userID,
556+
Message: innerBin,
557+
Code: &secureHash,
523558
}
524559

525560
reqBin, err := proto.Marshal(authMsg)
@@ -578,9 +613,5 @@ func APIAuthenticatedCall(reqURL string, userID string, request proto.Message) [
578613
decodedAuthBuf.Message = buf.Bytes()
579614
}
580615

581-
if decodedAuthBuf.GetMessage() == nil {
582-
decodedAuthBuf.Message = []byte(decodedAuthBuf.GetCode())
583-
}
584-
585616
return decodedAuthBuf.Message
586617
}

src/ei/ei_api_leaderboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetLeaderboardFromAPI(eggIncID string, scope string, grade Contract_PlayerG
3737
Grade: &grade,
3838
}
3939

40-
payload := APIAuthenticatedCall(reqURL, eiUserID, &leaderboardRequest)
40+
payload := APIAuthenticatedCall(reqURL, &leaderboardRequest)
4141
if payload == nil {
4242
log.Print("GetLeaderboardFromAPI: APICall returned nil response")
4343
return nil

0 commit comments

Comments
 (0)