app_session.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. PocketRequestToken string `json:"pocket_request_token"`
  20. LastForceRefresh string `json:"last_force_refresh"`
  21. WebAuthnSessionData WebAuthnSession `json:"webauthn_session_data"`
  22. }
  23. func (s SessionData) String() string {
  24. return fmt.Sprintf(`CSRF=%q, OAuth2State=%q, OAuth2CodeVerifier=%q, FlashMsg=%q, FlashErrMsg=%q, Lang=%q, Theme=%q, PocketTkn=%q, LastForceRefresh=%s, WebAuthnSession=%q`,
  25. s.CSRF,
  26. s.OAuth2State,
  27. s.OAuth2CodeVerifier,
  28. s.FlashMessage,
  29. s.FlashErrorMessage,
  30. s.Language,
  31. s.Theme,
  32. s.PocketRequestToken,
  33. s.LastForceRefresh,
  34. s.WebAuthnSessionData,
  35. )
  36. }
  37. // Value converts the session data to JSON.
  38. func (s SessionData) Value() (driver.Value, error) {
  39. j, err := json.Marshal(s)
  40. return j, err
  41. }
  42. // Scan converts raw JSON data.
  43. func (s *SessionData) Scan(src interface{}) error {
  44. source, ok := src.([]byte)
  45. if !ok {
  46. return errors.New("session: unable to assert type of src")
  47. }
  48. err := json.Unmarshal(source, s)
  49. if err != nil {
  50. return fmt.Errorf("session: %v", err)
  51. }
  52. return err
  53. }
  54. // Session represents a session in the system.
  55. type Session struct {
  56. ID string
  57. Data *SessionData
  58. }
  59. func (s *Session) String() string {
  60. return fmt.Sprintf(`ID=%q, Data={%v}`, s.ID, s.Data)
  61. }