forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
96 lines (79 loc) · 2.83 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package influxdb
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"
// RenewSessionTime is the the time to extend session, currently set to 5min.
var RenewSessionTime = time.Duration(time.Second * 300)
// DefaultSessionLength is the default session length on initial creation.
var DefaultSessionLength = time.Hour
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"
// OpRenewSession = "RenewSession"
OpRenewSession = "RenewSession"
)
// SessionAuthorizionKind defines the type of authorizer
const SessionAuthorizionKind = "session"
// Session is a user session.
type Session struct {
// ID is only required for auditing purposes.
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) {
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.
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 }
// 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,
}
}
// 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)
RenewSession(ctx context.Context, session *Session, newExpiration time.Time) error
}