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