Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KenWilliamson committed Feb 22, 2023
1 parent e3bf395 commit 4876dd8
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 153 deletions.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
# go-secure-sessions
A new session module to replace gorilla/sessions.
This go-secure-sessions is not dependant on any router
This go-secure-sessions is not dependant on any router

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


### Using go-secure-sessions

```go

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 securily as a cookie in the user's browser
err:= session.Save(w)
if err != nil{
log.Println("Sesion not saved")
}

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


```
1 change: 0 additions & 1 deletion configOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gosecuresessions

// ConfigOptions ConfigOptions
type ConfigOptions struct {
//secretKey string
path string
domain string
maxAge int
Expand Down
49 changes: 18 additions & 31 deletions cookieSession.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package gosecuresessions

import (
"errors"
"net/http"

cok "github.com/GolangToolKits/go-secure-cookies"
//cok "github.com/GolangToolKits/go-secure-cookies"
)

// CookieSession CookieSession
type CookieSession struct {
id string
name string
// store Store
id string
name string
values map[any]any
cookies cok.Cookies
// secretKey string
path string
domain string
maxAge int
manager *Manager
path string
domain string
maxAge int
}

// Set Set
// Set Set values
func (s *CookieSession) Set(key string, value any) {
if s.values == nil {
s.values = make(map[any]any)
}
s.values[key] = value
}

// Get Get
// Get Get values
func (s *CookieSession) Get(key string) any {
var rtn any
if s.values == nil {
Expand All @@ -38,23 +36,12 @@ func (s *CookieSession) Get(key string) any {
return rtn
}

// Save Save
func (s *CookieSession) Save(r *http.Request, w http.ResponseWriter) error {

return nil
// Save Save session
func (s *CookieSession) Save(w http.ResponseWriter) error {
var rtnErr = errors.New("Warning: Failed to save session")
suc := s.manager.saveSession(w, s)
if suc {
rtnErr = nil
}
return rtnErr
}

// // Save Save
// func (s *SecureSession) Save(r *http.Request, w http.ResponseWriter) bool {
// return false
// }

// // Name Name
// func (s *SecureSession) Name() string {
// return ""
// }

// // Store Store
// func (s *SecureSession) Store() SessionStore {
// return nil
// }
99 changes: 88 additions & 11 deletions cookieSession_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gosecuresessions

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

Expand Down Expand Up @@ -39,13 +42,13 @@ func TestCookieSession_Set(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
cookies: tt.fields.cookies,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
//cookies: tt.fields.cookies,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
s.Set(tt.args.key, tt.args.value)
if tt.name == "test 1" && s.Get("test1") != "a test 1" {
Expand Down Expand Up @@ -79,24 +82,98 @@ func TestCookieSession_Get(t *testing.T) {
{
name: "test 1",
args: args{
key:"test1",
key: "test1",
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
//cookies: tt.fields.cookies,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
if got := s.Get(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CookieSession.Get() = %v, want %v", got, tt.want)
}
})
}
}

func TestCookieSession_Save(t *testing.T) {
var cf ConfigOptions
cf.maxAge = 3600
m, err := NewSessionManager("dsdfs6dfs61dssdfsdfdsdsfsdsdllsd", cf)
if err != nil {
fmt.Println(err)
}
v := make(map[any]any)
v["test1"] = "test11111"
v["test2"] = "test22222"

tw := httptest.NewRecorder()
tr, _ := http.NewRequest("POST", "/test/test1", nil)

type fields struct {
id string
name string
values map[any]any
manager *Manager
path string
domain string
maxAge int
}
type args struct {
w http.ResponseWriter
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
name: "test_session",
values: v,
manager: m.(*Manager),
path: "/",
maxAge: 3600,
},
args: args{
w: tw,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &CookieSession{
id: tt.fields.id,
name: tt.fields.name,
values: tt.fields.values,
cookies: tt.fields.cookies,
manager: tt.fields.manager,
path: tt.fields.path,
domain: tt.fields.domain,
maxAge: tt.fields.maxAge,
}
if got := s.Get(tt.args.key); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CookieSession.Get() = %v, want %v", got, tt.want)
if err := s.Save(tt.args.w); (err != nil) != tt.wantErr {
t.Errorf("CookieSession.Save() error = %v, wantErr %v", err, tt.wantErr)
}

cook := tw.Result().Cookies()
if len(cook) > 0 {
tr.AddCookie(cook[0])
}
ses := m.NewSession(tr, "test_session")
if ses.Get("test2") != "test22222" {
t.Fail()
}
})
}
Expand Down
103 changes: 55 additions & 48 deletions coverage.out
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
mode: set
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:37.88,41.16 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:50.2,50.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:41.16,47.3 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:47.8,49.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:54.68,56.43 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:77.2,77.12 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:56.43,59.32 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:59.32,61.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:61.9,67.27 5 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:72.4,74.14 3 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:67.27,69.5 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:69.10,71.5 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:81.77,85.28 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:95.2,95.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:85.28,87.17 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:87.17,89.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:89.9,91.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:92.8,94.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:98.63,102.14 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:127.2,127.11 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:130.2,130.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:102.14,107.17 5 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:107.17,109.18 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:109.18,111.19 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:111.19,113.20 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:113.20,115.21 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:115.21,117.22 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:117.22,120.9 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:127.11,129.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:133.66,140.16 6 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:166.2,166.11 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:169.2,169.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:140.16,144.17 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:144.17,146.18 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:146.18,148.19 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:148.19,150.20 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:150.20,152.21 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:152.21,154.22 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:154.22,159.9 3 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:166.11,168.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:23.52,24.21 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:27.2,27.23 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:24.21,26.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:31.45,33.21 2 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:38.2,38.12 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:33.21,35.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:35.8,37.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:42.76,44.2 1 0
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:32.88,36.16 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:44.2,44.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:36.16,41.3 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:41.8,43.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:50.68,52.43 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:70.2,70.12 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:52.43,54.32 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:54.32,56.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:56.9,60.27 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:65.4,67.14 3 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:60.27,62.5 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:62.10,64.5 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:74.70,76.14 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:92.2,92.12 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:76.14,79.17 3 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:79.17,87.18 8 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:87.18,89.5 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:96.77,100.28 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:110.2,110.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:100.28,102.17 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:102.17,104.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:104.9,106.4 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:107.8,109.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:113.63,117.14 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:142.2,142.11 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:145.2,145.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:117.14,122.17 5 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:122.17,124.18 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:124.18,126.19 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:126.19,128.20 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:128.20,130.21 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:130.21,132.22 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:132.22,135.9 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:142.11,144.3 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:148.66,155.16 6 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:182.2,182.11 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:185.2,185.20 1 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:155.16,159.17 4 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:159.17,161.18 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:161.18,163.19 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:163.19,165.20 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:165.20,167.21 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:167.21,169.22 2 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:169.22,175.9 3 1
github.com/GolangToolKits/go-secure-sessions/sessionManager.go:182.11,184.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:21.52,22.21 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:25.2,25.23 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:22.21,24.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:29.45,31.21 2 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:36.2,36.12 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:31.21,33.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:33.8,35.3 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:40.59,43.9 3 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:46.2,46.15 1 1
github.com/GolangToolKits/go-secure-sessions/cookieSession.go:43.9,45.3 1 1
Loading

0 comments on commit 4876dd8

Please sign in to comment.