A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

129 lines
2.8 KiB

  1. package writefreely
  2. import (
  3. "encoding/gob"
  4. "github.com/gorilla/sessions"
  5. "github.com/writeas/web-core/log"
  6. "net/http"
  7. "strings"
  8. )
  9. const (
  10. day = 86400
  11. sessionLength = 180 * day
  12. cookieName = "wfu"
  13. cookieUserVal = "u"
  14. blogPassCookieName = "ub"
  15. )
  16. // initSession creates the cookie store. It depends on the keychain already
  17. // being loaded.
  18. func initSession(app *app) *sessions.CookieStore {
  19. // Register complex data types we'll be storing in cookies
  20. gob.Register(&User{})
  21. // Create the cookie store
  22. store := sessions.NewCookieStore(app.keys.cookieAuthKey, app.keys.cookieKey)
  23. store.Options = &sessions.Options{
  24. Path: "/",
  25. MaxAge: sessionLength,
  26. HttpOnly: true,
  27. Secure: strings.HasPrefix(app.cfg.App.Host, "https://"),
  28. }
  29. return store
  30. }
  31. func getSessionFlashes(app *app, w http.ResponseWriter, r *http.Request, session *sessions.Session) ([]string, error) {
  32. var err error
  33. if session == nil {
  34. session, err = app.sessionStore.Get(r, cookieName)
  35. if err != nil {
  36. return nil, err
  37. }
  38. }
  39. f := []string{}
  40. if flashes := session.Flashes(); len(flashes) > 0 {
  41. for _, flash := range flashes {
  42. if str, ok := flash.(string); ok {
  43. f = append(f, str)
  44. }
  45. }
  46. }
  47. saveUserSession(app, r, w)
  48. return f, nil
  49. }
  50. func addSessionFlash(app *app, w http.ResponseWriter, r *http.Request, m string, session *sessions.Session) error {
  51. var err error
  52. if session == nil {
  53. session, err = app.sessionStore.Get(r, cookieName)
  54. }
  55. if err != nil {
  56. log.Error("Unable to add flash '%s': %v", m, err)
  57. return err
  58. }
  59. session.AddFlash(m)
  60. saveUserSession(app, r, w)
  61. return nil
  62. }
  63. func getUserAndSession(app *app, r *http.Request) (*User, *sessions.Session) {
  64. session, err := app.sessionStore.Get(r, cookieName)
  65. if err == nil {
  66. // Got the currently logged-in user
  67. val := session.Values[cookieUserVal]
  68. var u = &User{}
  69. var ok bool
  70. if u, ok = val.(*User); ok {
  71. return u, session
  72. }
  73. }
  74. return nil, nil
  75. }
  76. func getUserSession(app *app, r *http.Request) *User {
  77. u, _ := getUserAndSession(app, r)
  78. return u
  79. }
  80. func saveUserSession(app *app, r *http.Request, w http.ResponseWriter) error {
  81. session, err := app.sessionStore.Get(r, cookieName)
  82. if err != nil {
  83. return ErrInternalCookieSession
  84. }
  85. // Extend the session
  86. session.Options.MaxAge = int(sessionLength)
  87. // Remove any information that accidentally got added
  88. // FIXME: find where Plan information is getting saved to cookie.
  89. val := session.Values[cookieUserVal]
  90. var u = &User{}
  91. var ok bool
  92. if u, ok = val.(*User); ok {
  93. session.Values[cookieUserVal] = u.Cookie()
  94. }
  95. err = session.Save(r, w)
  96. if err != nil {
  97. log.Error("Couldn't saveUserSession: %v", err)
  98. }
  99. return err
  100. }
  101. func getFullUserSession(app *app, r *http.Request) *User {
  102. u := getUserSession(app, r)
  103. if u == nil {
  104. return nil
  105. }
  106. u, _ = app.db.GetUserByID(u.ID)
  107. return u
  108. }