update port
This commit is contained in:
12
vendor/github.com/wader/gormstore/.travis.yml
generated
vendored
Normal file
12
vendor/github.com/wader/gormstore/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
sudo: required
|
||||
services:
|
||||
- docker
|
||||
|
||||
language: go
|
||||
go:
|
||||
- "tip"
|
||||
- "1.10"
|
||||
- "1.11"
|
||||
- "1.12"
|
||||
|
||||
script: ./test
|
||||
19
vendor/github.com/wader/gormstore/LICENSE
generated
vendored
Normal file
19
vendor/github.com/wader/gormstore/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2016 Mattias Wadman
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
48
vendor/github.com/wader/gormstore/README.md
generated
vendored
Normal file
48
vendor/github.com/wader/gormstore/README.md
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
#### GORM backend for gorilla sessions
|
||||
|
||||
go get github.com/wader/gormstore
|
||||
|
||||
#### Documentation
|
||||
|
||||
http://www.godoc.org/github.com/wader/gormstore
|
||||
|
||||
#### Example
|
||||
|
||||
```go
|
||||
// initialize and setup cleanup
|
||||
store := gormstore.New(gorm.Open(...), []byte("secret"))
|
||||
// db cleanup every hour
|
||||
// close quit channel to stop cleanup
|
||||
quit := make(chan struct{})
|
||||
go store.PeriodicCleanup(1*time.Hour, quit)
|
||||
```
|
||||
|
||||
```go
|
||||
// in HTTP handler
|
||||
func handlerFunc(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := store.Get(r, "session")
|
||||
session.Values["user_id"] = 123
|
||||
store.Save(r, w, session)
|
||||
http.Error(w, "", http.StatusOK)
|
||||
}
|
||||
```
|
||||
|
||||
For more details see [gormstore godoc documentation](http://www.godoc.org/github.com/wader/gormstore).
|
||||
|
||||
#### Testing
|
||||
|
||||
Just sqlite3 tests:
|
||||
|
||||
go test
|
||||
|
||||
All databases using docker:
|
||||
|
||||
./test
|
||||
|
||||
If docker is not local (docker-machine etc):
|
||||
|
||||
DOCKER_IP=$(docker-machine ip dev) ./test
|
||||
|
||||
#### License
|
||||
|
||||
gormstore is licensed under the MIT license. See [LICENSE](LICENSE) for the full license text.
|
||||
240
vendor/github.com/wader/gormstore/gormstore.go
generated
vendored
Normal file
240
vendor/github.com/wader/gormstore/gormstore.go
generated
vendored
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
Package gormstore is a GORM backend for gorilla sessions
|
||||
|
||||
Simplest form:
|
||||
|
||||
store := gormstore.New(gorm.Open(...), []byte("secret-hash-key"))
|
||||
|
||||
All options:
|
||||
|
||||
store := gormstore.NewOptions(
|
||||
gorm.Open(...), // *gorm.DB
|
||||
gormstore.Options{
|
||||
TableName: "sessions", // "sessions" is default
|
||||
SkipCreateTable: false, // false is default
|
||||
},
|
||||
[]byte("secret-hash-key"), // 32 or 64 bytes recommended, required
|
||||
[]byte("secret-encyption-key")) // nil, 16, 24 or 32 bytes, optional
|
||||
|
||||
// some more settings, see sessions.Options
|
||||
store.SessionOpts.Secure = true
|
||||
store.SessionOpts.HttpOnly = true
|
||||
store.SessionOpts.MaxAge = 60 * 60 * 24 * 60
|
||||
|
||||
If you want periodic cleanup of expired sessions:
|
||||
|
||||
quit := make(chan struct{})
|
||||
go store.PeriodicCleanup(1*time.Hour, quit)
|
||||
|
||||
For more information about the keys see https://github.com/gorilla/securecookie
|
||||
|
||||
For API to use in HTTP handlers see https://github.com/gorilla/sessions
|
||||
*/
|
||||
package gormstore
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/context"
|
||||
"github.com/gorilla/securecookie"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
const sessionIDLen = 32
|
||||
const defaultTableName = "sessions"
|
||||
const defaultMaxAge = 60 * 60 * 24 * 30 // 30 days
|
||||
const defaultPath = "/"
|
||||
|
||||
// Options for gormstore
|
||||
type Options struct {
|
||||
TableName string
|
||||
SkipCreateTable bool
|
||||
}
|
||||
|
||||
// Store represent a gormstore
|
||||
type Store struct {
|
||||
db *gorm.DB
|
||||
opts Options
|
||||
Codecs []securecookie.Codec
|
||||
SessionOpts *sessions.Options
|
||||
}
|
||||
|
||||
type gormSession struct {
|
||||
ID string `sql:"unique_index"`
|
||||
Data string `sql:"type:text"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ExpiresAt time.Time `sql:"index"`
|
||||
|
||||
tableName string `sql:"-"` // just for convenience instead of db.Table(...)
|
||||
}
|
||||
|
||||
// Define a type for context keys so that they can't clash with anything else stored in context
|
||||
type contextKey string
|
||||
|
||||
func (gs *gormSession) TableName() string {
|
||||
return gs.tableName
|
||||
}
|
||||
|
||||
// New creates a new gormstore session
|
||||
func New(db *gorm.DB, keyPairs ...[]byte) *Store {
|
||||
return NewOptions(db, Options{}, keyPairs...)
|
||||
}
|
||||
|
||||
// NewOptions creates a new gormstore session with options
|
||||
func NewOptions(db *gorm.DB, opts Options, keyPairs ...[]byte) *Store {
|
||||
st := &Store{
|
||||
db: db,
|
||||
opts: opts,
|
||||
Codecs: securecookie.CodecsFromPairs(keyPairs...),
|
||||
SessionOpts: &sessions.Options{
|
||||
Path: defaultPath,
|
||||
MaxAge: defaultMaxAge,
|
||||
},
|
||||
}
|
||||
if st.opts.TableName == "" {
|
||||
st.opts.TableName = defaultTableName
|
||||
}
|
||||
|
||||
if !st.opts.SkipCreateTable {
|
||||
st.db.AutoMigrate(&gormSession{tableName: st.opts.TableName})
|
||||
}
|
||||
|
||||
return st
|
||||
}
|
||||
|
||||
// Get returns a session for the given name after adding it to the registry.
|
||||
func (st *Store) Get(r *http.Request, name string) (*sessions.Session, error) {
|
||||
return sessions.GetRegistry(r).Get(st, name)
|
||||
}
|
||||
|
||||
// New creates a session with name without adding it to the registry.
|
||||
func (st *Store) New(r *http.Request, name string) (*sessions.Session, error) {
|
||||
session := sessions.NewSession(st, name)
|
||||
opts := *st.SessionOpts
|
||||
session.Options = &opts
|
||||
|
||||
st.MaxAge(st.SessionOpts.MaxAge)
|
||||
|
||||
// try fetch from db if there is a cookie
|
||||
if cookie, err := r.Cookie(name); err == nil {
|
||||
if err := securecookie.DecodeMulti(name, cookie.Value, &session.ID, st.Codecs...); err != nil {
|
||||
return session, nil
|
||||
}
|
||||
s := &gormSession{tableName: st.opts.TableName}
|
||||
if err := st.db.Where("id = ? AND expires_at > ?", session.ID, gorm.NowFunc()).First(s).Error; err != nil {
|
||||
return session, nil
|
||||
}
|
||||
if err := securecookie.DecodeMulti(session.Name(), s.Data, &session.Values, st.Codecs...); err != nil {
|
||||
return session, nil
|
||||
}
|
||||
|
||||
context.Set(r, contextKey(name), s)
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// Save session and set cookie header
|
||||
func (st *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
|
||||
s, _ := context.Get(r, contextKey(session.Name())).(*gormSession)
|
||||
|
||||
// delete if max age is < 0
|
||||
if session.Options.MaxAge < 0 {
|
||||
if s != nil {
|
||||
if err := st.db.Delete(s).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
http.SetCookie(w, sessions.NewCookie(session.Name(), "", session.Options))
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := securecookie.EncodeMulti(session.Name(), session.Values, st.Codecs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now()
|
||||
expire := now.Add(time.Second * time.Duration(session.Options.MaxAge))
|
||||
|
||||
if s == nil {
|
||||
// generate random session ID key suitable for storage in the db
|
||||
session.ID = strings.TrimRight(
|
||||
base32.StdEncoding.EncodeToString(
|
||||
securecookie.GenerateRandomKey(sessionIDLen)), "=")
|
||||
s = &gormSession{
|
||||
ID: session.ID,
|
||||
Data: data,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
ExpiresAt: expire,
|
||||
tableName: st.opts.TableName,
|
||||
}
|
||||
if err := st.db.Create(s).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
context.Set(r, contextKey(session.Name()), s)
|
||||
} else {
|
||||
s.Data = data
|
||||
s.UpdatedAt = now
|
||||
s.ExpiresAt = expire
|
||||
if err := st.db.Save(s).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// set session id cookie
|
||||
id, err := securecookie.EncodeMulti(session.Name(), session.ID, st.Codecs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
http.SetCookie(w, sessions.NewCookie(session.Name(), id, session.Options))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MaxAge sets the maximum age for the store and the underlying cookie
|
||||
// implementation. Individual sessions can be deleted by setting
|
||||
// Options.MaxAge = -1 for that session.
|
||||
func (st *Store) MaxAge(age int) {
|
||||
st.SessionOpts.MaxAge = age
|
||||
for _, codec := range st.Codecs {
|
||||
if sc, ok := codec.(*securecookie.SecureCookie); ok {
|
||||
sc.MaxAge(age)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MaxLength restricts the maximum length of new sessions to l.
|
||||
// If l is 0 there is no limit to the size of a session, use with caution.
|
||||
// The default is 4096 (default for securecookie)
|
||||
func (st *Store) MaxLength(l int) {
|
||||
for _, c := range st.Codecs {
|
||||
if codec, ok := c.(*securecookie.SecureCookie); ok {
|
||||
codec.MaxLength(l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup deletes expired sessions
|
||||
func (st *Store) Cleanup() {
|
||||
st.db.Delete(&gormSession{tableName: st.opts.TableName}, "expires_at <= ?", gorm.NowFunc())
|
||||
}
|
||||
|
||||
// PeriodicCleanup runs Cleanup every interval. Close quit channel to stop.
|
||||
func (st *Store) PeriodicCleanup(interval time.Duration, quit <-chan struct{}) {
|
||||
t := time.NewTicker(interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
st.Cleanup()
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
60
vendor/github.com/wader/gormstore/test
generated
vendored
Normal file
60
vendor/github.com/wader/gormstore/test
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/bin/bash
|
||||
|
||||
DOCKER_IP=${DOCKER_IP:-127.0.0.1}
|
||||
|
||||
sqlite3() {
|
||||
DATABASE_URI="sqlite3://file:dummy?mode=memory&cache=shared" go test -v -race -cover
|
||||
return $?
|
||||
}
|
||||
|
||||
postgres96() {
|
||||
ID=$(docker run -p 5432 -d postgres:9.6)
|
||||
PORT=$(docker port "$ID" 5432 | cut -d : -f 2)
|
||||
DATABASE_URI="postgres://user=postgres password=postgres dbname=postgres host=$DOCKER_IP port=$PORT sslmode=disable" go test -v -race -cover
|
||||
S=$?
|
||||
docker rm -vf "$ID" > /dev/null
|
||||
return $S
|
||||
}
|
||||
|
||||
postgres94() {
|
||||
ID=$(docker run -p 5432 -d postgres:9.4)
|
||||
PORT=$(docker port "$ID" 5432 | cut -d : -f 2)
|
||||
DATABASE_URI="postgres://user=postgres password=postgres dbname=postgres host=$DOCKER_IP port=$PORT sslmode=disable" go test -v -race -cover
|
||||
S=$?
|
||||
docker rm -vf "$ID" > /dev/null
|
||||
return $S
|
||||
}
|
||||
|
||||
mysql57() {
|
||||
ID=$(docker run \
|
||||
-e MYSQL_ROOT_PASSWORD=root \
|
||||
-e MYSQL_USER=mysql \
|
||||
-e MYSQL_PASSWORD=mysql \
|
||||
-e MYSQL_DATABASE=mysql \
|
||||
-p 3306 -d mysql:5.7)
|
||||
PORT=$(docker port "$ID" 3306 | cut -d : -f 2)
|
||||
DATABASE_URI="mysql://mysql:mysql@tcp($DOCKER_IP:$PORT)/mysql?charset=utf8&parseTime=True" go test -v -race -cover
|
||||
S=$?
|
||||
docker rm -vf "$ID" > /dev/null
|
||||
return $S
|
||||
}
|
||||
|
||||
mysql80() {
|
||||
ID=$(docker run \
|
||||
-e MYSQL_ROOT_PASSWORD=root \
|
||||
-e MYSQL_USER=mysql \
|
||||
-e MYSQL_PASSWORD=mysql \
|
||||
-e MYSQL_DATABASE=mysql \
|
||||
-p 3306 -d mysql:8.0)
|
||||
PORT=$(docker port "$ID" 3306 | cut -d : -f 2)
|
||||
DATABASE_URI="mysql://mysql:mysql@tcp($DOCKER_IP:$PORT)/mysql?charset=utf8&parseTime=True" go test -v -race -cover
|
||||
S=$?
|
||||
docker rm -vf "$ID" > /dev/null
|
||||
return $S
|
||||
}
|
||||
|
||||
sqlite3 || exit 1
|
||||
postgres94 || exit 1
|
||||
postgres96 || exit 1
|
||||
mysql57 || exit 1
|
||||
mysql80 || exit 1
|
||||
Reference in New Issue
Block a user