session

package
v0.0.0-...-d027d8b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionKey     = "session_key"
	SessionExpires = 7 * 24 * time.Hour
)

Variables

This section is empty.

Functions

func AppIDInCtx

func AppIDInCtx(ctx context.Context) (int32, bool)

AppIDInCtx returns the app id from the context. Notice: NewSessionMD must be used in your service, or else this function always returns false.

func AppIDInCtxOrEmpty

func AppIDInCtxOrEmpty(ctx context.Context) int32

AppIDInCtxOrEmpty returns the app id from the context, 0 if not present.

func NewSessionMD

func NewSessionMD(ap AuthProvider, ss ISessionService) func(next endpoint.Endpoint) endpoint.Endpoint

NewSessionMD creates a session middleware for Kitex.

func UserIDInCtx

func UserIDInCtx(ctx context.Context) (string, bool)

UserIDInCtx returns the user ID from the context. Notice: NewSessionMD must be used in your service, or else this function always returns false.

func UserIDInCtxOrEmpty

func UserIDInCtxOrEmpty(ctx context.Context) string

UserIDInCtxOrEmpty returns the user id from the context, empty string if not present.

func WithCtxUser

func WithCtxUser(ctx context.Context, user *User) context.Context

Types

type AuthProvider

type AuthProvider interface {
	GetLoginUser(ctx context.Context, req any) (*User, error)
}

type AuthProviderFn

type AuthProviderFn func(ctx context.Context, req any) (*User, error)

AuthProviderFn 是 AuthProvider 的函数类型实现

func (AuthProviderFn) GetLoginUser

func (fn AuthProviderFn) GetLoginUser(ctx context.Context, req any) (*User, error)

type ISessionService

type ISessionService interface {
	ValidateSession(ctx context.Context, sessionID string) (*Session, error)
	GenerateSessionKey(ctx context.Context, session *Session) (string, error)
}

func NewSessionService

func NewSessionService() ISessionService

type Session

type Session struct {
	UserID string

	SessionID int64
	CreatedAt time.Time
	ExpiresAt time.Time
}

type User

type User struct {
	AppID int32  `json:"app_id,omitempty"`
	ID    string `json:"id"`
	Name  string `json:"name,omitempty"`
	Email string `json:"email,omitempty"`
}

func UserInCtx

func UserInCtx(ctx context.Context) (*User, bool)

UserInCtx returns the user from the context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL