本文整理匯總了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)
}
}
示例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)
}
示例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
}
示例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
}