/
session.go
63 lines (56 loc) · 2 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
/*
* OAuth Module Session Definitions
* This session object is used internally to transfer user and expiry information to the storage providers
*
* AuthPlz Project (https://github.com/authplz/authplz-core)
* Copyright 2017 Ryan Kurte
*/
package oauth
import (
"bytes"
"encoding/gob"
"time"
)
// Session is an OAuth session for module use
// Relevant data is persisted with each grant type object and returned using a similar object
// meeting the UserSession interface from the datastore
type Session struct {
UserID string
Username string
Subject string
AccessExpiry time.Time
RefreshExpiry time.Time
AuthorizeExpiry time.Time
IDExpiry time.Time
}
// NewSession creates a new default session instance for a given user
func NewSession(userID, username string) *Session {
return &Session{
UserID: userID,
Username: username,
AccessExpiry: time.Time{},
RefreshExpiry: time.Time{},
AuthorizeExpiry: time.Time{},
IDExpiry: time.Time{},
}
}
func (s *Session) GetUserID() string { return s.UserID }
func (s *Session) GetUsername() string { return s.Username }
func (s *Session) GetSubject() string { return s.Subject }
func (s *Session) SetAccessExpiry(t time.Time) { s.AccessExpiry = t }
func (s *Session) GetAccessExpiry() time.Time { return s.AccessExpiry }
func (s *Session) SetRefreshExpiry(t time.Time) { s.RefreshExpiry = t }
func (s *Session) GetRefreshExpiry() time.Time { return s.RefreshExpiry }
func (s *Session) SetAuthorizeExpiry(t time.Time) { s.AuthorizeExpiry = t }
func (s *Session) GetAuthorizeExpiry() time.Time { return s.AuthorizeExpiry }
func (s *Session) SetIDExpiry(t time.Time) { s.IDExpiry = t }
func (s *Session) GetIDExpiry() time.Time { return s.IDExpiry }
func (s *Session) Clone() interface{} {
clone := Session{}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
_ = enc.Encode(s)
_ = dec.Decode(&clone)
return &clone
}