2014-06-14 20:15:10 +04:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2016-02-18 12:08:51 +03:00
|
|
|
"github.com/admpub/boltstore/shared/protobuf"
|
2015-09-16 14:48:02 +03:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2014-06-14 20:15:10 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Session converts the byte slice to the session struct value.
|
|
|
|
func Session(data []byte) (protobuf.Session, error) {
|
|
|
|
session := protobuf.Session{}
|
|
|
|
err := proto.Unmarshal(data, &session)
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expired checks if the session is expired.
|
|
|
|
func Expired(session protobuf.Session) bool {
|
|
|
|
return *session.ExpiresAt > 0 && *session.ExpiresAt <= time.Now().Unix()
|
|
|
|
}
|
2014-06-17 12:53:24 +04:00
|
|
|
|
|
|
|
// NewSession creates and returns a session data.
|
|
|
|
func NewSession(values []byte, maxAge int) *protobuf.Session {
|
|
|
|
expiresAt := time.Now().Unix() + int64(maxAge)
|
|
|
|
return &protobuf.Session{Values: values, ExpiresAt: &expiresAt}
|
|
|
|
}
|