Пример #1
0
func (lo *LoginMap) Callback(w *wrapper.Wrapper) {
	oauthlogins := make(map[string]map[string]string)
	w.SiteConfig.RawConfig.MarshalKey("OAuthLogins", &oauthlogins)
	loginurls := make(map[string]string)
	w.SiteConfig.RawConfig.MarshalKey("LoginURLs", &loginurls)
	if _, ok := oauthlogins[w.APIParams[0]]; ok {
		if _, ok := lo.Logins[w.APIParams[0]]; ok {
			s := w.Request.FormValue("state")
			sc := oauthlogins[w.APIParams[0]]
			login := lo.Logins[w.APIParams[0]]
			if lo.State != s {
				errmessage := fmt.Sprintf("Invalid oauth state, expected %s, got %s", lo.State, s)
				w.SiteConfig.Logger.Error(errmessage)
				http.Redirect(w.Writer, w.Request, loginurls["failure"], 301)
				return
			}
			login.SetConfig(sc, "", "")
			code := w.Request.FormValue("code")
			token, err := login.GetToken(code)
			if err != nil {
				errmessage := fmt.Sprintf("Exchange() failed with %s", err.Error())
				w.SiteConfig.Logger.Error(errmessage)
				http.Redirect(w.Writer, w.Request, loginurls["failure"], 301)
				return
			}
			u := login.GetUser()
			err = u.Set(w)
			if err != nil {
				errmessage := fmt.Sprintf("Unable to set user: %s", err.Error())
				w.SiteConfig.Logger.Error(errmessage)
				http.Redirect(w.Writer, w.Request, loginurls["failure"], 301)
				return
			}
			err = w.SetSessionValue("user_id", u.MongoId)
			if err != nil {
				errmessage := fmt.Sprintf("Unable to set user id on session: %s", err.Error())
				w.SiteConfig.Logger.Error(errmessage)
				http.Redirect(w.Writer, w.Request, loginurls["failure"], 301)
				return
			}
			err = w.SetSessionValue("token", token)
			if err != nil {
				errmessage := fmt.Sprintf("Unable to set token on session: %s", err.Error())
				w.SiteConfig.Logger.Error(errmessage)
				http.Redirect(w.Writer, w.Request, loginurls["failure"], 301)
				return
			}
			http.Redirect(w.Writer, w.Request, loginurls["success"], 301)
			return
		}
	}
	http.Error(w.Writer, "Forbidden", 403)
	return
}