當前位置: 首頁>>代碼示例>>Golang>>正文


Golang SecureCookie.Encode方法代碼示例

本文整理匯總了Golang中github.com/gorilla/securecookie.SecureCookie.Encode方法的典型用法代碼示例。如果您正苦於以下問題:Golang SecureCookie.Encode方法的具體用法?Golang SecureCookie.Encode怎麽用?Golang SecureCookie.Encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gorilla/securecookie.SecureCookie的用法示例。


在下文中一共展示了SecureCookie.Encode方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: StoreCookie

func StoreCookie(w http.ResponseWriter, r *http.Request, storedCookie *securecookie.SecureCookie, cookieName, cookieValue string) {
	value := map[string]string{
		cookieName: cookieValue,
	}
	if encoded, err := storedCookie.Encode(cookieName, value); err == nil {
		cookie := &http.Cookie{
			Name:  cookieName,
			Value: encoded,
			Path:  "/",
		}
		http.SetCookie(w, cookie)
	}
}
開發者ID:rjourde,項目名稱:udacity.cs253.go,代碼行數:13,代碼來源:cookies.go

示例2: Connect

func Connect(w http.ResponseWriter, r *http.Request, u auth.User, sessionStore sessions.Store, secureCookie *securecookie.SecureCookie, dbStore *Store) {
	StatCount("connect call", 1)
	session, err := sessionStore.Get(r, SESSIONNAME)
	if err != nil {
		xlog.Errorf("Error fetching session: %v", err)
		session, _ = sessionStore.New(r, SESSIONNAME)
	}

	if userID, ok := session.Values["userID"].(int); ok {
		xlog.Debugf("Connect: already logged in (userID = %d), connecting account", userID)
		// we have a valid session -> connect account to user
		username := u.Provider() + ":" + u.Id()

		err := dbStore.AddUser(username, userID)
		if err != nil {
			xlog.Errorf("Error adding user: %v", err)
			http.Error(w, err.Error(), http.StatusForbidden)
			return
		}

		w.Header().Set("Location", "/settings")
	} else {
		xlog.Debugf("Connect: not logged in, actually log in user.")
		// no valid session -> actually login user
		username := u.Provider() + ":" + u.Id()
		xlog.Debugf("Connect: username = %s", username)
		userID, err := dbStore.CreateUser(username)
		if err != nil {
			xlog.Errorf("Error creating user: %v", err)
			http.Error(w, err.Error(), http.StatusForbidden)
			return
		}

		xlog.Debugf("Connect: userID = %d", userID)

		// set session values
		session.Values["userID"] = userID
		session.Values["username"] = username
		session.Values["email"] = u.Email()
		session.Values["name"] = u.Name()
		session.Save(r, w)

		// set XSRF-TOKEN for AngularJS
		xsrftoken, _ := secureCookie.Encode(XSRFTOKEN, username)
		http.SetCookie(w, &http.Cookie{Name: XSRFTOKEN, Value: xsrftoken, Path: "/"})

		w.Header().Set("Location", "/")
	}
	w.WriteHeader(http.StatusFound)
}
開發者ID:joinmytalk,項目名稱:satsuma,代碼行數:50,代碼來源:login.go

示例3: CreateCookie

func CreateCookie(s *securecookie.SecureCookie) (*http.Cookie, error) {
	var err error

	// Create secure cookie with login info
	value := map[string]string{
		"authenticated": "true",
	}
	if encoded, err := s.Encode("whiteboard", value); err == nil {
		cookie := &http.Cookie{
			Name:  "whiteboard",
			Value: encoded,
			Path:  "/",
		}
		cookie.MaxAge = 10000
		return cookie, err
	}

	return nil, err
}
開發者ID:hunterpraska,項目名稱:Whiteboard,代碼行數:19,代碼來源:auth.go

示例4: Cookie

func (ac *agentCredentials) Cookie(sc *securecookie.SecureCookie) (*http.Cookie, error) {
	encoded, err := json.Marshal(ac)
	if err != nil {
		return nil, err
	}

	secured, err := sc.Encode(agentCookieName, encoded)
	if err != nil {
		return nil, err
	}

	cookie := &http.Cookie{
		Name:     agentCookieName,
		Value:    secured,
		Path:     "/",
		Expires:  time.Now().Add(agentCookieDuration),
		HttpOnly: true,
	}
	return cookie, nil
}
開發者ID:Tt2uang,項目名稱:heim,代碼行數:20,代碼來源:agent.go


注:本文中的github.com/gorilla/securecookie.SecureCookie.Encode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。