gosecuresessions

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 6 Imported by: 0

README

go-secure-sessions

A new session module to replace gorilla/sessions. This go-secure-sessions is not dependant on any router.

Uses AES-128, AES-192, or AES-256 to store sessions as cookies.

Using go-secure-sessions

    import "github.com/GolangToolKits/go-secure-sessions"

    // securekey must be at least 16 char long
    // The key argument should be the AES key,
    // either 16, 24, or 32 bytes to select
    // AES-128, AES-192, or AES-256.
    var secretKey = "dsdfs6dfs61dssdfsdfdsdsfsdsdllsd"

    var cf ConfigOptions
	cf.maxAge = 3600
    cf.path= "/"
	sessionManager, err := NewSessionManager(secretKey, cf)
	if err != nil {
		fmt.Println(err)
	}

    r, _ := http.NewRequest("POST", "/test/test1", nil)
    var w http.ResponseWriter

    // If a session cookie already exists in the request, it is loaded
    // Otherwise a completely new session is created with the name given
    session := sessionManager.NewSession(r, "new_test_sesstion")


    type SomeObject struct{
        Id int
        Name string
    }

    obj := SomeObject{
        Id: 1,
        Name: "test"
    }
    
    // needed to serialize and deserialize this object
    gob.Register(SomeObject{})

    //set some session values
    session.Set("test1", "some test1 value")
    session.Set("test2", "some test2 value")
    session.Set("test3", obj)
    // save the session before quitting or the values will be lost
    // session is saved securly as a cookie in the user's browser
    err:= session.Save(w)
    if err != nil{
        log.Println("Session not saved")
    }

    // Read a value out of the session
    v1:= session.Get("test1")
    fmt.Println(v1)


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigOptions

type ConfigOptions struct {
	Path        string
	Domain      string
	MaxAge      int
	SessionType int
}

ConfigOptions ConfigOptions

type CookieSession

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

CookieSession CookieSession

func (*CookieSession) Get

func (s *CookieSession) Get(key string) any

Get Get values

func (*CookieSession) Save

Save Save session

func (*CookieSession) Set

func (s *CookieSession) Set(key string, value any)

Set Set values

type Manager

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

Manager Manager

func (*Manager) NewSession

func (m *Manager) NewSession(r *http.Request, name string) Session

NewSession NewSession Gets session from request if one exists Creates a new session if one doesn't exist in request

type Session

type Session interface {
	Set(key string, value any)
	Get(key string) any
	Save(w http.ResponseWriter) error
}

Session Session

type SessionManager

type SessionManager interface {
	NewSession(r *http.Request, name string) Session
}

SessionManager SessionManager

func NewSessionManager

func NewSessionManager(secretKey string, config ConfigOptions) (SessionManager, error)

NewSessionManager securekey must be at least 16 char long The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

Jump to

Keyboard shortcuts

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