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


Golang Gas.SetCookie方法代码示例

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


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

示例1: Output

func (o *rerouteOutputter) Output(code int, g *gas.Gas) {
	var cookieVal string

	if o.data != nil {
		buf := new(bytes.Buffer)
		enc := gob.NewEncoder(buf)
		err := enc.Encode(o.data)

		// TODO: do we want to ignore an encode error here?
		if err != nil {
			Error(g, err).Output(code, g)
			return
		}

		cookieVal = base64.StdEncoding.EncodeToString(buf.Bytes())
	}

	g.SetCookie(&http.Cookie{
		Path:     "/",
		Name:     "_reroute",
		Value:    cookieVal,
		Expires:  time.Now().Add(60 * time.Second),
		HttpOnly: true,
	})

	redirectOutputter(o.path).Output(code, g)
}
开发者ID:moshee,项目名称:gas,代码行数:27,代码来源:out.go

示例2: SignIn

// SignIn signs the user in by creating a new session and setting a cookie on
// the client.
func SignIn(g *gas.Gas, u User, password string) error {
	if store == nil {
		return ErrNoStore
	}
	// already signed in?
	sess, _ := GetSession(g)
	if sess != nil {
		cookie, err := g.Cookie("s")
		if err != nil && err != http.ErrNoCookie {
			return err
		}

		if err = VerifyCookie(cookie); err != nil {
			return err
		}

		id, err := base64.StdEncoding.DecodeString(cookie.Value)
		if err != nil {
			return err
		}
		//id := []byte(cookie.Value)

		if err := store.Update(id); err != nil {
			return err
		}

		return nil
	}

	pass, salt, err := u.Secrets()
	if err != nil {
		return err
	}
	if !VerifyHash([]byte(password), pass, salt) {
		return ErrBadPassword
	}

	username := u.Username()
	sessid := make([]byte, Env.SessidLen)
	rand.Read(sessid)
	err = store.Create(sessid, time.Now().Add(Env.MaxCookieAge), username)
	if err != nil {
		return err
	}

	cookie := &http.Cookie{
		Name:     "s",
		Path:     "/",
		Value:    base64.StdEncoding.EncodeToString(sessid),
		MaxAge:   int(Env.MaxCookieAge / time.Second),
		HttpOnly: true,
	}

	SignCookie(cookie)

	g.SetCookie(cookie)

	return nil
}
开发者ID:moshee,项目名称:gas,代码行数:61,代码来源:auth.go

示例3: SignOut

// SignOut signs the user out, destroying the associated session and cookie.
func SignOut(g *gas.Gas) error {
	if store == nil {
		return ErrNoStore
	}
	cookie, err := g.Cookie("s")
	if err != nil {
		return err
	}
	if err := VerifyCookie(cookie); err != nil {
		return err
	}

	id, err := base64.StdEncoding.DecodeString(cookie.Value)
	if err != nil {
		return err
	}
	//id := []byte(cookie.Value)

	if err := store.Delete(id); err != nil && err != sql.ErrNoRows {
		return err
	}

	cookie = &http.Cookie{
		Name:     "s",
		Path:     "/",
		Value:    "",
		Expires:  time.Time{},
		MaxAge:   -1,
		HttpOnly: true,
	}

	SignCookie(cookie)
	g.SetCookie(cookie)

	return nil
}
开发者ID:moshee,项目名称:gas,代码行数:37,代码来源:auth.go

示例4: CheckReroute

// CheckReroute is a middleware handler that will check for and deal with
// reroute cookies
func CheckReroute(g *gas.Gas) (int, gas.Outputter) {
	reroute, err := g.Cookie("_reroute")
	if reroute != nil {
		if err == nil {
			blob, err := base64.StdEncoding.DecodeString(reroute.Value)

			if err == nil {
				g.SetData("_reroute", blob)
			} else {
				log.Println("gas: dispatch reroute:", err)
			}
		} else {
			log.Println("gas: reroute cookie:", err)
		}

		// Empty the cookie out and toss it back
		reroute.Value = ""
		reroute.MaxAge = -1

		g.SetCookie(reroute)
	}

	return g.Continue()
}
开发者ID:moshee,项目名称:gas,代码行数:26,代码来源:out.go


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