本文整理汇总了Golang中github.com/goincremental/negroni-sessions.Session类的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleOAuth2Callback
func handleOAuth2Callback(config *oauth2.Config, s sessions.Session, w http.ResponseWriter, r *http.Request) {
providedState := extractPath(r.URL.Query().Get("state"))
//fmt.Printf("Got state from request %s\n", providedState)
//verify that the provided state is the state we generated
//if it is not, then redirect to the error page
originalState := s.Get(keyState)
//fmt.Printf("Got state from session %s\n", originalState)
if providedState != originalState {
//http.Redirect(w, r, PathError, http.StatusFound)
//return
}
//next := s.Get(keyNextPage).(string)
//fmt.Printf("Got a next page from the session: %s\n", next)
next := ""
code := r.URL.Query().Get("code")
t, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
// Pass the error message, or allow dev to provide its own
// error handler.
fmt.Println("There is some error in the code exchange")
http.Redirect(w, r, PathError, http.StatusFound)
return
}
// Store the credentials in the session.
val, _ := json.Marshal(t)
s.Set(KeyToken, maskval(val))
http.Redirect(w, r, next, http.StatusFound)
}
示例2: unmarshallToken
func unmarshallToken(s sessions.Session) *token {
if s.Get(KeyToken) == nil {
return nil
}
data := getMaskValue(s.Get(KeyToken).(string))
var tk oauth2.Token
json.Unmarshal(data, &tk)
return &token{tk}
}
示例3: unmarshallToken
func unmarshallToken(s sessions.Session) *token {
if s.Get(keyToken) == nil {
return nil
}
data := s.Get(keyToken).([]byte)
var tk oauth2.Token
json.Unmarshal(data, &tk)
return &token{tk}
}
示例4: login
func login(opts *Options, config *oauth2.Config, s sessions.Session, w http.ResponseWriter, r *http.Request) {
next := extractPath(r.URL.Query().Get(keyNextPage))
if s.Get(keyToken) == nil {
// User is not logged in.
if next == "" {
next = "/"
}
http.Redirect(w, r, config.AuthCodeURL(next, oauth2.AccessTypeOffline), http.StatusFound)
return
}
// No need to login, redirect to the next page.
http.Redirect(w, r, next, http.StatusFound)
}
示例5: handleOAuth2Callback
func handleOAuth2Callback(opts *Options, config *oauth2.Config, s sessions.Session, w http.ResponseWriter, r *http.Request) {
next := extractPath(r.URL.Query().Get("state"))
code := r.URL.Query().Get("code")
t, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
// Pass the error message, or allow dev to provide its own
// error handler.
http.Redirect(w, r, PathError, http.StatusFound)
return
}
// Store the credentials in the session.
val, _ := json.Marshal(t)
s.Set(keyToken, val)
http.Redirect(w, r, next, http.StatusFound)
}
示例6: handleOAuth2Callback
func handleOAuth2Callback(config *oauth2.Config, s sessions.Session, w http.ResponseWriter, r *http.Request) {
providedState := extractPath(r.URL.Query().Get("state"))
//verify that the provided state is the state we generated
//if it is not, then redirect to the error page
originalState := s.Get(keyState)
if providedState != originalState {
http.Redirect(w, r, PathError, http.StatusFound)
return
}
next := s.Get(keyNextPage).(string)
code := r.URL.Query().Get("code")
t, err := config.Exchange(oauth2.NoContext, code)
if err != nil {
// Pass the error message, or allow dev to provide its own
// error handler.
http.Redirect(w, r, PathError, http.StatusFound)
return
}
// Store the credentials in the session.
val, _ := json.Marshal(t)
s.Set(keyToken, val)
http.Redirect(w, r, next, http.StatusFound)
}
示例7: login
func login(config *oauth2.Config, s sessions.Session, w http.ResponseWriter, r *http.Request) {
next := extractPath(r.URL.Query().Get(keyNextPage))
if s.Get(keyToken) == nil {
// User is not logged in.
if next == "" {
next = "/"
}
state := newState()
// store the next url and state token in the session
s.Set(keyState, state)
s.Set(keyNextPage, next)
http.Redirect(w, r, config.AuthCodeURL(state, oauth2.AccessTypeOffline), http.StatusFound)
return
}
// No need to login, redirect to the next page.
http.Redirect(w, r, next, http.StatusFound)
}
示例8: logout
func logout(s sessions.Session, w http.ResponseWriter, r *http.Request) {
next := extractPath(r.URL.Query().Get(keyNextPage))
s.Delete(KeyToken)
http.Redirect(w, r, next, http.StatusFound)
}