Ejemplo n.º 1
0
func doLogin(w http.ResponseWriter, r *http.Request) {
	importedTemplate, err := template.ParseFile("../templates/base.html")
	if err != nil {
		http.Error(w, "ParseFile: "+err.Error(), http.StatusInternalServerError)
		log.Print("doLogin > ParseFile: " + err.Error())
		return
	}
	loggedIn, user, err := getCurrentUser(r)
	if err != nil {
		http.Error(w, "getCurrentUser: "******"doLogin > getCurrentUser: "******"PushURL":    "ws://" + r.Host + "/ws",
		"LogInLink":  getLoginURL(r),
		"LogOutLink": "/logout",
		"IsLoggedIn": loggedIn,
		"User":       user,
		"OpenLogin":  true,
	}
	if err := importedTemplate.Execute(w, templateVars); err != nil {
		http.Error(w, "importedTemplate.Execute: "+err.Error(), http.StatusInternalServerError)
		log.Print("doLogin > importedTemplate.Execute: " + err.Error())
		return
	}
}
Ejemplo n.º 2
0
func registerHandler(w http.ResponseWriter, r *http.Request) {
	importedTemplate, err := template.ParseFile("../templates/users/register.html")
	if err != nil {
		http.Error(w, "parseFile: "+err.Error(), http.StatusInternalServerError)
		log.Print("registerHandler > parseFile: " + err.Error())
		return
	}
	templateVars := map[string]interface{}{}
	if err := importedTemplate.Execute(w, templateVars); err != nil {
		http.Error(w, "importedTemplate.Execute: "+err.Error(), http.StatusInternalServerError)
		log.Print("registerHandler > importedTemplate.Execute: " + err.Error())
		return
	}
}
Ejemplo n.º 3
0
func openID(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		grant, _, ax, err := openid.VerifyValues(r.URL.Query())
		if grant {
			user, err := getUserByAccount(ax["Email"].(string), "Google")
			if err != nil {
				http.Error(w, "getUserByAccount: "+err.Error(), http.StatusInternalServerError)
				log.Print("openID > GET > getUserByAccount: " + err.Error())
				return
			}
			if user == nil {
				importedTemplate, err := template.ParseFile("../templates/users/register.html")
				if err != nil {
					http.Error(w, "importedTemplate: "+err.Error(), http.StatusInternalServerError)
					log.Print("openID > GET > ParseFile: " + err.Error())
					return
				}
				templateVars := map[string]interface{}{
					"AX": ax,
				}
				if err := importedTemplate.Execute(w, templateVars); err != nil {
					http.Error(w, "importedTemplate.Execute: "+err.Error(), http.StatusInternalServerError)
					log.Print("openID > GET > importedTemplate.Execute: " + err.Error())
					return
				}
			} else {
				if r.URL.Path == "/openid/callback/chrome" {
					w.Header().Set("Content-Type", "application/json")
					response := map[string]interface{}{
						"success": true,
						"error":   "",
						"data": map[string]string{
							"user":   user.Username.Lowercase,
							"secret": user.Secret,
						},
					}
					enc := json.NewEncoder(w)
					enc.Encode(response)
					return
				} else {
					login(w, r, user.Username.Lowercase)
					http.Redirect(w, r, "/", http.StatusFound)
					return
				}
			}
		} else {
			if err != nil {
				http.Error(w, "openid.verify: "+err.Error(), http.StatusInternalServerError)
				log.Print("openID > GET > openid.VerifyValues: " + err.Error())
				return
			} else {
				log.Print("openid > GET > redirecting...")
				http.Redirect(w, r, "/", http.StatusFound)
				return
			}
		}
	} else if r.Method == "POST" {
		r.ParseForm()
		grant, _, ax, err := openid.VerifyValues(r.URL.Query())
		if err != nil {
			http.Error(w, "openid.verify: "+err.Error(), http.StatusInternalServerError)
			log.Print("openID > POST > openid.VerifyValues: " + err.Error())
			return
		}
		if grant {
			user, _ := getUserByAccount(ax["Email"].(string), "Google")
			if user == nil {
				username := r.FormValue("Username")
				if valid, err := validateUsername(username); !valid {
					http.Error(w, "validateUsername: "******"openID > POST > validateUsername: "******"Email")
					if valid, err := validateEmail(email); !valid {
						http.Error(w, "validateEmail: "+err.Error(), http.StatusInternalServerError)
						log.Print("openID > POST > validateEmail: " + err.Error())
						return
					} else {
						key := strings.TrimSpace(r.FormValue("BetaKey"))
						if valid, err := validateBetaKey(key); !valid {
							if err != nil {
								http.Error(w, "validateBetaKey: "+err.Error(), http.StatusInternalServerError)
								log.Print("openID > POST > validateBetaKey: " + err.Error())
							} else {
								http.Error(w, "Invalid beta key.", http.StatusInternalServerError)
							}
							return
						} else {
							givenName := r.FormValue("GivenName")
							familyName := r.FormValue("FamilyName")
							values := make(url.Values)
							values.Add("Username", username)
							values.Add("Email", email)
							values.Add("Name.Given", givenName)
							values.Add("Name.Family", familyName)
							values.Add("Account.Identifier", ax["Email"].(string))
							values.Add("Account.Type", "Google")
							values.Add("Account.AuthMethod", "OpenID")
							err = useKey(key, username)
							if err != nil {
								log.Print("Key: " + key + ", User: "******"openID > POST > useKey: " + err.Error())
							}
							user, err := register(values)
							if err != nil {
								log.Print(user)
								log.Print("openID > POST > register: " + err.Error())
							}
							login(w, r, user.Username.Lowercase)
							http.Redirect(w, r, "/?registered=true", http.StatusFound)
							return
						}
					}
				}
			}
		}
	}
}