phpsessgo

package module
v0.0.0-...-f52026a Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 7 Imported by: 1

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/tiket-oss/phpsessgo"
)

sessionManager := phpsessgo.NewSessionManager( 
	phpsessgo.DefaultSessionName,
	&phpsessgo.UUIDCreator{},
	&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"
)

Variables

This section is empty.

Functions

func Read

func Read(sessionID string) (data string, err error)

func Write

func Write(sessionID string, sessionData string) error

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) 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) 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 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 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
	Encoder() SessionEncoder
	SetCookieString(string) string
}

func NewSessionManager

func NewSessionManager(config SessionManagerConfig) SessionManager

NewSessionManager create new instance of SessionManager

func NewSessionManagerRaw

func NewSessionManagerRaw(
	sessionName string,
	sidCreator SessionIDCreator,
	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
examples
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