// get login redirect url from cookie func GetLoginRedirect(ctx *context.Context) string { loginRedirect := strings.TrimSpace(ctx.GetCookie("login_to")) if utils.IsMatchHost(loginRedirect) == false { loginRedirect = "/" } else { ctx.SetCookie("login_to", "", -1, "/") } return loginRedirect }
func giveCookie(ctx *context.Context) { _, err := ctx.Request.Cookie("uid") if err != nil { //没有cookie uid := GetID() h := md5.New() h.Write([]byte(uid)) cookie := hex.EncodeToString(h.Sum(nil)) err = models.AddUser(uid, cookie) if err != nil { return } ctx.SetCookie("uid", cookie, 1<<31-1) beego.Info(cookie) } return }
// readFlash is similar to beego.ReadFromRequest except that it takes a *context.Context instead // of a *beego.Controller, and returns a map[string]string directly instead of a Beego.FlashData // (which only has a Data field anyway). func readFlash(ctx *context.Context) map[string]string { data := map[string]string{} if cookie, err := ctx.Request.Cookie(beego.FlashName); err == nil { v, _ := url.QueryUnescape(cookie.Value) vals := strings.Split(v, "\x00") for _, v := range vals { if len(v) > 0 { kv := strings.Split(v, "\x23"+beego.FlashSeperator+"\x23") if len(kv) == 2 { data[kv[0]] = kv[1] } } } // read one time then delete it ctx.SetCookie(beego.FlashName, "", -1, "/") } return data }
// SetLangVer sets site language version. // It returns true when language is changed by URL parameters. func SetLangVer(ctx *context.Context, input url.Values, data map[interface{}]interface{}) (bool, LangType) { isNeedRedir := false // 1. Check URL arguments. lang := input.Get("lang") // 2. Get language information from cookies. if len(lang) == 0 { ck, err := ctx.Request.Cookie("lang") if err == nil { lang = ck.Value } } else { isNeedRedir = true } // Check again in case someone modify by purpose. isValid := false for _, v := range LangTypes { if lang == v.Lang { isValid = true break } } if !isValid { lang = "" isNeedRedir = false } // 3. Get language information from 'Accept-Language'. if len(lang) == 0 { al := ctx.Request.Header.Get("Accept-Language") if len(al) > 2 { al = al[:2] // Only compare first two letters. for _, v := range LangTypes { if al == v.Lang { lang = al break } } } } // 4. DefaucurLang language is English. if len(lang) == 0 { lang = "en-US" isNeedRedir = false } curLang := LangType{ Lang: lang, } // Save language information in cookies. ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/") restLangs := make([]*LangType, 0, len(LangTypes)-1) for _, v := range LangTypes { if lang != v.Lang { restLangs = append(restLangs, v) } else { curLang.Name = v.Name } } // Set language properties. data["Lang"] = curLang.Lang data["CurLang"] = curLang.Name data["RestLangs"] = restLangs return isNeedRedir, curLang }
func DeleteRememberCookie(ctx *context.Context) { ctx.SetCookie(setting.CookieUserName, "", -1) ctx.SetCookie(setting.CookieRememberName, "", -1) }
func WriteRememberCookie(user *models.User, ctx *context.Context) { secret := utils.EncodeMd5(user.Rands + user.Password) days := 86400 * setting.LoginRememberDays ctx.SetCookie(setting.CookieUserName, user.UserName, days) ctx.SetSecureCookie(secret, setting.CookieRememberName, user.UserName, days) }
func (this *BaseController) DeleteRememberCookie(ctx *context.Context) { ctx.SetCookie(setting.CookieUsername, "", -1) ctx.SetCookie(setting.CookieRememberName, "", -1) }
func (this *BaseController) WriteRememberCookie(u *user.User, ctx *context.Context) { secret := utils.EncodeMd5(u.PasswordSalt + u.Password) days := 86400 * setting.LoginRememberDays ctx.SetCookie(setting.CookieUsername, u.Username, days) ctx.SetSecureCookie(secret, setting.CookieRememberName, u.Username, days) }
func SetCookie(ctx *context.Context, username, password string, expire int) { ctx.SetCookie("uname", username, expire, "/") ctx.SetCookie("pwd", password, expire, "/") }