2019-05-05 12:33:13 +00:00
|
|
|
package sessions
|
2015-06-23 11:23:39 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-20 13:59:24 +00:00
|
|
|
"encoding/json"
|
2015-06-23 11:23:39 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-11-27 11:45:05 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/cookie"
|
2015-06-23 11:23:39 +00:00
|
|
|
)
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// SessionState is used to store information about the currently authenticated user session
|
2015-06-23 11:23:39 +00:00
|
|
|
type SessionState struct {
|
2019-03-20 13:59:24 +00:00
|
|
|
AccessToken string `json:",omitempty"`
|
|
|
|
IDToken string `json:",omitempty"`
|
2019-05-07 14:32:46 +00:00
|
|
|
CreatedAt time.Time `json:"-"`
|
2019-03-20 13:59:24 +00:00
|
|
|
ExpiresOn time.Time `json:"-"`
|
|
|
|
RefreshToken string `json:",omitempty"`
|
|
|
|
Email string `json:",omitempty"`
|
|
|
|
User string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionStateJSON is used to encode SessionState into JSON without exposing time.Time zero value
|
|
|
|
type SessionStateJSON struct {
|
|
|
|
*SessionState
|
2019-05-07 14:32:46 +00:00
|
|
|
CreatedAt *time.Time `json:",omitempty"`
|
2019-03-20 13:59:24 +00:00
|
|
|
ExpiresOn *time.Time `json:",omitempty"`
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// IsExpired checks whether the session has expired
|
2015-06-23 11:23:39 +00:00
|
|
|
func (s *SessionState) IsExpired() bool {
|
|
|
|
if !s.ExpiresOn.IsZero() && s.ExpiresOn.Before(time.Now()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:32:46 +00:00
|
|
|
// Age returns the age of a session
|
|
|
|
func (s *SessionState) Age() time.Duration {
|
|
|
|
if !s.CreatedAt.IsZero() {
|
|
|
|
return time.Now().Truncate(time.Second).Sub(s.CreatedAt)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// String constructs a summary of the session state
|
2015-06-23 11:23:39 +00:00
|
|
|
func (s *SessionState) String() string {
|
2019-03-20 13:59:24 +00:00
|
|
|
o := fmt.Sprintf("Session{email:%s user:%s", s.Email, s.User)
|
2015-06-23 11:23:39 +00:00
|
|
|
if s.AccessToken != "" {
|
|
|
|
o += " token:true"
|
|
|
|
}
|
2018-01-27 10:53:17 +00:00
|
|
|
if s.IDToken != "" {
|
|
|
|
o += " id_token:true"
|
|
|
|
}
|
2019-05-07 14:32:46 +00:00
|
|
|
if !s.CreatedAt.IsZero() {
|
|
|
|
o += fmt.Sprintf(" created:%s", s.CreatedAt)
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
if !s.ExpiresOn.IsZero() {
|
|
|
|
o += fmt.Sprintf(" expires:%s", s.ExpiresOn)
|
|
|
|
}
|
|
|
|
if s.RefreshToken != "" {
|
|
|
|
o += " refresh_token:true"
|
|
|
|
}
|
|
|
|
return o + "}"
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:37:59 +00:00
|
|
|
// EncodeSessionState returns string representation of the current session
|
2015-06-23 11:23:39 +00:00
|
|
|
func (s *SessionState) EncodeSessionState(c *cookie.Cipher) (string, error) {
|
2019-03-20 13:59:24 +00:00
|
|
|
var ss SessionState
|
2015-06-23 11:23:39 +00:00
|
|
|
if c == nil {
|
2019-03-20 13:59:24 +00:00
|
|
|
// Store only Email and User when cipher is unavailable
|
|
|
|
ss.Email = s.Email
|
|
|
|
ss.User = s.User
|
|
|
|
} else {
|
|
|
|
ss = *s
|
|
|
|
var err error
|
2019-04-09 11:55:33 +00:00
|
|
|
if ss.Email != "" {
|
|
|
|
ss.Email, err = c.Encrypt(ss.Email)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ss.User != "" {
|
|
|
|
ss.User, err = c.Encrypt(ss.User)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if ss.AccessToken != "" {
|
|
|
|
ss.AccessToken, err = c.Encrypt(ss.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if ss.IDToken != "" {
|
|
|
|
ss.IDToken, err = c.Encrypt(ss.IDToken)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-01-27 10:53:17 +00:00
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if ss.RefreshToken != "" {
|
|
|
|
ss.RefreshToken, err = c.Encrypt(ss.RefreshToken)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
// Embed SessionState and ExpiresOn pointer into SessionStateJSON
|
|
|
|
ssj := &SessionStateJSON{SessionState: &ss}
|
2019-05-07 14:32:46 +00:00
|
|
|
if !ss.CreatedAt.IsZero() {
|
|
|
|
ssj.CreatedAt = &ss.CreatedAt
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if !ss.ExpiresOn.IsZero() {
|
|
|
|
ssj.ExpiresOn = &ss.ExpiresOn
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(ssj)
|
|
|
|
return string(b), err
|
2017-09-26 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
// legacyDecodeSessionStatePlain decodes older plain session state string
|
|
|
|
func legacyDecodeSessionStatePlain(v string) (*SessionState, error) {
|
2017-09-26 21:31:27 +00:00
|
|
|
chunks := strings.Split(v, " ")
|
|
|
|
if len(chunks) != 2 {
|
2019-03-20 13:59:24 +00:00
|
|
|
return nil, fmt.Errorf("invalid session state (legacy: expected 2 chunks for user/email got %d)", len(chunks))
|
2017-09-26 21:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := strings.TrimPrefix(chunks[1], "user:")
|
2019-03-20 13:59:24 +00:00
|
|
|
email := strings.TrimPrefix(chunks[0], "email:")
|
2017-09-26 21:31:27 +00:00
|
|
|
|
|
|
|
return &SessionState{User: user, Email: email}, nil
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
// legacyDecodeSessionState attempts to decode the session state string
|
|
|
|
// generated by v3.1.0 or older
|
|
|
|
func legacyDecodeSessionState(v string, c *cookie.Cipher) (*SessionState, error) {
|
|
|
|
chunks := strings.Split(v, "|")
|
|
|
|
|
2017-09-26 21:31:27 +00:00
|
|
|
if c == nil {
|
2019-03-20 13:59:24 +00:00
|
|
|
if len(chunks) != 1 {
|
|
|
|
return nil, fmt.Errorf("invalid session state (legacy: expected 1 chunk for plain got %d)", len(chunks))
|
|
|
|
}
|
|
|
|
return legacyDecodeSessionStatePlain(chunks[0])
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
if len(chunks) != 4 && len(chunks) != 5 {
|
|
|
|
return nil, fmt.Errorf("invalid session state (legacy: expected 4 or 5 chunks for full got %d)", len(chunks))
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
i := 0
|
|
|
|
ss, err := legacyDecodeSessionStatePlain(chunks[i])
|
2017-09-26 21:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
i++
|
|
|
|
ss.AccessToken = chunks[i]
|
|
|
|
|
|
|
|
if len(chunks) == 5 {
|
|
|
|
// SessionState with IDToken in v3.1.0
|
|
|
|
i++
|
|
|
|
ss.IDToken = chunks[i]
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|
2017-09-26 21:31:27 +00:00
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
i++
|
|
|
|
ts, err := strconv.Atoi(chunks[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid session state (legacy: wrong expiration time: %s)", err)
|
2018-01-27 10:53:17 +00:00
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
ss.ExpiresOn = time.Unix(int64(ts), 0)
|
|
|
|
|
|
|
|
i++
|
|
|
|
ss.RefreshToken = chunks[i]
|
2018-01-27 10:53:17 +00:00
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
return ss, nil
|
|
|
|
}
|
2017-09-26 21:31:27 +00:00
|
|
|
|
2019-03-20 13:59:24 +00:00
|
|
|
// DecodeSessionState decodes the session cookie string into a SessionState
|
|
|
|
func DecodeSessionState(v string, c *cookie.Cipher) (*SessionState, error) {
|
|
|
|
var ssj SessionStateJSON
|
|
|
|
var ss *SessionState
|
|
|
|
err := json.Unmarshal([]byte(v), &ssj)
|
|
|
|
if err == nil && ssj.SessionState != nil {
|
2019-05-07 14:32:46 +00:00
|
|
|
// Extract SessionState and CreatedAt,ExpiresOn value from SessionStateJSON
|
2019-03-20 13:59:24 +00:00
|
|
|
ss = ssj.SessionState
|
2019-05-07 14:32:46 +00:00
|
|
|
if ssj.CreatedAt != nil {
|
|
|
|
ss.CreatedAt = *ssj.CreatedAt
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if ssj.ExpiresOn != nil {
|
|
|
|
ss.ExpiresOn = *ssj.ExpiresOn
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Try to decode a legacy string when json.Unmarshal failed
|
|
|
|
ss, err = legacyDecodeSessionState(v, c)
|
|
|
|
if err != nil {
|
2015-06-23 11:23:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if c == nil {
|
|
|
|
// Load only Email and User when cipher is unavailable
|
|
|
|
ss = &SessionState{
|
|
|
|
Email: ss.Email,
|
|
|
|
User: ss.User,
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-09 11:55:33 +00:00
|
|
|
// Backward compatibility with using unecrypted Email
|
|
|
|
if ss.Email != "" {
|
2019-04-09 12:17:40 +00:00
|
|
|
decryptedEmail, errEmail := c.Decrypt(ss.Email)
|
|
|
|
if errEmail == nil {
|
2019-04-09 11:55:33 +00:00
|
|
|
ss.Email = decryptedEmail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Backward compatibility with using unecrypted User
|
|
|
|
if ss.User != "" {
|
2019-04-09 12:17:40 +00:00
|
|
|
decryptedUser, errUser := c.Decrypt(ss.User)
|
|
|
|
if errUser == nil {
|
2019-04-09 11:55:33 +00:00
|
|
|
ss.User = decryptedUser
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 13:59:24 +00:00
|
|
|
if ss.AccessToken != "" {
|
|
|
|
ss.AccessToken, err = c.Decrypt(ss.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ss.IDToken != "" {
|
|
|
|
ss.IDToken, err = c.Decrypt(ss.IDToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ss.RefreshToken != "" {
|
|
|
|
ss.RefreshToken, err = c.Decrypt(ss.RefreshToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ss.User == "" {
|
2019-05-07 09:36:00 +00:00
|
|
|
ss.User = ss.Email
|
2019-03-20 13:59:24 +00:00
|
|
|
}
|
|
|
|
return ss, nil
|
2015-06-23 11:23:39 +00:00
|
|
|
}
|