func GetMatchedSession(w http.ResponseWriter, r *http.Request) (*MySession, error) { sessionId := hmSessionCookie.GetOne(SESSION_COOKIE_KEY, r) if len(sessionId) == 0 { warningMsg := fmt.Sprintf("Not found matched session! No session cookie!") go_lib.LogWarnln(warningMsg) return nil, nil } sessionkey := generateSessionKey(sessionId) exists, err := dao.Exists(sessionkey) if err != nil { return nil, err } if !exists { warningMsg := fmt.Sprintf("Not found matched session! No session in storage! (sessionId=%s, sessionKey=%s)", sessionId, sessionkey) go_lib.LogWarnln(warningMsg) return nil, nil } grantors, err := dao.GetHash(sessionkey, SESSION_GRANTORS_KEY) if err != nil { return nil, err } if len(grantors) == 0 { warningMsg := fmt.Sprintf("Not found grantor from session (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_GRANTORS_KEY) go_lib.LogWarnln(warningMsg) return nil, nil } groupName, err := dao.GetHash(sessionkey, SESSION_GROUP_KEY) if err != nil { return nil, err } if len(groupName) == 0 { warningMsg := fmt.Sprintf("Not found group name from session (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_GROUP_KEY) go_lib.LogWarnln(warningMsg) return nil, err } servivalSecondsLiterals, err := dao.GetHash(sessionkey, SESSION_SURVIVAL_SECONDS_KEY) if err != nil { return nil, err } if len(servivalSecondsLiterals) == 0 { warningMsg := fmt.Sprintf("Not found session servival seconds. Use default value '0'. (sessionKey=%s, attribute=%s)!\n", sessionkey, SESSION_SURVIVAL_SECONDS_KEY) go_lib.LogWarnln(warningMsg) return nil, err } hmSession := &MySession{key: sessionkey, sessionId: sessionId, w: w, r: r} return hmSession, nil }
func (self *MySession) Initialize( grantors string, // survivalSeconds: // '<0'- Don't set/Delete cookie; // '0' - Set temporary cookie; // '>0'- Set long term cookie according to this value & Set session expires survivalSeconds int, w http.ResponseWriter, r *http.Request) error { if len(grantors) == 0 { errorMsg := fmt.Sprintln("The session grantors is EMPTY!") return errors.New(errorMsg) } if w == nil { errorMsg := fmt.Sprintln("The pointer of http response writer is NIL!") return errors.New(errorMsg) } if r == nil { errorMsg := fmt.Sprintln("The pointer of http request is NIL!") return errors.New(errorMsg) } self.w = w self.r = r self.sessionId = generateSessionId(grantors, r) self.key = generateSessionKey(self.sessionId) go_lib.LogInfof("Initialize session (key=%s, grantors=%s)...\n", self.key, grantors) exists, err := dao.Exists(self.key) if err != nil { return err } if exists { _, err = dao.DelKey(self.key) if err != nil { return err } } _, err = dao.SetHash(self.key, SESSION_GRANTORS_KEY, grantors) if err != nil { return err } _, err = dao.SetHash(self.key, SESSION_SURVIVAL_SECONDS_KEY, strconv.FormatInt(int64(survivalSeconds), 10)) if err != nil { return err } cookieMaxAge := survivalSeconds if survivalSeconds <= 0 { cookieMaxAge = -1 } go_lib.LogInfof("Set session cookie (value=%s, grantors=%s, maxAge=%d)...\n", self.sessionId, grantors, cookieMaxAge) result := hmSessionCookie.SetOne(self.w, SESSION_COOKIE_KEY, self.sessionId, cookieMaxAge) if result { go_lib.LogInfof("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is successful.\n", self.sessionId, grantors, cookieMaxAge) } else { go_lib.LogWarnf("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is failing!\n", self.sessionId, grantors, cookieMaxAge) } if survivalSeconds > 0 { done, err := dao.SetExpires(self.key, uint64(survivalSeconds)) if err != nil || !done { warningMsg := fmt.Sprintf("Setting session expires failed! (key=%s, survivalSeconds=%d, error=%s)\n", self.key, survivalSeconds, err) go_lib.LogWarnln(warningMsg) } } _, err = dao.SetHash(SESSION_MAP_KEY, grantors, self.key) if err != nil { return err } return nil }