Ejemplo n.º 1
0
func Auth_Login_POST(rw http.ResponseWriter, req *http.Request) {
	email := req.PostFormValue("email")
	password := req.PostFormValue("password")
	userID, ak, isAdmin := ValidateUserEmailPassword(req, email, password)
	if userID == 0 {
		session.AddFlash(req, session.Flash{
			Severity: "error",
			Msg:      "Invalid e. mail address or password.",
		})
		Auth_Login_GET(rw, req)
		return
	}

	session.Set(req, "user_id", int(userID))
	session.Set(req, "user_ak", ak)
	session.Set(req, "user_is_admin", isAdmin)

	if req.PostFormValue("remember") != "" {
		session.Set(req, "session_lifetime", 90*24*time.Hour)
	}

	session.Bump(req)

	session.AddFlash(req, session.Flash{
		Severity: "success",
		Msg:      "You have been logged in.",
	})

	authz.ReturnRedirect(req, 302, authz.AfterLoginURL)
}
Ejemplo n.º 2
0
func Auth_Verify_GET(rw http.ResponseWriter, req *http.Request) {
	ac := req.FormValue("ac")
	reset_s := req.FormValue("r")
	email := req.FormValue("e")

	var userID int64
	var ak []byte
	var verified bool
	var isAdmin bool
	err := GetBackend(req).GetDatabase().QueryRow("SELECT node_id, ak, is_admin, email_verified FROM \"n_user\" WHERE email=$1 LIMIT 1", email).Scan(&userID, &ak, &isAdmin, &verified)
	log.Panice(err, "find ak for e. mail verify")

	if !webac.VerifyFor("verify-email/"+reset_s+"/"+email, ac, ak) {
		rw.WriteHeader(400)
		tpl.MustShow(req, "front/400", nil)
		return
	}

	if !verified {
		_, err = dbutil.UpdateKV(GetBackend(req).GetDatabase(), "n_user", dbutil.Set{"email_verified": true}, dbutil.Where{"node_id": userID})
		log.Panice(err)
	}

	if reset_s == "1" {
		_, err = rand.Read(ak)
		log.Panice(err)

		_, err = dbutil.UpdateKV(GetBackend(req).GetDatabase(), "n_user", dbutil.Set{"ak": ak}, dbutil.Where{"node_id": userID})
	}

	// log user in
	if reset_s == "0" {
		if verified {
			// non-reset links cannot be used to get a free login to an already-verified account
			rw.WriteHeader(400)
			tpl.MustShow(req, "front/400", nil)
			return
		}

		session.AddFlash(req, session.Flash{
			Severity: "success",
			Msg:      "Your e. mail address has been verified.",
		})
	} else {
		session.Set(req, "must_change_password", true)
	}
	session.Set(req, "user_id", int(userID))
	session.Set(req, "user_ak", ak)
	session.Set(req, "user_is_admin", isAdmin)
	session.Bump(req)

	miscctx.SeeOther(req, "/panel")
}