influxdb/session.go

94 lines
2.7 KiB
Go
Raw Normal View History

package influxdb
2018-09-25 19:13:38 +00:00
import (
"context"
"time"
)
// ErrSessionNotFound is the error messages for a missing sessions.
const ErrSessionNotFound = "session not found"
// ErrSessionExpired is the error message for expired sessions.
const ErrSessionExpired = "session has expired"
2019-01-11 22:07:50 +00:00
// RenewSessionTime is the the time to extend session, currently set to 5min.
var RenewSessionTime = time.Duration(time.Second * 300)
var (
// OpFindSession represents the operation that looks for sessions.
OpFindSession = "FindSession"
// OpExpireSession represents the operation that expires sessions.
OpExpireSession = "ExpireSession"
// OpCreateSession represents the operation that creates a session for a given user.
OpCreateSession = "CreateSession"
2019-01-11 22:07:50 +00:00
// OpRenewSession = "RenewSession"
OpRenewSession = "RenewSession"
)
// SessionAuthorizionKind defines the type of authorizer
2019-02-14 21:47:16 +00:00
const SessionAuthorizionKind = "session"
2018-09-25 19:13:38 +00:00
// Session is a user session.
type Session struct {
2018-09-26 18:17:03 +00:00
// ID is only required for auditing purposes.
2018-09-25 19:13:38 +00:00
ID ID `json:"id"`
Key string `json:"key"`
CreatedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
UserID ID `json:"userID,omitempty"`
Permissions []Permission `json:"permissions,omitempty"`
}
// Expired returns an error if the session is expired.
func (s *Session) Expired() error {
if time.Now().After(s.ExpiresAt) {
2019-01-11 22:07:50 +00:00
return &Error{
Code: EForbidden,
Msg: ErrSessionExpired,
}
}
return nil
}
// Allowed returns true if the authorization is unexpired and request permission
// exists in the sessions list of permissions.
func (s *Session) Allowed(p Permission) bool {
if err := s.Expired(); err != nil {
return false
}
return PermissionAllowed(p, s.Permissions)
}
// Kind returns session and is used for auditing.
2019-02-14 21:47:16 +00:00
func (s *Session) Kind() string { return SessionAuthorizionKind }
// Identifier returns the sessions ID and is used for auditing.
func (s *Session) Identifier() ID { return s.ID }
2018-10-24 15:13:30 +00:00
// GetUserID returns the user id.
func (s *Session) GetUserID() ID {
return s.UserID
}
// EphemeralAuth generates an Authorization that is not stored
// but at the user's max privs.
func (s *Session) EphemeralAuth(orgID ID) *Authorization {
return &Authorization{
ID: s.ID,
OrgID: orgID,
Status: Active,
UserID: s.UserID,
Permissions: s.Permissions,
}
}
2018-09-25 19:13:38 +00:00
// SessionService represents a service for managing user sessions.
type SessionService interface {
FindSession(ctx context.Context, key string) (*Session, error)
ExpireSession(ctx context.Context, key string) error
CreateSession(ctx context.Context, user string) (*Session, error)
2019-01-11 22:07:50 +00:00
RenewSession(ctx context.Context, session *Session, newExpiration time.Time) error
2018-09-25 19:13:38 +00:00
}