session.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 language field in session.
  46. func (s *Session) SetLanguage(language string) {
  47. s.store.UpdateSessionField(s.ctx.SessionID(), "language", language)
  48. }
  49. // SetPocketRequestToken updates Pocket Request Token.
  50. func (s *Session) SetPocketRequestToken(requestToken string) {
  51. s.store.UpdateSessionField(s.ctx.SessionID(), "pocket_request_token", requestToken)
  52. }
  53. // New returns a new session handler.
  54. func New(store *storage.Storage, ctx *context.Context) *Session {
  55. return &Session{store, ctx}
  56. }