session.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 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 session
  5. import (
  6. "github.com/miniflux/miniflux/crypto"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/storage"
  9. )
  10. // Session handles session data.
  11. type Session struct {
  12. store *storage.Storage
  13. ctx *context.Context
  14. }
  15. // NewOAuth2State generates a new OAuth2 state and stores the value into the database.
  16. func (s *Session) NewOAuth2State() string {
  17. state := crypto.GenerateRandomString(32)
  18. s.store.UpdateSessionField(s.ctx.SessionID(), "oauth2_state", state)
  19. return state
  20. }
  21. // NewFlashMessage creates a new flash message.
  22. func (s *Session) NewFlashMessage(message string) {
  23. s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", message)
  24. }
  25. // FlashMessage returns the current flash message if any.
  26. func (s *Session) FlashMessage() string {
  27. message := s.ctx.FlashMessage()
  28. if message != "" {
  29. s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", "")
  30. }
  31. return message
  32. }
  33. // NewFlashErrorMessage creates a new flash error message.
  34. func (s *Session) NewFlashErrorMessage(message string) {
  35. s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", message)
  36. }
  37. // FlashErrorMessage returns the last flash error message if any.
  38. func (s *Session) FlashErrorMessage() string {
  39. message := s.ctx.FlashErrorMessage()
  40. if message != "" {
  41. s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", "")
  42. }
  43. return message
  44. }
  45. // SetLanguage updates the language field in session.
  46. func (s *Session) SetLanguage(language string) {
  47. s.store.UpdateSessionField(s.ctx.SessionID(), "language", language)
  48. }
  49. // SetTheme updates the theme field in session.
  50. func (s *Session) SetTheme(theme string) {
  51. s.store.UpdateSessionField(s.ctx.SessionID(), "theme", theme)
  52. }
  53. // SetPocketRequestToken updates Pocket Request Token.
  54. func (s *Session) SetPocketRequestToken(requestToken string) {
  55. s.store.UpdateSessionField(s.ctx.SessionID(), "pocket_request_token", requestToken)
  56. }
  57. // New returns a new session handler.
  58. func New(store *storage.Storage, ctx *context.Context) *Session {
  59. return &Session{store, ctx}
  60. }