func GetCv(w http.ResponseWriter, r *http.Request) { r.ParseForm() w.WriteHeader(200) w.Header().Set("Content-Type", "text/html; charset=utf-8") attrMap := request.GenerateBasicAttrMap(w, r) loginName := attrMap[request.LOGIN_NAME_KEY] go_lib.LogInfoln(request.GetRequestInfo(r)) auth_code := r.FormValue(request.AUTH_CODE) go_lib.LogInfof("Getting CV by user '%s' with input '%s'...\n", loginName, auth_code) pass, err := request.VerifyAuthCode(auth_code) if err != nil { go_lib.LogErrorf("Occur error when verify auth code: %s\n", err) // w.WriteHeader(500) fmt.Fprintln(w, "Error: Something wrong when verify auth code!") return } if !pass { go_lib.LogWarnf("Unauthorized CV getting by user '%s' with input '%s'.\n", loginName, auth_code) // w.WriteHeader(401) fmt.Fprintln(w, "FAIL: Wrong authorization code.") return } cvContent, err := base.GetCvContent() if err != nil { go_lib.LogErrorf("Occur error when get cv content: %s.\n", err) // w.WriteHeader(500) fmt.Fprintln(w, "Error: Something wrong when get CV content!") return } fmt.Fprintln(w, cvContent) go_lib.LogInfof("The CV had taken by user '%s' with input '%s'.\n", loginName, auth_code) }
func ClearPageAccessRecord(pageName string, visitor string) (bool, error) { if len(pageName) == 0 { return false, errors.New("The parameter named pageName is EMPTY!") } sign := getSignForPage(pageName) sign.Set() defer sign.Unset() parameterInfo := fmt.Sprintf("(pageName=%s, visitor=%s)", pageName, visitor) var result bool conn := dao.RedisPool.Get() defer conn.Close() value, err := dao.GetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName) if err != nil { return false, err } visitorAccessRecords, err := parseVisitorAccessRecords(value) if err != nil { go_lib.LogErrorf("Parsing visitor access records error: %s %s\n", err, parameterInfo) } if visitorAccessRecords != nil { _, ok := visitorAccessRecords[visitor] if ok { delete(visitorAccessRecords, visitor) literals, err := formatVisitorAccessRecords(visitorAccessRecords) if err != nil { go_lib.LogErrorf("Formating visitor access records error: %s %s\n", err, parameterInfo) } else { result, err = dao.SetHash(dao.PAGE_ACCESS_RECORDS_KEY, pageName, literals) if err != nil { return false, err } } } } if result { go_lib.LogInfof("The page access info has been cleared. %s\n", parameterInfo) } else { go_lib.LogWarnf("The page access info failed to clear. %s\n", parameterInfo) } return result, 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 }
func Login(w http.ResponseWriter, r *http.Request) { r.ParseForm() go_lib.LogInfoln(request.GetRequestInfo(r)) attrMap := request.GenerateBasicAttrMap(w, r) loginName := attrMap[request.LOGIN_NAME_KEY] if r.Method == "GET" { token := request.GenerateToken(r, loginName) go_lib.LogInfof("Token: %v\n", token) request.SaveToken(token) attrMap := request.GenerateBasicAttrMap(w, r) attrMap[request.TOKEN_KEY] = token.Key hint := r.FormValue(request.HINT_KEY) if len(hint) > 0 { attrMap[request.HINT_KEY] = hint } t, err := template.ParseFiles(request.GeneratePagePath("login"), request.GeneratePagePath("common")) if err != nil { go_lib.LogErrorln("TemplateParseErr:", err) } err = t.ExecuteTemplate(w, "page", attrMap) if err != nil { go_lib.LogErrorln("PageWriteErr:", err) } } else { r.ParseForm() tokenKey := r.Form.Get(request.TOKEN_KEY) go_lib.LogInfoln("Token Key:", tokenKey) validToken := request.CheckToken(tokenKey) if !validToken { go_lib.LogWarnf("Invalid token key '%s' ! Ignore the login request.", tokenKey) r.Method = "GET" http.Redirect(w, r, r.URL.Path, http.StatusFound) return } else { request.RemoveToken(tokenKey) } loginName = template.HTMLEscapeString(r.Form.Get(request.LOGIN_NAME_KEY)) go_lib.LogInfoln("login - loginName:", loginName) password := template.HTMLEscapeString(r.Form.Get(request.PASSWORD_KEY)) go_lib.LogInfoln("login - password:"******"remember-me") go_lib.LogInfoln("login - remember-me:", rememberMe) validLogin, err := rights.VerifyUser(loginName, password) go_lib.LogInfoln("Verify user:"******"/" if err != nil { go_lib.LogErrorf("VerifyUserError (loginName=%s): %s\n", loginName, err) redirectPath = r.URL.Path } else { if validLogin { longTerm := len(rememberMe) == 0 || rememberMe != "y" _, err = session.NewSession(loginName, longTerm, w, r) if err != nil { go_lib.LogErrorf("SetSessionError (loginName=%s): %s\n", loginName, err) } } else { hint := "Wrong login name or password." redirectPath = request.AppendParameter(r.URL.Path, map[string]string{request.HINT_KEY: hint}) } } go_lib.LogInfof("RPATH: %s\n", redirectPath) http.Redirect(w, r, redirectPath, http.StatusFound) } }