app_session.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model // import "miniflux.app/model"
  5. import (
  6. "database/sql/driver"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. )
  11. // SessionData represents the data attached to the session.
  12. type SessionData struct {
  13. CSRF string `json:"csrf"`
  14. OAuth2State string `json:"oauth2_state"`
  15. FlashMessage string `json:"flash_message"`
  16. FlashErrorMessage string `json:"flash_error_message"`
  17. Language string `json:"language"`
  18. Theme string `json:"theme"`
  19. PocketRequestToken string `json:"pocket_request_token"`
  20. }
  21. func (s SessionData) String() string {
  22. return fmt.Sprintf(`CSRF=%q, OAuth2State=%q, FlashMsg=%q, FlashErrMsg=%q, Lang=%q, Theme=%q, PocketTkn=%q`,
  23. s.CSRF, s.OAuth2State, s.FlashMessage, s.FlashErrorMessage, s.Language, s.Theme, s.PocketRequestToken)
  24. }
  25. // Value converts the session data to JSON.
  26. func (s SessionData) Value() (driver.Value, error) {
  27. j, err := json.Marshal(s)
  28. return j, err
  29. }
  30. // Scan converts raw JSON data.
  31. func (s *SessionData) Scan(src interface{}) error {
  32. source, ok := src.([]byte)
  33. if !ok {
  34. return errors.New("session: unable to assert type of src")
  35. }
  36. err := json.Unmarshal(source, s)
  37. if err != nil {
  38. return fmt.Errorf("session: %v", err)
  39. }
  40. return err
  41. }
  42. // Session represents a session in the system.
  43. type Session struct {
  44. ID string
  45. Data *SessionData
  46. }
  47. func (s *Session) String() string {
  48. return fmt.Sprintf(`ID="%s", Data={%v}`, s.ID, s.Data)
  49. }