This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/db/session_key.go

115 lines
2.6 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package db
import (
"errors"
"fmt"
"reflect"
2015-08-18 05:57:27 +05:30
"time"
"github.com/go-gorp/gorp"
2015-08-18 05:57:27 +05:30
"github.com/jonboulle/clockwork"
"github.com/coreos/dex/pkg/log"
"github.com/coreos/dex/session"
)
const (
sessionKeyTableName = "session_key"
)
func init() {
register(table{
name: sessionKeyTableName,
model: sessionKeyModel{},
autoinc: false,
pkey: []string{"key"},
})
}
type sessionKeyModel struct {
Key string `db:"key"`
SessionID string `db:"session_id"`
ExpiresAt int64 `db:"expires_at"`
Stale bool `db:"stale"`
}
func NewSessionKeyRepo(dbm *gorp.DbMap) *SessionKeyRepo {
return NewSessionKeyRepoWithClock(dbm, clockwork.NewRealClock())
}
func NewSessionKeyRepoWithClock(dbm *gorp.DbMap, clock clockwork.Clock) *SessionKeyRepo {
return &SessionKeyRepo{dbMap: dbm, clock: clock}
}
type SessionKeyRepo struct {
dbMap *gorp.DbMap
clock clockwork.Clock
}
func (r *SessionKeyRepo) Push(sk session.SessionKey, exp time.Duration) error {
skm := &sessionKeyModel{
Key: sk.Key,
SessionID: sk.SessionID,
ExpiresAt: r.clock.Now().Unix() + int64(exp.Seconds()),
Stale: false,
}
return r.dbMap.Insert(skm)
}
func (r *SessionKeyRepo) Pop(key string) (string, error) {
m, err := r.dbMap.Get(sessionKeyModel{}, key)
if err != nil {
return "", err
}
if m == nil {
return "", errors.New("session key does not exist")
}
2015-08-18 05:57:27 +05:30
skm, ok := m.(*sessionKeyModel)
if !ok {
log.Errorf("expected sessionKeyModel but found %v", reflect.TypeOf(m))
2015-08-18 05:57:27 +05:30
return "", errors.New("unrecognized model")
}
if skm.Stale || skm.ExpiresAt < r.clock.Now().Unix() {
return "", errors.New("invalid session key")
}
2016-02-09 05:31:16 +05:30
qt := r.dbMap.Dialect.QuotedTableForQuery("", sessionKeyTableName)
2015-08-18 05:57:27 +05:30
q := fmt.Sprintf("UPDATE %s SET stale=$1 WHERE key=$2 AND stale=$3", qt)
2016-02-09 05:31:16 +05:30
res, err := executor(r.dbMap, nil).Exec(q, true, key, false)
2015-08-18 05:57:27 +05:30
if err != nil {
return "", err
}
if n, err := res.RowsAffected(); n != 1 {
if err != nil {
log.Errorf("Failed determining rows affected by UPDATE session_key query: %v", err)
}
return "", fmt.Errorf("failed to pop entity")
}
return skm.SessionID, nil
}
func (r *SessionKeyRepo) purge() error {
2016-02-09 05:31:16 +05:30
qt := r.dbMap.Dialect.QuotedTableForQuery("", sessionKeyTableName)
2015-08-18 05:57:27 +05:30
q := fmt.Sprintf("DELETE FROM %s WHERE stale = $1 OR expires_at < $2", qt)
2016-02-09 05:31:16 +05:30
res, err := executor(r.dbMap, nil).Exec(q, true, r.clock.Now().Unix())
2015-08-18 05:57:27 +05:30
if err != nil {
return err
}
d := "unknown # of"
if n, err := res.RowsAffected(); err == nil {
if n == 0 {
return nil
}
d = fmt.Sprintf("%d", n)
}
log.Infof("Deleted %s stale row(s) from %s table", d, sessionKeyTableName)
return nil
}