From 55ec6b6b1ff3adc57eeee483bafce509c2e2996f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Fernandes?= Date: Fri, 25 Oct 2024 14:07:18 +0100 Subject: [PATCH] chore(ctxkeys): rename package --- internal/ctx_env/ctx_env.go | 9 --------- internal/ctxkeys/ctxkeys.go | 9 +++++++++ internal/satori/satori.go | 10 +++++----- server/api.go | 14 +++++++------- server/api_rpc.go | 11 +++++++++-- 5 files changed, 30 insertions(+), 23 deletions(-) delete mode 100644 internal/ctx_env/ctx_env.go create mode 100644 internal/ctxkeys/ctxkeys.go diff --git a/internal/ctx_env/ctx_env.go b/internal/ctx_env/ctx_env.go deleted file mode 100644 index d1e669a45..000000000 --- a/internal/ctx_env/ctx_env.go +++ /dev/null @@ -1,9 +0,0 @@ -package ctx_env - -// Keys used for storing/retrieving user information in the context of a request after authentication. -type CtxUserIDKey struct{} -type CtxUsernameKey struct{} -type CtxVarsKey struct{} -type CtxExpiryKey struct{} -type CtxTokenIDKey struct{} -type CtxTokenIssuedAtKey struct{} diff --git a/internal/ctxkeys/ctxkeys.go b/internal/ctxkeys/ctxkeys.go new file mode 100644 index 000000000..a7d28e170 --- /dev/null +++ b/internal/ctxkeys/ctxkeys.go @@ -0,0 +1,9 @@ +package ctxkeys + +// Keys used for storing/retrieving user information in the context of a request after authentication. +type UserIDKey struct{} +type UsernameKey struct{} +type VarsKey struct{} +type ExpiryKey struct{} +type TokenIDKey struct{} +type TokenIssuedAtKey struct{} diff --git a/internal/satori/satori.go b/internal/satori/satori.go index 7ea495ef9..770b5e5bb 100644 --- a/internal/satori/satori.go +++ b/internal/satori/satori.go @@ -30,7 +30,7 @@ import ( "github.com/golang-jwt/jwt/v4" "github.com/heroiclabs/nakama-common/runtime" - "github.com/heroiclabs/nakama/v3/internal/ctx_env" + "github.com/heroiclabs/nakama/v3/internal/ctxkeys" "go.uber.org/zap" ) @@ -122,16 +122,16 @@ func (stc *sessionTokenClaims) Valid() error { } func (s *SatoriClient) generateToken(ctx context.Context, id string) (string, error) { - tid, _ := ctx.Value(ctx_env.CtxTokenIDKey{}).(string) - tIssuedAt, _ := ctx.Value(ctx_env.CtxTokenIssuedAtKey{}).(int64) - tExpirySec, _ := ctx.Value(ctx_env.CtxExpiryKey{}).(int64) + tid, _ := ctx.Value(ctxkeys.TokenIDKey{}).(string) + tIssuedAt, _ := ctx.Value(ctxkeys.TokenIssuedAtKey{}).(int64) + tExpirySec, _ := ctx.Value(ctxkeys.ExpiryKey{}).(int64) timestamp := time.Now().UTC() if tIssuedAt == 0 && tExpirySec > s.nakamaTokenExpirySec { // Token was issued before 'IssuedAt' had been added to the session token. // Thus Nakama will make a guess of that value. tIssuedAt = tExpirySec - s.nakamaTokenExpirySec - } else { + } else if tIssuedAt == 0 { // Unable to determine the token's issued at. tIssuedAt = timestamp.Unix() } diff --git a/server/api.go b/server/api.go index 98d326f5c..1f0957fa2 100644 --- a/server/api.go +++ b/server/api.go @@ -39,7 +39,7 @@ import ( grpcgw "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/heroiclabs/nakama-common/api" "github.com/heroiclabs/nakama/v3/apigrpc" - "github.com/heroiclabs/nakama/v3/internal/ctx_env" + "github.com/heroiclabs/nakama/v3/internal/ctxkeys" "github.com/heroiclabs/nakama/v3/social" "go.uber.org/zap" "google.golang.org/grpc" @@ -61,12 +61,12 @@ var once sync.Once const byteBracket byte = '{' // Keys used for storing/retrieving user information in the context of a request after authentication. -type ctxUserIDKey = ctx_env.CtxUserIDKey -type ctxUsernameKey = ctx_env.CtxUsernameKey -type ctxVarsKey = ctx_env.CtxVarsKey -type ctxExpiryKey = ctx_env.CtxExpiryKey -type ctxTokenIDKey = ctx_env.CtxTokenIDKey -type ctxTokenIssuedAtKey = ctx_env.CtxTokenIssuedAtKey +type ctxUserIDKey = ctxkeys.UserIDKey +type ctxUsernameKey = ctxkeys.UsernameKey +type ctxVarsKey = ctxkeys.VarsKey +type ctxExpiryKey = ctxkeys.ExpiryKey +type ctxTokenIDKey = ctxkeys.TokenIDKey +type ctxTokenIssuedAtKey = ctxkeys.TokenIssuedAtKey type ctxFullMethodKey struct{} diff --git a/server/api_rpc.go b/server/api_rpc.go index e8247338b..839800e71 100644 --- a/server/api_rpc.go +++ b/server/api_rpc.go @@ -51,6 +51,7 @@ func (s *ApiServer) RpcFuncHttp(w http.ResponseWriter, r *http.Request) { var username string var vars map[string]string var expiry int64 + requestCtx := r.Context() if httpKey := queryParams.Get("http_key"); httpKey != "" { if httpKey != s.config.GetRuntime().HTTPKey { // HTTP key did not match. @@ -76,7 +77,13 @@ func (s *ApiServer) RpcFuncHttp(w http.ResponseWriter, r *http.Request) { } } else { var tokenId string - userID, username, vars, expiry, tokenId, _, isTokenAuth = parseBearerAuth([]byte(s.config.GetSession().EncryptionKey), auth[0]) + var tokenIssuedAt int64 + userID, username, vars, expiry, tokenId, tokenIssuedAt, isTokenAuth = parseBearerAuth([]byte(s.config.GetSession().EncryptionKey), auth[0]) + requestCtx = context.WithValue(requestCtx, ctxTokenIDKey{}, tokenId) + requestCtx = context.WithValue(requestCtx, ctxExpiryKey{}, expiry) + requestCtx = context.WithValue(requestCtx, ctxTokenIssuedAtKey{}, tokenIssuedAt) + requestCtx = context.WithValue(requestCtx, ctxVarsKey{}, vars) + if !isTokenAuth || !s.sessionCache.IsValidSession(userID, expiry, tokenId) { // Auth token not valid or expired. w.Header().Set("content-type", "application/json") @@ -206,7 +213,7 @@ func (s *ApiServer) RpcFuncHttp(w http.ResponseWriter, r *http.Request) { } // Execute the function. - result, fnErr, code := fn(r.Context(), headers, queryParams, uid, username, vars, expiry, "", clientIP, clientPort, "", payload) + result, fnErr, code := fn(requestCtx, headers, queryParams, uid, username, vars, expiry, "", clientIP, clientPort, "", payload) if fnErr != nil { response, _ := json.Marshal(map[string]interface{}{"error": fnErr, "message": fnErr.Error(), "code": code}) w.Header().Set("content-type", "application/json")