Skip to content

Commit

Permalink
initial session support
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmueller committed Mar 28, 2023
1 parent b1c97d8 commit 83366a3
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 50 deletions.
6 changes: 5 additions & 1 deletion example/zero/controller/posts/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type IndexContext struct {
}

func (c *Controller) Index(ctx *IndexContext) (string, error) {
fmt.Println("got session", ctx.Session)
fmt.Println("got session user id", ctx.Session.UserID)
fmt.Println("is logged in?", !ctx.Session.Visitor())
id := 10
ctx.Session.UserID = &id
// fmt.Println("got session", ctx.Session)
return "Welcome to my blog!", nil
}
10 changes: 5 additions & 5 deletions example/zero/generator/middleware/generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 109 additions & 29 deletions example/zero/generator/session/generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/zero/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)
7 changes: 3 additions & 4 deletions example/zero/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNV
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/hexops/valast v1.4.1 h1:vlB+usah+MLacCyDDqACn2yhAoCDlpHYkpEKtej+RXE=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
Expand All @@ -39,7 +37,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -49,13 +46,15 @@ github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk=
github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
mvdan.cc/gofumpt v0.2.0 h1:AInyCTHfhp4bFrP2VYC5kR2wPwgWj7eGSb+7437zn7I=
18 changes: 8 additions & 10 deletions example/zero/mw/mw.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package mw

import (
"fmt"
"net/http"

"github.com/livebud/bud/example/zero/bud/pkg/sessions"
"github.com/livebud/bud/example/zero/env"
)

type Middleware struct {
*env.Env
*sessions.Store
// *sessions.Store
}

func (m *Middleware) Session(next http.Handler) http.Handler {
fmt.Println(m.Store)
// store := sessions.NewCookieStore([]byte(m.Env.Session.Key))
// _ = store
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
}
// func (m *Middleware) Session(next http.Handler) http.Handler {
// fmt.Println(m.Store)
// // store := sessions.NewCookieStore([]byte(m.Env.Session.Key))
// // _ = store
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// })
// }

func (m *Middleware) CSRF(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 4 additions & 1 deletion example/zero/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package web
import (
"net/http"

"github.com/livebud/bud/example/zero/bud/pkg/sessions"
"github.com/livebud/bud/example/zero/bud/pkg/web/controller"
"github.com/livebud/bud/example/zero/bud/pkg/web/middleware"
"github.com/livebud/bud/example/zero/bud/pkg/web/view"
"github.com/livebud/bud/example/zero/env"
"github.com/livebud/bud/example/zero/mw"
"github.com/livebud/bud/package/router"
)

type Web struct {
Env *env.Env
Controller *controller.Controller
Middleware *middleware.Middleware
MW *mw.Middleware
Expand All @@ -28,7 +31,7 @@ type Web struct {
// }

func (w *Web) Stack(s middleware.Stack) middleware.Interface {
return append(s, w.Middleware)
return append(s, w.Middleware, sessions.New(w.Env.Session.Key))
}

// TODO: use a router interface
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09
github.com/xlab/treeprint v1.1.0
go.kuoruan.net/v8go-polyfills v0.5.1-0.20220727011656-c74c5b408ebd
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ go.kuoruan.net/v8go-polyfills v0.5.1-0.20220727011656-c74c5b408ebd/go.mod h1:egH
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
Expand Down
49 changes: 49 additions & 0 deletions package/cipher/cipher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cipher

import (
"crypto/rand"
"errors"
"io"
)

var ErrDecrypting = errors.New("unable to decrypt")

type Cipher interface {
Encrypt(plaintext []byte) ([]byte, error)
Decrypt(ciphertext []byte) ([]byte, error)
}

func Random(b []byte) error {
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return err
}
return nil
}

func Rotator(latest Cipher, priors ...Cipher) Cipher {
return &rotator{append([]Cipher{latest}, priors...)}
}

type rotator struct {
ciphers []Cipher
}

// Decrypt with any past or present cipher
func (r *rotator) Decrypt(ciphertext []byte) ([]byte, error) {
for _, c := range r.ciphers {
plaintext, err := c.Decrypt(ciphertext)
if err != nil {
if !errors.Is(err, ErrDecrypting) {
return nil, err
}
continue
}
return plaintext, nil
}
return nil, ErrDecrypting
}

// Encrypt with the latest cipher
func (r *rotator) Encrypt(plaintext []byte) ([]byte, error) {
return r.ciphers[0].Encrypt(plaintext)
}

0 comments on commit 83366a3

Please sign in to comment.