A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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