Skip to content

Commit

Permalink
NK-622 Update Satori API (heroiclabs#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito authored Jan 24, 2025
1 parent f15adc2 commit 77dd456
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 26 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1
github.com/heroiclabs/nakama-common v1.35.1-0.20250124113809-897f4ac9b74e
github.com/heroiclabs/nakama-common v1.35.1-0.20250124120300-73e94a7d44b9
github.com/heroiclabs/sql-migrate v0.0.0-20241125131053-95a7949783b0
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.7.1
Expand Down Expand Up @@ -75,5 +75,3 @@ require (
golang.org/x/text v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
)

replace github.com/heroiclabs/nakama-common => ../nakama-common
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 h1:VNqngBF40hVlDloBruUehVYC3ArSgIyScOAyMRqBxRg=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1/go.mod h1:RBRO7fro65R6tjKzYgLAFo0t1QEXY1Dp+i/bvpRiqiQ=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/heroiclabs/nakama-common v1.35.1-0.20250124120300-73e94a7d44b9 h1:RvBWv2Zv8CLTwvCrUNIN183Q4cH7anfN5N9/QlOGgKQ=
github.com/heroiclabs/nakama-common v1.35.1-0.20250124120300-73e94a7d44b9/go.mod h1:E4kw2QpsINoXXJS7aOjen1dycPkoo9bD9pYPAjmA8rc=
github.com/heroiclabs/sql-migrate v0.0.0-20241125131053-95a7949783b0 h1:hHJcYOP6L2/wZIEnYjjkJM+rOk/bK0uaYkDAejYpLhI=
github.com/heroiclabs/sql-migrate v0.0.0-20241125131053-95a7949783b0/go.mod h1:uwcmopkVQIfb/JQqul5zmGI9ounclRC08j9S9lLcpRQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
51 changes: 38 additions & 13 deletions internal/satori/satori.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ func (s *SatoriClient) generateToken(ctx context.Context, id string) (string, er
}

type authenticateBody struct {
Id string `json:"id"`
Default map[string]string `json:"default,omitempty"`
Custom map[string]string `json:"custom,omitempty"`
Id string `json:"id"`
Default map[string]string `json:"default,omitempty"`
Custom map[string]string `json:"custom,omitempty"`
NoSession bool `json:"no_session,omitempty"`
}

// @group satori
Expand All @@ -180,25 +181,31 @@ type authenticateBody struct {
// @param id(type=string) The identifier of the identity.
// @param default(type=map[string]string, optional=true, default=nil) Default properties to update with this call. Set to nil to leave them as they are on the server.
// @param custom(type=map[string]string, optional=true, default=nil) Custom properties to update with this call. Set to nil to leave them as they are on the server.
// @param noSession(type=bool) Whether authenticate should skip session duration tracking.
// @param ipAddress(type=string, optional=true, default="") An optional client IP address to pass on to Satori for geo-IP lookup.
// @return error(error) An optional error value if an error occurred.
func (s *SatoriClient) Authenticate(ctx context.Context, id string, defaultProperties, customProperties map[string]string, ipAddress ...string) error {
func (s *SatoriClient) Authenticate(ctx context.Context, id string, defaultProperties, customProperties map[string]string, noSession bool, ipAddress ...string) (*runtime.Properties, error) {
if s.invalidConfig {
return runtime.ErrSatoriConfigurationInvalid
return nil, runtime.ErrSatoriConfigurationInvalid
}

url := s.url.String() + "/v1/authenticate"

body := &authenticateBody{Id: id, Default: defaultProperties, Custom: customProperties}
body := &authenticateBody{
Id: id,
Default: defaultProperties,
Custom: customProperties,
NoSession: noSession,
}

json, err := json.Marshal(body)
jsonBody, err := json.Marshal(body)
if err != nil {
return err
return nil, err
}

req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(json))
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonBody))
if err != nil {
return err
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(s.apiKey, "")
Expand All @@ -212,16 +219,34 @@ func (s *SatoriClient) Authenticate(ctx context.Context, id string, defaultPrope

res, err := s.httpc.Do(req)
if err != nil {
return err
return nil, err
}

defer res.Body.Close()

switch res.StatusCode {
case 200:
return nil
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

props := struct {
Properties runtime.Properties `json:"properties"`
}{
Properties: runtime.Properties{
Default: map[string]string{},
Custom: map[string]string{},
Computed: map[string]string{},
},
}
if err = json.Unmarshal(resBody, &props); err != nil {
return nil, err
}

return &props.Properties, nil
default:
return fmt.Errorf("%d status code", res.StatusCode)
return nil, fmt.Errorf("%d status code", res.StatusCode)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/satori/satori_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package satori

import (
"context"
"github.com/gofrs/uuid/v5"
"os"
"testing"
"time"

"github.com/gofrs/uuid/v5"
"github.com/heroiclabs/nakama-common/runtime"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -37,7 +37,8 @@ func TestSatoriClient_EventsPublish(t *testing.T) {
ctx, ctxCancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancelFn()

if err := client.Authenticate(ctx, identityID, nil, nil); err != nil {
_, err := client.Authenticate(ctx, identityID, nil, nil, true)
if err != nil {
t.Fatalf("error in client.Authenticate: %+v", err)
}

Expand Down
16 changes: 14 additions & 2 deletions server/runtime_javascript_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -8901,6 +8901,7 @@ func (n *RuntimeJavascriptNakamaModule) getSatori(r *goja.Object) func(goja.Func
// @summary Create a new identity.
// @param id(type=string) The identifier of the identity.
// @param properties(type=nkruntime.AuthPropertiesUpdate, optional=true, default=null) Opt. Properties to update.
// @param noSession(type=bool, optional=true, default=true) Whether authenticate should skip session tracking.
// @param ip(type=string, optional=true, default="") An optional client IP address to pass on to Satori for geo-IP lookup.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeJavascriptNakamaModule) satoriAuthenticate(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
Expand All @@ -8926,16 +8927,27 @@ func (n *RuntimeJavascriptNakamaModule) satoriAuthenticate(r *goja.Runtime) func
customPropsMap = getJsStringMap(r, r.ToValue(customProps))
}

noSession := true
if f.Argument(2) != goja.Undefined() && f.Argument(2) != goja.Null() {
noSession = getJsBool(r, f.Argument(2))
}

var ip string
if f.Argument(2) != goja.Undefined() && f.Argument(2) != goja.Null() {
ip = getJsString(r, f.Argument(2))
}

if err := n.satori.Authenticate(n.ctx, id, defPropsMap, customPropsMap, ip); err != nil {
properties, err := n.satori.Authenticate(n.ctx, id, defPropsMap, customPropsMap, noSession, ip)
if err != nil {
n.logger.Error("Failed to Satori Authenticate.", zap.Error(err))
panic(r.NewGoError(fmt.Errorf("failed to satori authenticate: %s", err.Error())))
}
return nil

return r.ToValue(map[string]any{
"default": properties.Default,
"custom": properties.Custom,
"computed": properties.Computed,
})
}
}

Expand Down
16 changes: 13 additions & 3 deletions server/runtime_lua_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -10936,6 +10936,7 @@ func (n *RuntimeLuaNakamaModule) getSatori(l *lua.LState) int {
// @param id(type=string) The identifier of the identity.
// @param defaultProperties(type=table, optional=true, default=nil) Default properties.
// @param customProperties(type=table, optional=true, default=nil) Custom properties.
// @param noSession(type=bool, optional=true, default=true) Whether authenticate should skip session tracking.
// @param ip(type=string) Ip address.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeLuaNakamaModule) satoriAuthenticate(l *lua.LState) int {
Expand Down Expand Up @@ -10963,14 +10964,23 @@ func (n *RuntimeLuaNakamaModule) satoriAuthenticate(l *lua.LState) int {
}
}

ip := l.OptString(4, "")
noSession := l.OptBool(4, true)

if err := n.satori.Authenticate(l.Context(), identifier, defaultPropsMap, customPropsMap, ip); err != nil {
ip := l.OptString(5, "")

properties, err := n.satori.Authenticate(l.Context(), identifier, defaultPropsMap, customPropsMap, noSession, ip)
if err != nil {
l.RaiseError("failed to satori authenticate: %v", err.Error())
return 0
}

return 0
propertiesTable := l.CreateTable(0, 3)
propertiesTable.RawSetString("default", RuntimeLuaConvertMapString(l, properties.Default))
propertiesTable.RawSetString("custom", RuntimeLuaConvertMapString(l, properties.Custom))
propertiesTable.RawSetString("computed", RuntimeLuaConvertMapString(l, properties.Computed))

l.Push(propertiesTable)
return 1
}

// @group satori
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/blugelabs/bluge/search/search.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopena
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options
github.com/grpc-ecosystem/grpc-gateway/v2/runtime
github.com/grpc-ecosystem/grpc-gateway/v2/utilities
# github.com/heroiclabs/nakama-common v1.35.1-0.20250124113809-897f4ac9b74e => ../nakama-common
# github.com/heroiclabs/nakama-common v1.35.1-0.20250124120300-73e94a7d44b9
## explicit; go 1.23.3
github.com/heroiclabs/nakama-common/api
github.com/heroiclabs/nakama-common/rtapi
Expand Down Expand Up @@ -428,4 +428,3 @@ gopkg.in/natefinch/lumberjack.v2
# gopkg.in/yaml.v3 v3.0.1
## explicit
gopkg.in/yaml.v3
# github.com/heroiclabs/nakama-common => ../nakama-common

0 comments on commit 77dd456

Please sign in to comment.