Skip to content

Commit

Permalink
Allow storage of arbitrary string key-value pairs in session tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
mofirouz committed Aug 20, 2019
1 parent 798a97c commit e222a57
Show file tree
Hide file tree
Showing 14 changed files with 678 additions and 631 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Log better startup error message when database schema is not set up at all.
- New `check` command to validate runtime modules without starting the server.
- Add discrete channel identifier fields in all messages and message history listings.
- Session tokens now allow storage of arbitrary string key-value pairs.

### Changed
- Use Go 1.12.9 on Alpine 3.10 as base Docker container image and native builds.
Expand Down
4 changes: 2 additions & 2 deletions server/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (p *Pipeline) ProcessRequest(logger *zap.Logger, session Session, envelope
messageNameID = strings.ToLower(messageName)

if fn := p.runtime.BeforeRt(messageNameID); fn != nil {
hookResult, hookErr := fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), envelope)
hookResult, hookErr := fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Vars(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), envelope)

if hookErr != nil {
// Errors from before hooks do not close the session.
Expand All @@ -154,7 +154,7 @@ func (p *Pipeline) ProcessRequest(logger *zap.Logger, session Session, envelope

if messageName != "" {
if fn := p.runtime.AfterRt(messageNameID); fn != nil {
fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), envelope)
fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Vars(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), envelope)
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/pipeline_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p *Pipeline) rpc(logger *zap.Logger, session Session, envelope *rtapi.Enve
return
}

result, fnErr, _ := fn(session.Context(), nil, session.UserID().String(), session.Username(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), rpcMessage.Payload)
result, fnErr, _ := fn(session.Context(), nil, session.UserID().String(), session.Username(), session.Vars(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), rpcMessage.Payload)
if fnErr != nil {
session.Send(&rtapi.Envelope{Cid: envelope.Cid, Message: &rtapi.Envelope_Error{Error: &rtapi.Error{
Code: int32(rtapi.Error_RUNTIME_FUNCTION_EXCEPTION),
Expand Down
242 changes: 121 additions & 121 deletions server/runtime.go

Large diffs are not rendered by default.

488 changes: 244 additions & 244 deletions server/runtime_go.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion server/runtime_go_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/heroiclabs/nakama/runtime"
)

func NewRuntimeGoContext(ctx context.Context, env map[string]string, mode RuntimeExecutionMode, queryParams map[string][]string, sessionExpiry int64, userID, username, sessionID, clientIP, clientPort string) context.Context {
func NewRuntimeGoContext(ctx context.Context, env map[string]string, mode RuntimeExecutionMode, queryParams map[string][]string, sessionExpiry int64, userID, username string, vars map[string]string, sessionID, clientIP, clientPort string) context.Context {
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_ENV, env)
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_MODE, mode.String())

Expand All @@ -30,6 +30,9 @@ func NewRuntimeGoContext(ctx context.Context, env map[string]string, mode Runtim
if userID != "" {
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_USER_ID, userID)
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_USERNAME, username)
if vars != nil {
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_VARS, vars)
}
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_USER_SESSION_EXP, sessionExpiry)
if sessionID != "" {
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_SESSION_ID, sessionID)
Expand Down
2 changes: 1 addition & 1 deletion server/runtime_go_match_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type RuntimeGoMatchCore struct {

func NewRuntimeGoMatchCore(logger *zap.Logger, matchRegistry MatchRegistry, router MessageRouter, id uuid.UUID, node string, db *sql.DB, env map[string]string, nk runtime.NakamaModule, match runtime.Match) (RuntimeMatchCore, error) {
ctx, ctxCancelFn := context.WithCancel(context.Background())
ctx = NewRuntimeGoContext(ctx, env, RuntimeExecutionModeMatch, nil, 0, "", "", "", "", "")
ctx = NewRuntimeGoContext(ctx, env, RuntimeExecutionModeMatch, nil, 0, "", "", nil, "", "", "")
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_MATCH_ID, fmt.Sprintf("%v.%v", id.String(), node))
ctx = context.WithValue(ctx, runtime.RUNTIME_CTX_MATCH_NODE, node)

Expand Down
4 changes: 2 additions & 2 deletions server/runtime_go_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (n *RuntimeGoNakamaModule) AuthenticateSteam(ctx context.Context, token, us
return AuthenticateSteam(ctx, n.logger, n.db, n.socialClient, n.config.GetSocial().Steam.AppID, n.config.GetSocial().Steam.PublisherKey, token, username, create)
}

func (n *RuntimeGoNakamaModule) AuthenticateTokenGenerate(userID, username string, exp int64) (string, int64, error) {
func (n *RuntimeGoNakamaModule) AuthenticateTokenGenerate(userID, username string, vars map[string]string, exp int64) (string, int64, error) {
if userID == "" {
return "", 0, errors.New("expects user id")
}
Expand All @@ -263,7 +263,7 @@ func (n *RuntimeGoNakamaModule) AuthenticateTokenGenerate(userID, username strin
exp = time.Now().UTC().Add(time.Duration(n.config.GetSession().TokenExpirySec) * time.Second).Unix()
}

token, exp := generateTokenWithExpiry(n.config, userID, username, exp)
token, exp := generateTokenWithExpiry(n.config, userID, username, vars, exp)
return token, exp, nil
}

Expand Down
Loading

0 comments on commit e222a57

Please sign in to comment.