app_session.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/v2/internal/model"
  4. import (
  5. "database/sql/driver"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. )
  10. // SessionData represents the data attached to the session.
  11. type SessionData struct {
  12. CSRF string `json:"csrf"`
  13. OAuth2State string `json:"oauth2_state"`
  14. OAuth2CodeVerifier string `json:"oauth2_code_verifier"`
  15. FlashMessage string `json:"flash_message"`
  16. FlashErrorMessage string `json:"flash_error_message"`
  17. Language string `json:"language"`
  18. Theme string `json:"theme"`
  19. LastForceRefresh string `json:"last_force_refresh"`
  20. WebAuthnSessionData WebAuthnSession `json:"webauthn_session_data"`
  21. }
  22. func (s *SessionData) String() string {
  23. return fmt.Sprintf(`CSRF=%q, OAuth2State=%q, OAuth2CodeVerifier=%q, FlashMsg=%q, FlashErrMsg=%q, Lang=%q, Theme=%q, LastForceRefresh=%s, WebAuthnSession=%q`,
  24. s.CSRF,
  25. s.OAuth2State,
  26. s.OAuth2CodeVerifier,
  27. s.FlashMessage,
  28. s.FlashErrorMessage,
  29. s.Language,
  30. s.Theme,
  31. s.LastForceRefresh,
  32. s.WebAuthnSessionData,
  33. )
  34. }
  35. // Value converts the session data to JSON.
  36. func (s *SessionData) Value() (driver.Value, error) {
  37. j, err := json.Marshal(s)
  38. return j, err
  39. }
  40. // Scan converts raw JSON data.
  41. func (s *SessionData) Scan(src any) error {
  42. source, ok := src.([]byte)
  43. if !ok {
  44. return errors.New("session: unable to assert type of src")
  45. }
  46. err := json.Unmarshal(source, s)
  47. if err != nil {
  48. return fmt.Errorf("session: %v", err)
  49. }
  50. return err
  51. }
  52. // Session represents a session in the system.
  53. type Session struct {
  54. ID string
  55. Data *SessionData
  56. }
  57. func (s *Session) String() string {
  58. return fmt.Sprintf(`ID=%q, Data={%v}`, s.ID, s.Data)
  59. }