当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.GetSecureCookie方法代码示例

本文整理汇总了Golang中github.com/gogits/gogs/modules/middleware.Context.GetSecureCookie方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.GetSecureCookie方法的具体用法?Golang Context.GetSecureCookie怎么用?Golang Context.GetSecureCookie使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/gogits/gogs/modules/middleware.Context的用法示例。


在下文中一共展示了Context.GetSecureCookie方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: SignIn

func SignIn(ctx *middleware.Context) {
	ctx.Data["Title"] = "Log In"

	if _, ok := ctx.Session.Get("socialId").(int64); ok {
		ctx.Data["IsSocialLogin"] = true
		ctx.HTML(200, "user/signin")
		return
	}

	if base.OauthService != nil {
		ctx.Data["OauthEnabled"] = true
		ctx.Data["OauthService"] = base.OauthService
	}

	// Check auto-login.
	userName := ctx.GetCookie(base.CookieUserName)
	if len(userName) == 0 {
		ctx.HTML(200, "user/signin")
		return
	}

	isSucceed := false
	defer func() {
		if !isSucceed {
			log.Trace("user.SignIn(auto-login cookie cleared): %s", userName)
			ctx.SetCookie(base.CookieUserName, "", -1)
			ctx.SetCookie(base.CookieRememberName, "", -1)
			return
		}
	}()

	user, err := models.GetUserByName(userName)
	if err != nil {
		ctx.HTML(500, "user/signin")
		return
	}

	secret := base.EncodeMd5(user.Rands + user.Passwd)
	value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName)
	if value != user.Name {
		ctx.HTML(500, "user/signin")
		return
	}

	isSucceed = true

	ctx.Session.Set("userId", user.Id)
	ctx.Session.Set("userName", user.Name)
	if redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to")); len(redirectTo) > 0 {
		ctx.SetCookie("redirect_to", "", -1)
		ctx.Redirect(redirectTo)
		return
	}

	ctx.Redirect("/")
}
开发者ID:jbeduya,项目名称:gogs,代码行数:56,代码来源:user.go

示例2: SignIn

func SignIn(ctx *middleware.Context, form auth.LogInForm) {
	ctx.Data["Title"] = "Log In"

	if ctx.Req.Method == "GET" {
		// Check auto-login.
		userName := ctx.GetCookie(base.CookieUserName)
		if len(userName) == 0 {
			ctx.HTML(200, "user/signin")
			return
		}

		isSucceed := false
		defer func() {
			if !isSucceed {
				log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName)
				ctx.SetCookie(base.CookieUserName, "", -1)
				ctx.SetCookie(base.CookieRememberName, "", -1)
			}
		}()

		user, err := models.GetUserByName(userName)
		if err != nil {
			ctx.HTML(200, "user/signin")
			return
		}

		secret := base.EncodeMd5(user.Rands + user.Passwd)
		value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName)
		if value != user.Name {
			ctx.HTML(200, "user/signin")
			return
		}

		isSucceed = true
		ctx.Session.Set("userId", user.Id)
		ctx.Session.Set("userName", user.Name)
		redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to"))
		if len(redirectTo) > 0 {
			ctx.SetCookie("redirect_to", "", -1)
			ctx.Redirect(redirectTo)
		} else {
			ctx.Redirect("/")
		}
		return
	}

	if ctx.HasError() {
		ctx.HTML(200, "user/signin")
		return
	}

	user, err := models.LoginUserPlain(form.UserName, form.Password)
	if err != nil {
		if err == models.ErrUserNotExist {
			log.Trace("%s Log in failed: %s/%s", ctx.Req.RequestURI, form.UserName, form.Password)
			ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
			return
		}

		ctx.Handle(200, "user.SignIn", err)
		return
	}

	if form.Remember == "on" {
		secret := base.EncodeMd5(user.Rands + user.Passwd)
		days := 86400 * base.LogInRememberDays
		ctx.SetCookie(base.CookieUserName, user.Name, days)
		ctx.SetSecureCookie(secret, base.CookieRememberName, user.Name, days)
	}

	ctx.Session.Set("userId", user.Id)
	ctx.Session.Set("userName", user.Name)
	redirectTo, _ := url.QueryUnescape(ctx.GetCookie("redirect_to"))
	if len(redirectTo) > 0 {
		ctx.SetCookie("redirect_to", "", -1)
		ctx.Redirect(redirectTo)
	} else {
		ctx.Redirect("/")
	}
}
开发者ID:JREAMLU,项目名称:gogs,代码行数:80,代码来源:user.go


注:本文中的github.com/gogits/gogs/modules/middleware.Context.GetSecureCookie方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。