Skip to content

Commit

Permalink
chore(ctxkeys): rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviofernandes004 committed Oct 29, 2024
1 parent d56cede commit 55ec6b6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
9 changes: 0 additions & 9 deletions internal/ctx_env/ctx_env.go

This file was deleted.

9 changes: 9 additions & 0 deletions internal/ctxkeys/ctxkeys.go
Original file line number Diff line number Diff line change
@@ -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{}
10 changes: 5 additions & 5 deletions internal/satori/satori.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
}
Expand Down
14 changes: 7 additions & 7 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}

Expand Down
11 changes: 9 additions & 2 deletions server/api_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 55ec6b6

Please sign in to comment.