1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-01-28 05:36:20 +02:00
oauth2-proxy/pkg/apis/sessions/session_state.go

134 lines
3.3 KiB
Go
Raw Normal View History

2019-05-05 13:33:13 +01:00
package sessions
import (
"encoding/json"
"fmt"
"time"
2020-03-29 14:54:36 +01:00
"github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
)
// SessionState is used to store information about the currently authenticated user session
type SessionState struct {
AccessToken string `json:",omitempty"`
IDToken string `json:",omitempty"`
CreatedAt *time.Time `json:",omitempty"`
ExpiresOn *time.Time `json:",omitempty"`
RefreshToken string `json:",omitempty"`
Email string `json:",omitempty"`
User string `json:",omitempty"`
PreferredUsername string `json:",omitempty"`
}
// IsExpired checks whether the session has expired
func (s *SessionState) IsExpired() bool {
if s.ExpiresOn != nil && !s.ExpiresOn.IsZero() && s.ExpiresOn.Before(time.Now()) {
return true
}
return false
}
2019-05-07 15:32:46 +01:00
// Age returns the age of a session
func (s *SessionState) Age() time.Duration {
if s.CreatedAt != nil && !s.CreatedAt.IsZero() {
return time.Now().Truncate(time.Second).Sub(*s.CreatedAt)
2019-05-07 15:32:46 +01:00
}
return 0
}
// String constructs a summary of the session state
func (s *SessionState) String() string {
o := fmt.Sprintf("Session{email:%s user:%s PreferredUsername:%s", s.Email, s.User, s.PreferredUsername)
if s.AccessToken != "" {
o += " token:true"
}
2018-01-27 10:53:17 +00:00
if s.IDToken != "" {
o += " id_token:true"
}
2019-05-07 15:32:46 +01:00
if !s.CreatedAt.IsZero() {
o += fmt.Sprintf(" created:%s", s.CreatedAt)
}
if !s.ExpiresOn.IsZero() {
o += fmt.Sprintf(" expires:%s", s.ExpiresOn)
}
if s.RefreshToken != "" {
o += " refresh_token:true"
}
return o + "}"
}
// EncodeSessionState returns string representation of the current session
2019-05-24 17:06:48 +01:00
func (s *SessionState) EncodeSessionState(c *encryption.Cipher) (string, error) {
var ss SessionState
if c == nil {
// Store only Email and User when cipher is unavailable
ss.Email = s.Email
ss.User = s.User
ss.PreferredUsername = s.PreferredUsername
} else {
ss = *s
for _, s := range []*string{
&ss.Email,
&ss.User,
&ss.PreferredUsername,
&ss.AccessToken,
&ss.IDToken,
&ss.RefreshToken,
} {
err := c.EncryptInto(s)
if err != nil {
return "", err
}
}
}
b, err := json.Marshal(ss)
return string(b), err
}
// DecodeSessionState decodes the session cookie string into a SessionState
2019-05-24 17:06:48 +01:00
func DecodeSessionState(v string, c *encryption.Cipher) (*SessionState, error) {
var ss SessionState
err := json.Unmarshal([]byte(v), &ss)
if err != nil {
return nil, fmt.Errorf("error unmarshalling session: %w", err)
}
if c == nil {
// Load only Email and User when cipher is unavailable
ss = SessionState{
Email: ss.Email,
User: ss.User,
PreferredUsername: ss.PreferredUsername,
}
} else {
2019-05-08 12:35:15 -07:00
// Backward compatibility with using unencrypted Email
2019-04-09 14:55:33 +03:00
if ss.Email != "" {
2019-04-09 15:17:40 +03:00
decryptedEmail, errEmail := c.Decrypt(ss.Email)
if errEmail == nil {
2019-04-09 14:55:33 +03:00
ss.Email = decryptedEmail
}
}
2019-05-08 12:35:15 -07:00
// Backward compatibility with using unencrypted User
2019-04-09 14:55:33 +03:00
if ss.User != "" {
2019-04-09 15:17:40 +03:00
decryptedUser, errUser := c.Decrypt(ss.User)
if errUser == nil {
2019-04-09 14:55:33 +03:00
ss.User = decryptedUser
}
}
for _, s := range []*string{
&ss.PreferredUsername,
&ss.AccessToken,
&ss.IDToken,
&ss.RefreshToken,
} {
err := c.DecryptInto(s)
if err != nil {
return nil, err
}
}
}
return &ss, nil
}