@@ -9,6 +9,7 @@ import (
9 "github.com/pusher/oauth2_proxy/cookie"
9 "github.com/pusher/oauth2_proxy/cookie"
12+// SessionState is used to store information about the currently authenticated user session
12type SessionState struct {
13type SessionState struct {
13 AccessToken string
14 AccessToken string
14 ExpiresOn time.Time
15 ExpiresOn time.Time
@@ -17,6 +18,7 @@ type SessionState struct {
17 User string
18 User string
21+// IsExpired checks whether the session has expired
20func (s *SessionState) IsExpired() bool {
22func (s *SessionState) IsExpired() bool {
21 if !s.ExpiresOn.IsZero() && s.ExpiresOn.Before(time.Now()) {
23 if !s.ExpiresOn.IsZero() && s.ExpiresOn.Before(time.Now()) {
22 return true
24 return true
@@ -24,6 +26,7 @@ func (s *SessionState) IsExpired() bool {
24 return false
26 return false
29+// String constructs a summary of the session state
27func (s *SessionState) String() string {
30func (s *SessionState) String() string {
28 o := fmt.Sprintf("Session{%s", s.accountInfo())
31 o := fmt.Sprintf("Session{%s", s.accountInfo())
29 if s.AccessToken != "" {
32 if s.AccessToken != "" {
@@ -38,6 +41,7 @@ func (s *SessionState) String() string {
38 return o + "}"
41 return o + "}"
44+// EncodeSessionState returns string representation of the current session
41func (s *SessionState) EncodeSessionState(c *cookie.Cipher) (string, error) {
45func (s *SessionState) EncodeSessionState(c *cookie.Cipher) (string, error) {
42 if c == nil || s.AccessToken == "" {
46 if c == nil || s.AccessToken == "" {
43 return s.accountInfo(), nil
47 return s.accountInfo(), nil
@@ -49,6 +53,7 @@ func (s *SessionState) accountInfo() string {
49 return fmt.Sprintf("email:%s user:%s", s.Email, s.User)
53 return fmt.Sprintf("email:%s user:%s", s.Email, s.User)
56+// EncryptedString encrypts the session state into a cookie string
52func (s *SessionState) EncryptedString(c *cookie.Cipher) (string, error) {
57func (s *SessionState) EncryptedString(c *cookie.Cipher) (string, error) {
53 var err error
58 var err error
54 if c == nil {
59 if c == nil {
@@ -84,6 +89,7 @@ func decodeSessionStatePlain(v string) (s *SessionState, err error) {
84 return &SessionState{User: user, Email: email}, nil
89 return &SessionState{User: user, Email: email}, nil
92+// DecodeSessionState decodes the session cookie string into a SessionState
87func DecodeSessionState(v string, c *cookie.Cipher) (s *SessionState, err error) {
93func DecodeSessionState(v string, c *cookie.Cipher) (s *SessionState, err error) {
88 if c == nil {
94 if c == nil {
89 return decodeSessionStatePlain(v)
95 return decodeSessionStatePlain(v)