phpsessgo

package module
v0.0.0-...-5cecd17 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2021 License: MIT Imports: 9 Imported by: 0

README

PHPSESSGO (Experimental)

The project aimed to imitating PHP Session Management in as much aspect as possible. This library may useful to porting PHP to Go without changing the request contracts. Any session parameter that created/modified should be accessible from PHP Server with same session source.

Usage Example

Create new session manager

import (
	"github.com/eligundry/phpsessgo"
)

sessionManager := phpsessgo.NewSessionManager( 
	phpsessgo.DefaultSessionName,
	&phpsessgo.UUIDCreator{},
	&phpsessgo.RedisSessionHandler{
		Client:         client,
		RedisKeyPrefix: DefaultRedisKeyPrefix,
	},
	&phpsessgo.PHPSessionEncoder{},
	phpsessgo.SessionManagerConfig{
		Expiration:     time.Hour * 24,
		CookiePath:     "/",
		CookieHttpOnly: true,
		CookieDomain:   "localhost",
		CookieSecure:   true,
	},
)

Example of HTTP Handler function

func handleFunc(w http.ResponseWriter, r *http.Request) {
	// PHP: session_start();
	session, err := sessionManager.Start(w, r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(err.Error()))
		return
	}
	defer sessionManager.Save(session)

	// PHP: $_SESSION["hello"] = "world";
	session.Value["hello"] = "world"

	// PHP: session_id();
	w.Write([]byte(session.SessionID))
}

Examples

Build and run the examples

# example using golang standard http library
make standard-http-example 

# example using echo web framework
make echo-middleware-example

Documentation

Overview

Package phpsessgo is a generated GoMock package.

Index

Constants

View Source
const (
	DefaultSessionName    = "PHPSESSID"
	DefaultRedisKeyPrefix = "PHPREDIS_SESSION:"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MockSessionManager

type MockSessionManager struct {
	// contains filtered or unexported fields
}

MockSessionManager is a mock of SessionManager interface

func NewMockSessionManager

func NewMockSessionManager(ctrl *gomock.Controller) *MockSessionManager

NewMockSessionManager creates a new mock instance

func (*MockSessionManager) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockSessionManager) Encoder

func (m *MockSessionManager) Encoder() SessionEncoder

Encoder mocks base method

func (*MockSessionManager) Handler

func (m *MockSessionManager) Handler() SessionHandler

Handler mocks base method

func (*MockSessionManager) SIDCreator

func (m *MockSessionManager) SIDCreator() SessionIDCreator

SIDCreator mocks base method

func (*MockSessionManager) Save

func (m *MockSessionManager) Save(session *Session) error

Save mocks base method

func (*MockSessionManager) SessionName

func (m *MockSessionManager) SessionName() string

SessionName mocks base method

func (*MockSessionManager) SetCookieString

func (m *MockSessionManager) SetCookieString(arg0 string) string

SetCookieString mocks base method

func (*MockSessionManager) Start

Start mocks base method

type MockSessionManagerMockRecorder

type MockSessionManagerMockRecorder struct {
	// contains filtered or unexported fields
}

MockSessionManagerMockRecorder is the mock recorder for MockSessionManager

func (*MockSessionManagerMockRecorder) Encoder

Encoder indicates an expected call of Encoder

func (*MockSessionManagerMockRecorder) Handler

Handler indicates an expected call of Handler

func (*MockSessionManagerMockRecorder) SIDCreator

func (mr *MockSessionManagerMockRecorder) SIDCreator() *gomock.Call

SIDCreator indicates an expected call of SIDCreator

func (*MockSessionManagerMockRecorder) Save

func (mr *MockSessionManagerMockRecorder) Save(session interface{}) *gomock.Call

Save indicates an expected call of Save

func (*MockSessionManagerMockRecorder) SessionName

func (mr *MockSessionManagerMockRecorder) SessionName() *gomock.Call

SessionName indicates an expected call of SessionName

func (*MockSessionManagerMockRecorder) SetCookieString

func (mr *MockSessionManagerMockRecorder) SetCookieString(arg0 interface{}) *gomock.Call

SetCookieString indicates an expected call of SetCookieString

func (*MockSessionManagerMockRecorder) Start

func (mr *MockSessionManagerMockRecorder) Start(w, r interface{}) *gomock.Call

Start indicates an expected call of Start

type PHPSessionEncoder

type PHPSessionEncoder struct {
	SessionEncoder
}

func (*PHPSessionEncoder) Decode

func (*PHPSessionEncoder) Encode

func (e *PHPSessionEncoder) Encode(session phpencode.PhpSession) (string, error)

type RedisSessionHandler

type RedisSessionHandler struct {
	SessionHandler
	Expiration     time.Duration
	Client         *redis.Client
	RedisKeyPrefix string
}

RedisSessionHandler session management using redis

func (*RedisSessionHandler) Close

func (h *RedisSessionHandler) Close()

Close the resource

func (*RedisSessionHandler) Read

func (h *RedisSessionHandler) Read(sessionID string) (data string, err error)

func (*RedisSessionHandler) Write

func (h *RedisSessionHandler) Write(sessionID string, sessionData string) error

type Session

type Session struct {
	SessionID string
	Value     phpencode.PhpSession
}

Session handle creation/modification of session parametr

func NewSession

func NewSession() *Session

NewSession create new instance of Session

type SessionEncoder

type SessionEncoder interface {
	Encode(session phpencode.PhpSession) (string, error)
	Decode(raw string) (phpencode.PhpSession, error)
}

type SessionHandler

type SessionHandler interface {
	Close()
	// Gc(maxLifeTime int) int
	Read(sessionID string) (string, error)
	Write(sessionID, sessionData string) error
}

SessionHandler is adoption of PHP SessionHandlerInterface For more reference: https://www.php.net/manual/en/class.sessionhandlerinterface.php

type SessionIDCreator

type SessionIDCreator interface {
	CreateSID() string
}

SessionIDCreator is adoptation of PHP SessionIDInterface Reference at https://www.php.net/manual/en/class.sessionidinterface.php

type SessionManager

type SessionManager interface {
	Start(w http.ResponseWriter, r *http.Request) (session *Session, err error)
	Save(session *Session) error
	SessionName() string
	SIDCreator() SessionIDCreator
	Handler() SessionHandler
	Encoder() SessionEncoder
	SetCookieString(string) string
}

func NewRedisSessionManager

func NewRedisSessionManager(client *redis.Client, config SessionManagerConfig) SessionManager

NewRedisSessionManager create new instance of SessionManager

func NewSessionManager

func NewSessionManager(
	sessionName string,
	sidCreator SessionIDCreator,
	handler SessionHandler,
	encoder SessionEncoder,
	config SessionManagerConfig,
) SessionManager

type SessionManagerConfig

type SessionManagerConfig struct {
	Expiration     time.Duration
	CookiePath     string
	CookieHttpOnly bool
	CookieDomain   string
	CookieSecure   bool
}

type UUIDCreator

type UUIDCreator struct {
	SessionIDCreator
}

UUIDCreator generate session ID using UUID V4

func (*UUIDCreator) CreateSID

func (c *UUIDCreator) CreateSID() string

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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