Example #1
0
func (this *LoginRouter) Get() {
	// Set const
	this.Data["App_Name"] = App_Name
	// xsrf
	this.Data["xsrf"] = template.HTML(this.XsrfFormHtml())
	// set token in case twice submit
	Token := models.Token()
	this.SetSession("Token", Token)
	// set cookie not bot in case bots
	Cookies := models.RandString(20)
	this.SetSession("Cookie", Cookies)

	this.Data["Token"] = Token
	this.Data["Cookie"] = Cookies

	// Get referral
	Refer := this.Input().Get("username")
	if models.UserExist(Refer) {
		this.Data["Refer"] = Refer
	} else {
		models.Log(models.Log_Struct{"error", "Login:"******"No such referer")})
	}

	// Get User Session
	var user Session_User
	u := this.GetSession("_User")
	if u != nil {
		user = u.(Session_User)
		this.Data["User"] = user
	} else {
		this.Data["User"] = false
	}

	// Show the login page or Error page
	var showLogin bool
	v := this.GetSession("ShowLogin")
	if v != nil {
		showLogin = v.(bool)
		this.DelSession("ShowLogin")
	} else {
		showLogin = true
	}
	this.Data["ShowLogin"] = showLogin

	// Errors
	if !showLogin {
		e := this.GetSession("Error")
		if e != nil {
			this.Data["Error"] = GetError(e)
			this.DelSession("Error")
		}
	}

	this.TplNames = "login.html"
}
Example #2
0
func (this *RegisterRouter) Post() {
	// Get user inputs
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Register:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Register:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate user inputs
	username := strings.TrimSpace(inputs.Get("username"))
	password := strings.TrimSpace(inputs.Get("password"))
	re_password := strings.TrimSpace(inputs.Get("re-password"))
	fundpass := strings.TrimSpace(inputs.Get("fundpassword"))
	re_fundpass := strings.TrimSpace(inputs.Get("re-fundpassword"))
	email := strings.TrimSpace(inputs.Get("email"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))

	// Check referral
	refer := strings.TrimSpace(inputs.Get("refer"))
	if refer != "" && models.ValidString(refer) {
		if !models.UserExist(refer) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Referral user not exist.")})
			this.SetSession("Error", ERROR_REFERNOTEXIST)
			this.fail()
			return
		}
	}

	// Validate user inputs, set sessions and redirect
	if models.ValidString(username) && models.ValidString(password) && models.ValidString(re_password) && models.ValidString(fundpass) && models.ValidString(re_fundpass) && password == re_password && fundpass == re_fundpass && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("User already exist.")})
			this.SetSession("Error", ERROR_USEREXIST)
			this.fail()
			return
		}

		// Check if email exist
		if models.EmailExist(email) {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Email already exist.")})
			this.SetSession("Error", ERROR_EMAILEXIST)
			this.fail()
			return
		}

		// Generate new address for new user
		address, err := models.NewAddress(username)
		if err != nil {
			models.Log(models.Log_Struct{"info", "Register:", err})
			this.fail()
			this.SetSession("Error", ERROR_ADDRESS)
			return
		}

		// Insert new user to DB
		ok := models.NewUser(models.User{Username: username, Password: models.EncodePass(password), Fundpassword: models.EncodePass(fundpass), Email: email, Btcaddress: address, Birth: dateofbirth, Referral: refer})
		if !ok {
			models.Log(models.Log_Struct{"info", "Register:", errors.New("Unable to insert user, need to delete from wallet.")})
			err = models.Archive(address)
			if err != nil {
				models.Log(models.Log_Struct{"warn", "Register:", errors.New("Unable to archive.")})
			} else {
				models.Log(models.Log_Struct{"info", "Register:", errors.New("Succeed archiving address.")})
			}
			this.fail()
			this.SetSession("Error", ERROR_DB)
			return
		}

		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Register:", errors.New("Failed, invalid data")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Example #3
0
func (this *ForgetRouter) Post() {
	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Forget:", errors.New("Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Forget:", errors.New("No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate inputs
	username := strings.TrimSpace(inputs.Get("username"))
	dateofbirth := strings.TrimSpace(inputs.Get("birth"))
	email := strings.TrimSpace(inputs.Get("email"))

	if models.ValidString(username) && models.ValidEmail(email) && models.ValidBirth(dateofbirth) {
		// Check if user exist
		if !models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("User not exist.")})
			this.SetSession("Error", ERROR_USERNOTEXIST)
			this.fail()
			return
		}
		// Check if birth matches
		if !models.BirthMatch(username, dateofbirth) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Birth not match.")})
			this.SetSession("Error", ERROR_BIRTHNOTMATCH)
			this.fail()
			return
		}
		// Check if email mathces
		if !models.EmailMatch(username, email) {
			models.Log(models.Log_Struct{"info", "Forget:", errors.New("Email not match.")})
			this.SetSession("Error", ERROR_EMAILNOTMATCH)
			this.fail()
			return
		}

		// Send Email to authenticate
		authen := models.RandString(8)
		if !models.SendEmail(email, "重设密码", username+":  请复制验证码,以完成重设密码操作---->", authen) {
			models.Log(models.Log_Struct{"warn", "Forget:", errors.New("Cant send email to authen password reset.")})
			this.SetSession("Error", ERROR_EMAILNOTSENT)
			this.fail()
			return
		}

		this.SetSession("Username", username)
		this.SetSession("Authen", authen)
		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Forget:", errors.New("Failed, invalid data.")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}
Example #4
0
func (this *LoginRouter) Post() {
	//fmt.Println(this.CheckXsrfCookie())

	// Get inputs and validate
	inputs := this.Input()

	// Check token in case twice submit
	var token string
	token_sess := this.GetSession("Token")
	if token_sess != nil {
		this.DelSession("Token")
		token = fmt.Sprintf("%d", token_sess.(int64))
	}
	if token != inputs.Get("token") {
		this.SetSession("Error", ERROR_TWICESUBMIT)
		models.Log(models.Log_Struct{"error", "Login:"******"Submit twice")})
		this.fail()
		return
	}

	// Check cookie in case bots
	cookie_sess := this.GetSession("Cookie")
	if cookie_sess != nil {
		this.DelSession("Cookie")
		cookie := cookie_sess.(string)
		if cookie != this.Ctx.GetCookie("nobot") {
			this.SetSession("Error", ERROR_CAPTCHA)
			models.Log(models.Log_Struct{"error", "Login:"******"No bot is allowed")})
			this.fail()
			return
		}
	}

	// Validate user inputs
	username := strings.TrimSpace(inputs.Get("username"))
	password := strings.TrimSpace(inputs.Get("password"))

	if models.ValidString(username) && models.ValidString(password) {
		// Check if user exist
		if !models.UserExist(username) {
			models.Log(models.Log_Struct{"info", "Login:"******"User not exist")})
			this.SetSession("Error", ERROR_USERNOTEXIST)
			this.fail()
			return
		}
		// Check if password correct
		if !models.PassMatch(username, password) {
			models.Log(models.Log_Struct{"info", "Login:"******"Password incorrect")})
			this.SetSession("Error", ERROR_PASSINCORRECT)
			this.fail()
			return
		}

		// Update IP
		ip := strings.Split(this.Ctx.Request.RemoteAddr, ":")[0]
		models.UpdateIP(username, ip)

		// Get Uid
		id := models.UidByUsername(username)

		this.SetSession("_User", Session_User{Uid: id, Username: username, Ip: ip})
		this.succ()
		return
	}

	models.Log(models.Log_Struct{"info", "Login:"******"Failed, invalid data")})
	this.SetSession("Error", ERROR_INVALIDINPUT)
	this.fail()
}