func logout(sess context.Session) (resp responseError) { resp = newResponse() if sess != nil { sess.DeleteAll() if err := sess.Write(nil); err != nil { resp.err = fmt.Errorf("Error writing session data: %v", err) } } return }
func getAuthData(user content.User, sess context.Session, capabilities capabilities) (resp responseError) { resp = newResponse() if sess != nil { sess.Set(readeef.AuthNameKey, user.Data().Login) if err := sess.Write(nil); err != nil { resp.err = fmt.Errorf("Error writing session data: %v", err) } } resp.val["Auth"] = true resp.val["Capabilities"] = capabilities resp.val["User"] = user return }
func (smw Session) Handler(ph http.Handler, c context.Context) http.Handler { var abspath string var maxAge, cleanupInterval, cleanupMaxAge time.Duration if filepath.IsAbs(smw.Path) { abspath = smw.Path } else { var err error abspath, err = filepath.Abs(path.Join(filepath.Dir(os.Args[0]), smw.Path)) if err != nil { panic(err) } } if smw.MaxAge != "" { var err error maxAge, err = time.ParseDuration(smw.MaxAge) if err != nil { panic(err) } } logger := webfw.GetLogger(c) if smw.CleanupInterval != "" { var err error cleanupInterval, err = time.ParseDuration(smw.CleanupInterval) if err != nil { panic(err) } cleanupMaxAge, err = time.ParseDuration(smw.CleanupMaxAge) if err != nil { panic(err) } go func() { for _ = range time.Tick(cleanupInterval) { logger.Print("Cleaning up old sessions") if err := context.CleanupSessions(abspath, cleanupMaxAge); err != nil { logger.Printf("Failed to clean up sessions: %v", err) } } }() } handler := func(w http.ResponseWriter, r *http.Request) { uriParts := strings.SplitN(r.RequestURI, "?", 2) if uriParts[0] == "" { uriParts[0] = r.URL.Path } ignore := false for _, prefix := range smw.IgnoreURLPrefix { if prefix[0] == '/' { prefix = prefix[1:] } if strings.HasPrefix(uriParts[0], smw.Pattern+prefix+"/") { ignore = true break } if uriParts[0] == smw.Pattern+prefix { ignore = true break } } if ignore { ph.ServeHTTP(w, r) return } firstTimer := false var sess context.Session if smw.SessionGenerator == nil { sess = context.NewSession(smw.Secret, smw.Cipher, abspath) } else { sess = smw.SessionGenerator(smw.Secret, smw.Cipher, abspath) } sess.SetMaxAge(maxAge) err := sess.Read(r, c) if err != nil && err != context.ErrExpired && err != context.ErrNotExist { sess.SetName(util.UUID()) firstTimer = true if err != context.ErrCookieNotExist { logger.Printf("Error reading session: %v", err) } } c.Set(r, context.BaseCtxKey("session"), sess) c.Set(r, context.BaseCtxKey("firstTimer"), firstTimer) rec := util.NewRecorderHijacker(w) ph.ServeHTTP(rec, r) for k, v := range rec.Header() { w.Header()[k] = v } if sess != nil { if err := sess.Write(w); err != nil { logger.Printf("Unable to write session: %v", err) } } w.WriteHeader(rec.GetCode()) w.Write(rec.GetBody().Bytes()) } return http.HandlerFunc(handler) }